@echo off
REM ============================================================
REM  S-CANVAS distribution build (PyInstaller onedir)
REM
REM  Run:  build.bat
REM
REM  Output:  dist\S-CANVAS\S-CANVAS.exe  (zip the folder to ship)
REM ============================================================

setlocal enableextensions enabledelayedexpansion
chcp 65001 >nul
cd /d "%~dp0"

echo.
echo === [1/4] Check PyInstaller ===
python -m PyInstaller --version >nul 2>&1
if errorlevel 1 (
    echo PyInstaller not installed -- installing now
    python -m pip install --upgrade pip
    python -m pip install pyinstaller
    if errorlevel 1 (
        echo [ERROR] Failed to install PyInstaller. Try:  pip install pyinstaller
        exit /b 1
    )
)
python -m PyInstaller --version

echo.
echo === [2/4] Generate window icon (scanvas_S.ico) ===
python _build_icon.py
if errorlevel 1 (
    echo [WARN] Icon generation failed -- continuing with default PyInstaller icon
)

echo.
echo === [3/4] Clean previous build artifacts ===
if exist build rmdir /s /q build
if exist "dist\S-CANVAS" rmdir /s /q "dist\S-CANVAS"

echo.
echo === [4/4] Run PyInstaller (10-15 min) ===
python -m PyInstaller --clean --noconfirm scanvas_maker.spec
if errorlevel 1 (
    echo.
    echo [ERROR] PyInstaller build failed. See logs above.
    exit /b 1
)

echo.
echo === DONE ===
if exist "dist\S-CANVAS\S-CANVAS.exe" (
    echo Entry point:  dist\S-CANVAS\S-CANVAS.exe
    echo Distribution: zip the dist\S-CANVAS\ folder
    echo User data:    %%LOCALAPPDATA%%\S-CANVAS\
    echo               db, logs, cache live here at runtime.
) else (
    echo [WARN] dist\S-CANVAS\S-CANVAS.exe not found. Check build output.
)

echo.
endlocal
