@echo off
REM Build the Windows installer (Jarvis-Setup-x64.exe) for manual testing.
REM PyInstaller produces dist\Jarvis\, then Inno Setup wraps that into the
REM installer at dist\Jarvis-Setup-x64.exe. The resulting installer is the
REM artefact CI ships, so manual runs of it exercise the same code paths
REM as a real release including install_cuda.ps1 and the VerifyCudaInstall hook.

REM Navigate to project root (use for-loop to resolve .. reliably across shells)
for %%I in ("%~dp0..") do set "PROJECT_ROOT=%%~fI"
cd /d "%PROJECT_ROOT%"

REM Resolve mamba env: prefer this checkout's own, fall back to the main
REM repo's when running from a git worktree (worktrees share one env).
set "MAMBA_ENV=%PROJECT_ROOT%\.mamba_env"
if not exist "%MAMBA_ENV%\python.exe" call :resolve_mamba_from_worktree

if not exist "%MAMBA_ENV%\python.exe" (
    echo [build_installer] ERROR: Mamba environment not found.
    echo                   Looked in: %PROJECT_ROOT%\.mamba_env
    echo                   And the main repo's .mamba_env ^(if this is a git worktree^).
    echo                   Run the setup script first.
    exit /b 1
)

REM ---- Stamp a dev version file so jarvis.get_version() works in the bundle.
echo [build_installer] Stamping dev _version.py...
for /f "delims=" %%i in ('git rev-parse --short=7 HEAD 2^>nul') do set "GIT_SHA=%%i"
if "%GIT_SHA%"=="" set "GIT_SHA=local"
set "DEV_VERSION=dev-%GIT_SHA%"
> "%PROJECT_ROOT%\src\jarvis\_version.py" (
    echo # Auto-generated by scripts/build_installer.bat
    echo VERSION = "%DEV_VERSION%"
    echo RELEASE_CHANNEL = "develop"
)

REM ---- Generate icons (idempotent; cheap to re-run).
echo [build_installer] Generating icons...
"%MAMBA_ENV%\python.exe" src\desktop_app\desktop_assets\generate_icons.py
if errorlevel 1 (
    echo [build_installer] ERROR: icon generation failed
    exit /b 1
)

REM ---- Clean previous build outputs.
echo [build_installer] Cleaning previous builds...
if exist "build" rmdir /s /q build
if exist "dist"  rmdir /s /q dist

REM ---- PyInstaller produces dist\Jarvis\.
echo [build_installer] Running PyInstaller...
"%MAMBA_ENV%\python.exe" -m PyInstaller jarvis_desktop.spec
if not exist "dist\Jarvis\Jarvis.exe" (
    echo [build_installer] ERROR: PyInstaller did not produce dist\Jarvis\Jarvis.exe
    exit /b 1
)

REM ---- Locate ISCC.exe. Try common install paths first, then PATH.
set "ISCC="
if exist "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" set "ISCC=C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
if not defined ISCC if exist "C:\Program Files\Inno Setup 6\ISCC.exe" set "ISCC=C:\Program Files\Inno Setup 6\ISCC.exe"
if not defined ISCC for /f "delims=" %%i in ('where iscc 2^>nul') do set "ISCC=%%i"

if not defined ISCC (
    echo [build_installer] ERROR: ISCC.exe not found.
    echo                   Install Inno Setup 6 from https://jrsoftware.org/isdl.php
    echo                   or run: choco install innosetup -y
    exit /b 1
)

REM ---- Build the installer. /DMyAppVersion is what the .iss file expects.
echo [build_installer] Running Inno Setup with version %DEV_VERSION%...
"%ISCC%" /DMyAppVersion="%DEV_VERSION%" installer\windows\jarvis_setup.iss
if errorlevel 1 (
    echo [build_installer] ERROR: Inno Setup failed
    exit /b 1
)

if not exist "dist\Jarvis-Setup-x64.exe" (
    echo [build_installer] ERROR: Installer was not produced at dist\Jarvis-Setup-x64.exe
    exit /b 1
)

echo.
echo [build_installer] SUCCESS
echo                   Installer:  %PROJECT_ROOT%\dist\Jarvis-Setup-x64.exe
echo                   Frozen app: %PROJECT_ROOT%\dist\Jarvis\Jarvis.exe
echo.
echo [build_installer] To test the CUDA install flow, run the installer with the
echo                   "Download NVIDIA CUDA libraries" task ticked, then check
echo                   "%%LOCALAPPDATA%%\Programs\Jarvis\cuda\install.log".

goto :eof

:resolve_mamba_from_worktree
for /f "usebackq delims=" %%G in (`git -C "%PROJECT_ROOT%" rev-parse --git-common-dir 2^>nul`) do set "GIT_COMMON_DIR=%%G"
if not defined GIT_COMMON_DIR goto :eof
for %%I in ("%GIT_COMMON_DIR%\..") do set "MAIN_REPO=%%~fI"
if exist "%MAIN_REPO%\.mamba_env\python.exe" set "MAMBA_ENV=%MAIN_REPO%\.mamba_env"
goto :eof
