@echo off
:: =============================================================================
:: TeslaConsole - Build ^& Package
:: =============================================================================
:: Publishes the console (net40, framework-dependent - XP11: runs on XP SP3
:: through Windows 11) into TeslaConsole\App and assembles the installable
:: package (App\ + install.bat) next to it. The 4.x runtime is in-box on
:: Windows 10/11; an XP-era control PC needs dotNetFx40_Full_x86_x64.exe once.
::
:: Requirements: .NET SDK (6.0+) to drive the build; internet for first restore.
::
:: Output:
::   TeslaConsole\
::     install.bat
::     App\  (TeslaConsole.exe + dependencies + RedPlanet\ content)
:: =============================================================================

setlocal

set ROOT=%~dp0
:: Package under dist\ so it never collides with the TeslaConsole\ SOURCE folder.
set BUILD_DIR=%ROOT%dist\TeslaConsole
set APP_OUT=%BUILD_DIR%\App
set ZIP=%ROOT%dist\TeslaConsole-pkg.zip

echo.
echo ============================================================
echo  TeslaConsole - Build ^& Package (net40, framework-dependent)
echo  Output : %BUILD_DIR%
echo ============================================================
echo.

where dotnet >nul 2>&1
if errorlevel 1 (
    echo ERROR: dotnet not found. Install the .NET SDK from https://dotnet.microsoft.com/download
    pause & exit /b 1
)

:: Don't publish over a running instance.
taskkill /F /IM TeslaConsole.exe >nul 2>&1

echo Publishing TeslaConsole...
if exist "%APP_OUT%" rmdir /s /q "%APP_OUT%"
dotnet publish "%ROOT%TeslaConsole.csproj" -c Release -o "%APP_OUT%"
if errorlevel 1 (
    echo.
    echo ERROR: Build failed.
    pause & exit /b 1
)

if exist "%ROOT%install.bat" copy /y "%ROOT%install.bat" "%BUILD_DIR%\" >nul

echo Zipping package...
if exist "%ZIP%" del /q "%ZIP%"
powershell -NoProfile -Command "Compress-Archive -Path '%BUILD_DIR%' -DestinationPath '%ZIP%' -Force"

echo.
echo ============================================================
echo  Package built: %BUILD_DIR%
echo  Zip          : %ZIP%
echo ============================================================
echo.
echo  Next steps:
echo    1. Copy TeslaConsole-pkg.zip to the control PC and extract it
echo    2. Run TeslaConsole\install.bat as Administrator
echo.
pause
