@echo off
REM ============================================================================
REM DarraEtherCAT C SDK - Windows 构建脚本
REM
REM 用法:
REM   build.bat                    默认构建 (Release, 仅头文件模式)
REM   build.bat Debug              Debug 构建
REM   build.bat Release examples   Release + 构建示例
REM   build.bat clean              清理构建目录
REM   build.bat install            构建并安装
REM   build.bat package            构建并打包
REM
REM 环境变量:
REM   DARRA_DLL_DIR    Darra.Core.dll 所在目录
REM   DARRA_INSTALL    安装目录 (默认 install)
REM
REM Copyright (c) Hangzhou Darra Technology Co., Ltd.
REM ============================================================================

setlocal enabledelayedexpansion

REM ---- 默认参数 ----
set BUILD_TYPE=Release
set BUILD_DIR=build
set INSTALL_DIR=%DARRA_INSTALL%
if "%INSTALL_DIR%"=="" set INSTALL_DIR=install
set BUILD_EXAMPLES=OFF
set HEADER_ONLY=ON
set DO_INSTALL=0
set DO_PACKAGE=0
set DO_CLEAN=0

REM ---- 解析参数 ----
:parse_args
if "%1"=="" goto :done_args

if /i "%1"=="Debug" (
    set BUILD_TYPE=Debug
    shift
    goto :parse_args
)
if /i "%1"=="Release" (
    set BUILD_TYPE=Release
    shift
    goto :parse_args
)
if /i "%1"=="examples" (
    set BUILD_EXAMPLES=ON
    shift
    goto :parse_args
)
if /i "%1"=="linked" (
    set HEADER_ONLY=OFF
    shift
    goto :parse_args
)
if /i "%1"=="install" (
    set DO_INSTALL=1
    shift
    goto :parse_args
)
if /i "%1"=="package" (
    set DO_PACKAGE=1
    shift
    goto :parse_args
)
if /i "%1"=="clean" (
    set DO_CLEAN=1
    shift
    goto :parse_args
)

echo 未知参数: %1
shift
goto :parse_args

:done_args

REM ---- 清理 ----
if %DO_CLEAN%==1 (
    echo [清理] 删除构建目录...
    if exist %BUILD_DIR% rmdir /s /q %BUILD_DIR%
    if exist %INSTALL_DIR% rmdir /s /q %INSTALL_DIR%
    echo [清理] 完成
    exit /b 0
)

REM ---- 检查 CMake ----
where cmake >nul 2>&1
if errorlevel 1 (
    echo 错误: 未找到 cmake, 请安装 CMake 并添加到 PATH
    exit /b 1
)

REM ---- 显示配置 ----
echo ========================================
echo DarraEtherCAT C SDK 构建配置
echo ========================================
echo   构建类型:     %BUILD_TYPE%
echo   头文件模式:   %HEADER_ONLY%
echo   构建示例:     %BUILD_EXAMPLES%
echo   构建目录:     %BUILD_DIR%
echo   安装目录:     %INSTALL_DIR%
echo ========================================
echo.

REM ---- CMake 配置 ----
set CMAKE_ARGS=-DCMAKE_BUILD_TYPE=%BUILD_TYPE%
set CMAKE_ARGS=%CMAKE_ARGS% -DDARRA_HEADER_ONLY=%HEADER_ONLY%
set CMAKE_ARGS=%CMAKE_ARGS% -DDARRA_BUILD_EXAMPLES=%BUILD_EXAMPLES%
set CMAKE_ARGS=%CMAKE_ARGS% -DCMAKE_INSTALL_PREFIX=%INSTALL_DIR%

if defined DARRA_DLL_DIR (
    set CMAKE_ARGS=%CMAKE_ARGS% -DDARRA_DLL_DIR=%DARRA_DLL_DIR%
)

echo [配置] cmake -B %BUILD_DIR% %CMAKE_ARGS%
cmake -B %BUILD_DIR% %CMAKE_ARGS%
if errorlevel 1 (
    echo 错误: CMake 配置失败
    exit /b 1
)

REM ---- 构建 ----
echo.
echo [构建] cmake --build %BUILD_DIR% --config %BUILD_TYPE%
cmake --build %BUILD_DIR% --config %BUILD_TYPE%
if errorlevel 1 (
    echo 错误: 构建失败
    exit /b 1
)

echo.
echo [构建] 成功!

REM ---- 安装 ----
if %DO_INSTALL%==1 (
    echo.
    echo [安装] cmake --install %BUILD_DIR% --config %BUILD_TYPE%
    cmake --install %BUILD_DIR% --config %BUILD_TYPE%
    if errorlevel 1 (
        echo 错误: 安装失败
        exit /b 1
    )
    echo [安装] 已安装到 %INSTALL_DIR%
)

REM ---- 打包 ----
if %DO_PACKAGE%==1 (
    echo.
    echo [打包] cpack --config %BUILD_DIR%/CPackConfig.cmake -B %BUILD_DIR%/packages
    cpack --config %BUILD_DIR%/CPackConfig.cmake -B %BUILD_DIR%/packages
    if errorlevel 1 (
        echo 错误: 打包失败
        exit /b 1
    )
    echo [打包] 输出目录: %BUILD_DIR%/packages
)

echo.
echo ========================================
echo 构建完成!
echo ========================================

endlocal
