@echo off
chcp 65001 >nul
setlocal

title Server Manager - 本地测试包（不发版、不覆盖 build\output）

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

echo ============================================================
echo   Server Manager - 本地测试包
echo   build\output_local\ServerManager_Setup_LOCAL.exe
echo   不修改版本号，不覆盖正式目录 build\output\
echo   测试通过后请再执行 scripts\package\release.bat 或 scripts\package\pack_all.bat
echo ============================================================
echo(

echo([Step 1/3] PyInstaller 打包 main + mini_updater ...
call "%ROOT%\scripts\package\build.bat" nopause
if errorlevel 1 (
    echo(
    echo([失败] PyInstaller 打包未成功
    echo(若在本终端乱码/解析失败，请改用 CMD 窗口双击本脚本，或执行: python scripts/package/pack_local_test.py
    pause
    exit /b 1
)
echo(

echo([Step 2/3] Inno Setup 编译【本地测试】安装包 ^(LOCAL_TEST^) ...
if not exist "%ISCC%" goto :missing_iscc
"%ISCC%" /DLOCAL_TEST=1 "%ROOT%\scripts\package\build_installer.iss"
if errorlevel 1 goto :iscc_failed

echo(
echo([Step 3/3] 复制 version.json 到 build\output_local ...
if not exist "%OUTPUT_LOCAL_DIR%" mkdir "%OUTPUT_LOCAL_DIR%"
copy /y "%ROOT%\resources\version.json" "%OUTPUT_LOCAL_DIR%\version.json" >nul

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

echo.
echo ============================================================
echo 本地测试包已生成，请安装验证
echo   %OUTPUT_LOCAL_DIR%\ServerManager_Setup_LOCAL.exe
echo 验证通过后，再执行正式发版
echo ============================================================
echo.
pause
exit /b 0

:missing_iscc
echo([错误] 未找到 Inno Setup: %ISCC%
pause
exit /b 1

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