@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion

REM 一键打包：PyInstaller -> dist\niumasoftware -> 生成 Inno 脚本 -> (可选) 编译安装包
REM 需要：
REM - Python + PyInstaller（pip install pyinstaller）
REM - Inno Setup（可选，用于自动编译 .iss）

cd /d "%~dp0"

echo.
echo === Clean old build artifacts ===
if exist "build" rmdir /s /q "build"
if exist "dist" rmdir /s /q "dist"

echo.
echo === Build main app (onedir) ===
python -m PyInstaller --noconfirm --onedir --name niumasoftware --windowed ^
  --icon "logo.ico" ^
  --add-data "assets;assets" ^
  --add-data "logo.png;." ^
  main.py
if errorlevel 1 goto :fail

echo.
echo === Build update helper (onefile -> dist\niumasoftware) ===
python -m PyInstaller --noconfirm --onefile --name update_helper --console --distpath dist\niumasoftware ^
  --icon "logo.ico" ^
  update_helper.py
if errorlevel 1 goto :fail

echo.
echo === Generate Inno Setup script with AppVersion ===
python installer\build_installer.py
if errorlevel 1 goto :fail

echo.
echo === (Optional) Compile installer via ISCC.exe ===
set "ISCC="
if exist "%ProgramFiles(x86)%\Inno Setup 6\ISCC.exe" set "ISCC=%ProgramFiles(x86)%\Inno Setup 6\ISCC.exe"
if exist "%ProgramFiles%\Inno Setup 6\ISCC.exe" set "ISCC=%ProgramFiles%\Inno Setup 6\ISCC.exe"

if not defined ISCC (
  echo ISCC.exe not found, skip compiling installer.
  echo You can compile: installer\niumasoftware.generated.iss
  goto :ok
)

echo Using ISCC: "%ISCC%"
"%ISCC%" "installer\niumasoftware.generated.iss"
if errorlevel 1 goto :fail

:ok
echo.
echo === Done ===
echo dist\niumasoftware\niumasoftware.exe
echo dist\niumasoftware\update_helper.exe
echo installer\niumasoftware.generated.iss
echo.
exit /b 0

:fail
echo.
echo Build failed.
exit /b 1

