@echo off
:: =============================================================================
:: TeslaLauncher — Unified Build / Package Script
:: =============================================================================
:: Publishes TeslaLauncherService.exe and TeslaLauncherAgent.exe as
:: framework-dependent net48 builds and assembles the installable TeslaLauncher\
:: package next to install.bat. The target pod needs .NET Framework 4.8 (built
:: into Windows 10/11) — there is no bundled runtime, so the package is small.
::
:: Requirements:
::   .NET SDK (6.0+) to drive the build  https://dotnet.microsoft.com/download
::   Internet access for NuGet restore (first build only)
::
:: Usage:
::   build.bat              - build both components + package
::   build.bat /service     - build Service only
::   build.bat /agent       - build Agent only
::
:: Output:
::   TeslaLauncher\
::     install.bat
::     Service\TeslaLauncherService.exe
::     Agent\TeslaLauncherAgent.exe
::     [oalinst.exe, dx9201006\]   (if vendored under assets\)
::
:: NOTE: the projects reference ..\Contract\Tesla.Contract.csproj, so they are
:: published IN PLACE (not staged into a temp folder) — the project reference
:: must be able to resolve relative to this directory.
:: =============================================================================

setlocal enabledelayedexpansion

set ROOT=%~dp0
set BUILD_DIR=%ROOT%TeslaLauncher

:: -- Parse arguments ----------------------------------------------------------
set BUILD_SERVICE=1
set BUILD_AGENT=1
set QUIET=0

for %%a in (%*) do (
    if /i "%%~a"=="/service" set BUILD_AGENT=0
    if /i "%%~a"=="/agent"   set BUILD_SERVICE=0
    if /i "%%~a"=="/q"       set QUIET=1
)

echo.
echo ============================================================
echo  Tesla Launcher v0.1 - Build ^& Package (net48, framework-dependent)
echo  Output : %BUILD_DIR%
echo ============================================================
echo.

:: -- Verify .NET SDK ----------------------------------------------------------
where dotnet >nul 2>&1
if errorlevel 1 (
    echo ERROR: dotnet not found.
    echo        Install the .NET 8 SDK from:
    echo        https://dotnet.microsoft.com/download/dotnet/8.0
    if "%QUIET%"=="0" pause
    exit /b 1
)
for /f "tokens=*" %%v in ('dotnet --version 2^>^&1') do set SDK_VER=%%v
echo SDK : %SDK_VER%
echo.

:: -- Clean prior package output ----------------------------------------------
if exist "%BUILD_DIR%\Service" rmdir /s /q "%BUILD_DIR%\Service"
if exist "%BUILD_DIR%\Agent"   rmdir /s /q "%BUILD_DIR%\Agent"

:: -- Build Service ------------------------------------------------------------
if %BUILD_SERVICE%==0 goto :skip_service

echo [1/2] Publishing TeslaLauncherService (Session 0 Windows Service)...
echo.
dotnet publish "%ROOT%TeslaLauncherService.csproj" ^
    -c Release ^
    -o "%BUILD_DIR%\Service"
if errorlevel 1 (
    echo.
    echo ERROR: Service build failed.
    if "%QUIET%"=="0" pause
    exit /b 1
)
echo.
echo   Service : %BUILD_DIR%\Service\TeslaLauncherService.exe
echo.

:skip_service

:: -- Build Agent --------------------------------------------------------------
if %BUILD_AGENT%==0 goto :skip_agent

if %BUILD_SERVICE%==1 (echo [2/2] Publishing TeslaLauncherAgent...) else (echo [1/1] Publishing TeslaLauncherAgent...)
echo.
dotnet publish "%ROOT%TeslaLauncherAgent.csproj" ^
    -c Release ^
    "-p:DefineConstants=WINFORMS" ^
    -o "%BUILD_DIR%\Agent"
if errorlevel 1 (
    echo.
    echo ERROR: Agent build failed.
    if "%QUIET%"=="0" pause
    exit /b 1
)
echo.
echo   Agent : %BUILD_DIR%\Agent\TeslaLauncherAgent.exe
echo.

:skip_agent

:: -- Copy shared assets into the package --------------------------------------
if exist "%ROOT%install.bat"           copy /y "%ROOT%install.bat"        "%BUILD_DIR%\" >nul
if exist "%ROOT%assets\oalinst.exe"    copy /y "%ROOT%assets\oalinst.exe" "%BUILD_DIR%\" >nul
if exist "%ROOT%assets\dx9201006\"     xcopy /y /s /i /q "%ROOT%assets\dx9201006" "%BUILD_DIR%\dx9201006" >nul

:: -- Summary ------------------------------------------------------------------
echo ============================================================
echo  Build complete
echo ============================================================
echo.
if %BUILD_SERVICE%==1 echo   Service  :  %BUILD_DIR%\Service\TeslaLauncherService.exe
if %BUILD_AGENT%==1   echo   Agent    :  %BUILD_DIR%\Agent\TeslaLauncherAgent.exe
echo.
echo  Next steps:
echo    1. Copy the TeslaLauncher\ folder to the pod (or stand-in) PC
echo    2. Run TeslaLauncher\install.bat as Administrator
echo.
if "%QUIET%"=="0" pause
exit /b 0
