#!/bin/bash
# ============================================================================
# DarraEtherCAT C SDK - Linux/macOS 构建脚本
#
# 用法:
#   ./build.sh                     默认构建 (Release, 仅头文件模式)
#   ./build.sh debug               Debug 构建
#   ./build.sh release examples    Release + 构建示例
#   ./build.sh clean               清理构建目录
#   ./build.sh install             构建并安装
#   ./build.sh package             构建并打包
#
# 环境变量:
#   DARRA_DLL_DIR    Darra.Core.so 所在目录
#   DARRA_INSTALL    安装目录 (默认 install)
#   CC               C 编译器 (默认 gcc)
#
# Copyright (c) Hangzhou Darra Technology Co., Ltd.
# ============================================================================

set -e

# ---- 默认参数 ----
BUILD_TYPE="Release"
BUILD_DIR="build"
INSTALL_DIR="${DARRA_INSTALL:-install}"
BUILD_EXAMPLES="OFF"
HEADER_ONLY="ON"
DO_INSTALL=0
DO_PACKAGE=0
DO_CLEAN=0

# ---- 颜色输出 ----
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;33m'
CYAN='\033[0;36m'
NC='\033[0m'

info()  { echo -e "${CYAN}[信息]${NC} $*"; }
ok()    { echo -e "${GREEN}[成功]${NC} $*"; }
warn()  { echo -e "${YELLOW}[警告]${NC} $*"; }
err()   { echo -e "${RED}[错误]${NC} $*"; exit 1; }

# ---- 解析参数 ----
for arg in "$@"; do
    case "$arg" in
        debug|Debug)     BUILD_TYPE="Debug" ;;
        release|Release) BUILD_TYPE="Release" ;;
        examples)        BUILD_EXAMPLES="ON" ;;
        linked)          HEADER_ONLY="OFF" ;;
        install)         DO_INSTALL=1 ;;
        package)         DO_PACKAGE=1 ;;
        clean)           DO_CLEAN=1 ;;
        *)               warn "未知参数: $arg" ;;
    esac
done

# ---- 清理 ----
if [ "$DO_CLEAN" -eq 1 ]; then
    info "删除构建目录..."
    rm -rf "$BUILD_DIR" "$INSTALL_DIR"
    ok "清理完成"
    exit 0
fi

# ---- 检查依赖 ----
command -v cmake >/dev/null 2>&1 || err "未找到 cmake, 请先安装"
command -v make >/dev/null 2>&1 || command -v ninja >/dev/null 2>&1 || err "未找到 make 或 ninja"

# 优先使用 Ninja (如果可用)
GENERATOR=""
if command -v ninja >/dev/null 2>&1; then
    GENERATOR="-G Ninja"
fi

# ---- 显示配置 ----
echo "========================================"
echo "DarraEtherCAT C SDK 构建配置"
echo "========================================"
echo "  构建类型:     $BUILD_TYPE"
echo "  头文件模式:   $HEADER_ONLY"
echo "  构建示例:     $BUILD_EXAMPLES"
echo "  构建目录:     $BUILD_DIR"
echo "  安装目录:     $INSTALL_DIR"
echo "  C 编译器:     ${CC:-$(command -v gcc || command -v cc)}"
echo "========================================"
echo ""

# ---- CMake 配置 ----
CMAKE_ARGS=(
    -DCMAKE_BUILD_TYPE="$BUILD_TYPE"
    -DDARRA_HEADER_ONLY="$HEADER_ONLY"
    -DDARRA_BUILD_EXAMPLES="$BUILD_EXAMPLES"
    -DCMAKE_INSTALL_PREFIX="$INSTALL_DIR"
)

if [ -n "$DARRA_DLL_DIR" ]; then
    CMAKE_ARGS+=(-DDARRA_DLL_DIR="$DARRA_DLL_DIR")
fi

info "cmake -B $BUILD_DIR $GENERATOR ${CMAKE_ARGS[*]}"
cmake -B "$BUILD_DIR" $GENERATOR "${CMAKE_ARGS[@]}"

# ---- 构建 ----
echo ""
info "cmake --build $BUILD_DIR --config $BUILD_TYPE"
cmake --build "$BUILD_DIR" --config "$BUILD_TYPE" -- -j"$(nproc 2>/dev/null || echo 4)"

echo ""
ok "构建成功!"

# ---- 安装 ----
if [ "$DO_INSTALL" -eq 1 ]; then
    echo ""
    info "cmake --install $BUILD_DIR --config $BUILD_TYPE"
    cmake --install "$BUILD_DIR" --config "$BUILD_TYPE"
    ok "已安装到 $INSTALL_DIR"
fi

# ---- 打包 ----
if [ "$DO_PACKAGE" -eq 1 ]; then
    echo ""
    info "cpack --config $BUILD_DIR/CPackConfig.cmake -B $BUILD_DIR/packages"
    cpack --config "$BUILD_DIR/CPackConfig.cmake" -B "$BUILD_DIR/packages"
    ok "输出目录: $BUILD_DIR/packages"
fi

echo ""
echo "========================================"
echo "构建完成!"
echo "========================================"
