@echo off
setlocal enabledelayedexpansion

:: Check if Python is installed
python --version >nul 2>&1
if %errorlevel% neq 0 (
    echo Python is not installed. Please install Python first.
    pause
    exit /b 1
)

:: Install PyInstaller if not already installed
python -m pip show pyinstaller >nul 2>&1
if %errorlevel% neq 0 (
    echo Installing PyInstaller...
    python -m pip install pyinstaller
    if %errorlevel% neq 0 (
        echo Failed to install PyInstaller.
        pause
        exit /b 1
    )
)

:: Build the executable
python build_executable.py
if %errorlevel% neq 0 (
    echo Failed to build the executable.
    pause
    exit /b 1
)

:: Find Inno Setup installation path
set ISCC_PATH=

:: Check if ISCC_PATH is already set in environment variables
if defined ISCC_PATH (
    if exist "%ISCC_PATH%" (
        goto :found_iscc
    )
)

:: Check common installation paths
:: Each path must be checked individually to handle spaces correctly
set "PATH1=C:\Program Files (x86)\Inno Setup 6\ISCC.exe"
set "PATH2=C:\Program Files\Inno Setup 6\ISCC.exe"
set "PATH3=C:\Program Files (x86)\Inno Setup 5\ISCC.exe"
set "PATH4=C:\Program Files\Inno Setup 5\ISCC.exe"
set "PATH5=C:\Inno Setup 6\ISCC.exe"
set "PATH6=C:\Inno Setup 5\ISCC.exe"

if exist "%PATH1%" (
    set "ISCC_PATH=%PATH1%"
    goto :found_iscc
)
if exist "%PATH2%" (
    set "ISCC_PATH=%PATH2%"
    goto :found_iscc
)
if exist "%PATH3%" (
    set "ISCC_PATH=%PATH3%"
    goto :found_iscc
)
if exist "%PATH4%" (
    set "ISCC_PATH=%PATH4%"
    goto :found_iscc
)
if exist "%PATH5%" (
    set "ISCC_PATH=%PATH5%"
    goto :found_iscc
)
if exist "%PATH6%" (
    set "ISCC_PATH=%PATH6%"
    goto :found_iscc
)

:: Check for any Inno Setup folder in Program Files directories
for /d %%d in ("C:\Program Files (x86)\Inno Setup *") do (
    if exist "%%d\ISCC.exe" (
        set "ISCC_PATH=%%d\ISCC.exe"
        goto :found_iscc
    )
)

for /d %%d in ("C:\Program Files\Inno Setup *") do (
    if exist "%%d\ISCC.exe" (
        set "ISCC_PATH=%%d\ISCC.exe"
        goto :found_iscc
    )
)

:: Try to find ISCC.exe in system PATH
where ISCC.exe >nul 2>&1
if %errorlevel% equ 0 (
    set "ISCC_PATH=ISCC.exe"
    goto :found_iscc
)

:found_iscc

:: Debug: Show what path was found
if defined ISCC_PATH (
    echo Debug: Found ISCC.exe at "%ISCC_PATH%"
)

if "%ISCC_PATH%"=="" (
    echo Inno Setup is not installed. Please install Inno Setup first.
    echo.
    echo The script looked for ISCC.exe in:
    echo - Environment variables
    echo - Common installation paths
    echo - Program Files directories
    echo - System PATH
    echo.
    echo If Inno Setup is installed in a custom location, please:
    echo 1. Set the ISCC_PATH environment variable to point to ISCC.exe
    echo 2. Or add the Inno Setup directory to your system PATH
    pause
    exit /b 1
)

:: Create the installer
"%ISCC_PATH%" create_installer.iss
if %errorlevel% neq 0 (
    echo Failed to create the installer.
    pause
    exit /b 1
)

echo Installer created successfully!
pause
