@echo off
chcp 65001 >nul
setlocal

title Server Manager - pack all to build\output

:: 发版前请先打本地测试包验证: scripts\package\pack_local_test.bat 或 python scripts/package/pack_local_test.py

for %%I in ("%~dp0..\..") do set "ROOT=%%~fI"
set "ISCC=C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
set "OUTPUT_DIR=%ROOT%\build\output"

echo(============================================================
echo(         Server Manager - 一键打包
echo(         EXE + Inno Setup 安装包
echo(============================================================
echo(

:: Step 1: PyInstaller 打包 ServerManager.exe（在 src 目录执行）
echo([Step 1/2] PyInstaller 打包 ServerManager.exe ...
echo(
call "%ROOT%\scripts\package\build.bat" nopause
if errorlevel 1 (
    echo(
    echo([失败] PyInstaller 打包未成功
    pause
    exit /b 1
)
echo(

:: Step 2: Inno Setup 编译安装包
echo([Step 2/2] Inno Setup 编译安装包 ...
echo(
if not exist "%ISCC%" goto :missing_iscc

"%ISCC%" "%ROOT%\scripts\package\build_installer.iss"
if errorlevel 1 goto :iscc_failed

:: 将正式产物放在 build\output
if not exist "%OUTPUT_DIR%" mkdir "%OUTPUT_DIR%"
copy /y "%ROOT%\resources\version.json" "%OUTPUT_DIR%\version.json" >nul
if not exist "%OUTPUT_DIR%\patches" mkdir "%OUTPUT_DIR%\patches"
echo(  已复制: version.json -> build\output\version.json
echo(  增量包目录: build\output\patches\

echo(
echo([清理] 删除打包中间产物 ...
python "%ROOT%\scripts\package\clean_artifacts.py"

echo(
echo(============================================================
echo(一键打包完成！
echo(  新发布目录：
echo(    - build\output\ServerManager_Setup.exe        完整安装包
echo(    - build\output\version.json                   版本与更新配置
echo(    - build\output\patches\*.patch                增量补丁（可选）
echo(============================================================
echo(
pause
exit /b 0

:missing_iscc
echo([错误] 未找到 Inno Setup 编译器: %ISCC%
echo(       请安装 Inno Setup 6 或修改本脚本中的 ISCC 路径
pause
exit /b 1

:iscc_failed
echo(
echo([失败] Inno Setup 编译未成功
pause
exit /b 1
