@echo off
setlocal
cd /d "%~dp0\..\.."
set "LAUNCHER_LOG=tools\oem-batch-builder\launcher.log"
set "ELECTRON_SOURCE=node_modules\electron\dist\electron.exe"
set "TOOL_EXE=node_modules\electron\dist\OEMBatchBuilderTool.exe"
set "TOOL_APP_DIR=tools\oem-batch-builder"
echo [%date% %time%] launcher cwd=%cd%>> "%LAUNCHER_LOG%"
if exist "tools\oem-batch-builder\kill-stale-build-processes.ps1" (
  powershell.exe -NoProfile -ExecutionPolicy Bypass -File "tools\oem-batch-builder\kill-stale-build-processes.ps1" -ProjectRoot "%cd%" >> "%LAUNCHER_LOG%" 2>&1
)
if not exist "%ELECTRON_SOURCE%" (
  echo [%date% %time%] Electron not found. Please run npm install first.>> "%LAUNCHER_LOG%"
  echo Electron not found. Please run npm install first.
  pause
  exit /b 1
)
if not exist "%TOOL_EXE%" copy /Y "%ELECTRON_SOURCE%" "%TOOL_EXE%" >nul
if not exist "%TOOL_APP_DIR%\package.json" (
  echo [%date% %time%] OEM tool package not found: %TOOL_APP_DIR%\package.json>> "%LAUNCHER_LOG%"
  echo OEM tool package not found: %TOOL_APP_DIR%\package.json
  pause
  exit /b 1
)
echo [%date% %time%] starting OEM batch builder>> "%LAUNCHER_LOG%"
"%TOOL_EXE%" "%TOOL_APP_DIR%" >> "%LAUNCHER_LOG%" 2>&1
echo [%date% %time%] OEM batch builder exited code=%ERRORLEVEL%>> "%LAUNCHER_LOG%"
