@echo off
set "SETUP=X:\sources\setup.exe"
if not exist "%SETUP%" set "SETUP=X:\setup.exe"
REM WinPE entry point (launched via Setup\CmdLine). SM_UNATTENDED=1 -> skip the UI and
REM launch Setup with the default answer file (used by CI / non-interactive builds).
if "%SM_UNATTENDED%"=="1" (
  start /wait "%SETUP%" /unattend:X:\autounattend.xml
  exit /b 0
)
REM Initialise WinPE (a Setup\CmdLine launch can bypass the normal startnet/wpeinit).
wpeinit
echo ============================================
echo   SilverMetal pre-config collector
echo ============================================
del /f /q X:\sm\collector-error.txt 2>nul
powershell -NoProfile -ExecutionPolicy Bypass -File X:\sm\Collector.ps1
set RC=%errorlevel%
if %RC% GEQ 1 (
  echo.
  echo   Collector exited with code %RC% -- falling back to default unattended install.
  if exist X:\sm\collector-error.txt type X:\sm\collector-error.txt
  echo   (pausing ~25s so this is readable on the console)
  ping -n 26 127.0.0.1 >nul
  start /wait "%SETUP%" /unattend:X:\autounattend.xml
)
exit /b 0
