#!/bin/bash
# ================================================================
# copy_headers.sh — 公共头文件复制到 release/inc/
#
# 用法:
#   release/copy_headers.sh          # 从工程根目录调用
#   make headers                     # 通过 makefile 调用
#
# 新增模块时，在本脚本末尾追加一行:
#   copy_module "模块名" "头文件名"
# ================================================================
set -e

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
SRC_DIR="$ROOT_DIR/src"
DST_DIR="$ROOT_DIR/release/inc"

echo "=== copying public headers to release/inc/ ==="

copy_module()
{
    local module="$1"
    local header="$2"
    local src="$SRC_DIR/public/$module/inc/$header"

    if [ -f "$src" ]
    then
        cp -f "$src" "$DST_DIR/"
        echo "  [$module] $header"
    else
        echo "  [WARN] $src not found, skip"
    fi
}

# --- protocol/ 模块下的头文件 ---
copy_proto_module()
{
    local module="$1"
    local header="$2"
    local src="$SRC_DIR/protocol/$module/inc/$header"

    if [ -f "$src" ]
    then
        cp -f "$src" "$DST_DIR/"
        echo "  [protocol/$module] $header"
    else
        echo "  [WARN] $src not found, skip"
    fi
}

# --- system/ 模块下的头文件 ---
copy_sys_module()
{
    local module="$1"
    local header="$2"
    local src="$SRC_DIR/system/$module/inc/$header"

    if [ -f "$src" ]
    then
        cp -f "$src" "$DST_DIR/"
        echo "  [system/$module] $header"
    else
        echo "  [WARN] $src not found, skip"
    fi
}

# --- system/ 公共头文件 ---
copy_sys_header()
{
    local header="$1"
    local src="$SRC_DIR/system/inc/$header"

    if [ -f "$src" ]
    then
        cp -f "$src" "$DST_DIR/"
        echo "  [system/inc] $header"
    else
        echo "  [WARN] $src not found, skip"
    fi
}

# ================================================================
# 公共库模块（按顺序添加）
# ================================================================
copy_module "liblist"        "list.h"
copy_module "libfunc"        "myFunc.h"
copy_module "liblog"         "myLog.h"
copy_module "libmd5"         "myMd5.h"
copy_module "libcJSON"       "cJSON.h"
copy_module "libmy_xxhash"   "xxhash.h"
copy_module "libtask"        "myTask.h"
copy_module "libxml"         "myXml.h"
copy_module "libxml"         "tinyxml2.h"
copy_module "libcmd"         "myCmd.h"
copy_module "libdatacenter"  "myDatacenter.h"
copy_module "libcomm"       "myComm.h"

# ================================================================
# 协议模块（protocol/）
# ================================================================
copy_proto_module "libmodbus" "myModbus.h"
copy_proto_module "lib60870" "myIec60870.h"
copy_proto_module "libmongoose" "myMongoose.h"
copy_proto_module "libicp67" "myIcp67.h"

# ================================================================
# 系统模块（system/）
# ================================================================
copy_sys_header "mySystem.h"
copy_sys_header "app_modules.h"
copy_sys_header "app_msg_queue.h"

echo "=== done ==="
