@echo off
setlocal EnableExtensions EnableDelayedExpansion
title AetherForge — Pack USB
cd /d "%~dp0"
set "ROOT=%CD%"
set "USB=%ROOT%\USB"

echo.
echo  ================================================================
echo       AetherForge  —  Pack Portable USB
echo  ================================================================
echo.
echo  This script builds AetherForge.exe and assembles everything
echo  needed to run the Control Deck from a USB drive.
echo.
echo  Output folder: %USB%\
echo.

:: ----------------------------------------------------------------
:: 0. Check prerequisites
:: ----------------------------------------------------------------
where go >nul 2>nul
if errorlevel 1 (
    echo   ERROR: Go not found on PATH.
    echo   Install from https://go.dev/dl/ or add it to PATH.
    pause
    exit /b 1
)
where npm >nul 2>nul
if errorlevel 1 (
    echo   WARNING: npm not found. Skipping frontend build.
    set "SKIP_FRONTEND=1"
)

:: ----------------------------------------------------------------
:: 1. Build the frontend (if npm available)
:: ----------------------------------------------------------------
if not defined SKIP_FRONTEND (
    echo   [1/4] Building frontend...
    cd "%ROOT%\server\web"
    call npm install --silent
    call npm run build
    if errorlevel 1 (
        echo   ERROR: Frontend build failed.
        cd "%ROOT%"
        pause
        exit /b 1
    )
    cd "%ROOT%"
    echo   [1/4] Frontend built.
) else (
    echo   [1/4] Skipped (npm not found).
)

:: ----------------------------------------------------------------
:: 2. Build AetherForge.exe for the current machine (amd64 windows)
:: ----------------------------------------------------------------
echo   [2/4] Compiling AetherForge.exe...
cd "%ROOT%\server"
set "CGO_ENABLED=0"
set "GOOS=windows"
set "GOARCH=amd64"
go build -ldflags="-s -w" -o "%USB%\AetherForge.exe" .
if errorlevel 1 (
    echo   ERROR: Go build failed.
    cd "%ROOT%"
    pause
    exit /b 1
)
cd "%ROOT%"
echo   [2/4] AetherForge.exe -> USB\AetherForge.exe

:: ----------------------------------------------------------------
:: 3. Copy webroot
:: ----------------------------------------------------------------
echo   [3/4] Copying webroot...
if exist "%ROOT%\server\webroot" (
    if exist "%USB%\webroot" rd /s /q "%USB%\webroot"
    xcopy /e /i /q "%ROOT%\server\webroot" "%USB%\webroot" >nul
    echo   [3/4] webroot copied.
) else if exist "%ROOT%\server\web\dist" (
    if exist "%USB%\webroot" rd /s /q "%USB%\webroot"
    xcopy /e /i /q "%ROOT%\server\web\dist" "%USB%\webroot" >nul
    echo   [3/4] webroot copied from web\dist.
) else (
    echo   [3/4] WARNING: No webroot found. Run 'npm run build' in server\web\ first.
)

:: ----------------------------------------------------------------
:: 4. Copy source trees (needed for Forge to compile agents on the USB)
:: ----------------------------------------------------------------
echo   [4/4] Copying agent + fusion source...

if exist "%USB%\agent"  rd /s /q "%USB%\agent"
if exist "%USB%\fusion" rd /s /q "%USB%\fusion"

:: Copy agent source, excluding test results and build outputs
xcopy /e /i /q /EXCLUDE:"%ROOT%\usb_pack_exclude.txt" "%ROOT%\agent"  "%USB%\agent"  >nul
xcopy /e /i /q /EXCLUDE:"%ROOT%\usb_pack_exclude.txt" "%ROOT%\fusion" "%USB%\fusion" >nul
echo   [4/4] Source trees copied.

:: ----------------------------------------------------------------
:: 5. Ensure data directories exist on USB
:: ----------------------------------------------------------------
if not exist "%USB%\data\builds"     mkdir "%USB%\data\builds"
if not exist "%USB%\data\logs"       mkdir "%USB%\data\logs"
if not exist "%USB%\data\blueprints" mkdir "%USB%\data\blueprints"
if not exist "%USB%\data\preps"      mkdir "%USB%\data\preps"

:: ----------------------------------------------------------------
:: 6. Summary
:: ----------------------------------------------------------------
echo.
echo  ================================================================
echo   USB pack complete!
echo.
echo   Contents of %USB%\:
dir /b "%USB%"
echo.
echo   To use:
echo     1. Copy the entire USB\ folder to a USB drive (or another PC)
echo     2. Double-click LAUNCH.bat
echo     3. Open http://localhost:8989 in a browser
echo.
echo   Toolchain note:
echo     If Go is not installed on the target PC, LAUNCH.bat will
echo     download it automatically on first run and save it to
echo     USB\toolchain\ for future use.
echo  ================================================================
echo.

pause
endlocal
