@echo off
:: =============================================================================
:: TeslaConsole - Build ^& Package
:: =============================================================================
:: Publishes the console (net48, framework-dependent) into TeslaConsole\App and
:: assembles the installable package (App\ + install.bat) next to it. net48 is
:: in-box on Windows 10/11, so the target control PC needs no runtime install.
::
:: 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

echo.
echo ============================================================
echo  TeslaConsole - Build ^& Package (net48, 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.
echo ============================================================
echo  Package built: %BUILD_DIR%
echo ============================================================
echo.
echo  Next steps:
echo    1. Copy the TeslaConsole\ folder to the control PC
echo    2. Run TeslaConsole\install.bat as Administrator
echo.
pause
