@echo off
setlocal EnableDelayedExpansion

:: 设置版本信息
set VERSION=2.0.0

:: 设置颜色代码
set "GREEN=[32m"
set "RED=[31m"
set "YELLOW=[33m"
set "RESET=[0m"

:: 设置编译优化标志
set "LDFLAGS=-s -w"
set "BUILDMODE=pie"
set "GCFLAGS=-N -l"

:: 设置 CGO
set CGO_ENABLED=0

:: 显示编译信息
echo %YELLOW%开始构建 version %VERSION%%RESET%
echo %YELLOW%使用优化标志: LDFLAGS=%LDFLAGS%, BUILDMODE=%BUILDMODE%%RESET%
echo %YELLOW%CGO_ENABLED=%CGO_ENABLED%%RESET%

:: 清理旧的构建文件
echo %YELLOW%清理旧的构建文件...%RESET%
if exist "..\bin" (
    rd /s /q "..\bin"
    echo %GREEN%清理完成%RESET%
) else (
    echo %YELLOW%bin 目录不存在，无需清理%RESET%
)

:: 创建输出目录
mkdir "..\bin" 2>nul

:: 定义目标平台数组
set platforms[0].os=windows
set platforms[0].arch=amd64
set platforms[0].ext=.exe
set platforms[0].suffix=

set platforms[1].os=darwin
set platforms[1].arch=amd64
set platforms[1].ext=
set platforms[1].suffix=_intel

set platforms[2].os=darwin
set platforms[2].arch=arm64
set platforms[2].ext=
set platforms[2].suffix=_m1

set platforms[3].os=linux
set platforms[3].arch=amd64
set platforms[3].ext=
set platforms[3].suffix=

:: 设置开始时间
set start_time=%time%

:: 编译所有目标
echo 开始编译所有平台...

for /L %%i in (0,1,3) do (
    set "os=!platforms[%%i].os!"
    set "arch=!platforms[%%i].arch!"
    set "ext=!platforms[%%i].ext!"
    set "suffix=!platforms[%%i].suffix!"
    
    echo.
    echo Building for !os! !arch!...
    
    set GOOS=!os!
    set GOARCH=!arch!
    
    :: 构建输出文件名
    set "outfile=..\bin\cursor_id_modifier_v%VERSION%_!os!_!arch!!suffix!!ext!"
    
    :: 执行构建
    go build -trimpath -buildmode=%BUILDMODE% -ldflags="%LDFLAGS%" -gcflags="%GCFLAGS%" -o "!outfile!" ..\main.go
    
    if !errorlevel! equ 0 (
        echo %GREEN%Build successful: !outfile!%RESET%
    ) else (
        echo %RED%Build failed for !os! !arch!%RESET%
    )
)

:: 计算总耗时
set end_time=%time%
set /a duration = %end_time:~0,2% * 3600 + %end_time:~3,2% * 60 + %end_time:~6,2% - (%start_time:~0,2% * 3600 + %start_time:~3,2% * 60 + %start_time:~6,2%)

echo.
echo %GREEN%所有构建完成! 总耗时: %duration% 秒%RESET%
if exist "..\bin" dir /b "..\bin"

pause
endlocal