@echo off
setlocal
set "CONFIG=%~1"
if "%CONFIG%"=="" set "CONFIG=%~dp0gdr8050l_auth_v252_stateful_real_eject_iscsi_config.json"
set "MEDIA=%~2"
set "ADVERTISE=%~3"
set "PORT=%~4"
if "%MEDIA%"=="" set "MEDIA=D:\vscode-workspace\xbox-dvd-tools-v2\disc_dumps\Red Faction II[TQ00501A].iso"
if "%ADVERTISE%"=="" set "ADVERTISE=192.168.0.8"
if "%PORT%"=="" set "PORT=3260"
if not exist "%CONFIG%" (
  echo [FATAL] dynamic media config not found: %CONFIG%
  exit /b 2
)
if not exist "%MEDIA%" (
  echo [FATAL] media ISO not found: %MEDIA%
  exit /b 3
)
set "LOGBASE=%~n0_live"
set "OUTJSONL=%LOGBASE%.jsonl"
set "RESPDIR=%LOGBASE%_responses"
if exist "%OUTJSONL%" del /f /q "%OUTJSONL%" >nul 2>nul
if exist "%RESPDIR%" rmdir /s /q "%RESPDIR%" >nul 2>nul
mkdir "%RESPDIR%" >nul 2>nul
echo [V252] Starting GDR-8050L stateful iSCSI target with Redump-style media-profile-aware READ mapping + media preflight + v252 firmware read-window path probe collection + exact-firmware locator.
echo [V252] Config: %CONFIG%
echo [V252] Media : %MEDIA%
echo [V252] Locked view  : legacy direct, or Redump video L0/L1 split from .media.json
echo [V252] Unlocked view: legacy fallback +306080, or Redump game_output_start_lba from .media.json (198144 for current validated XGD1 profile)
echo [V252] Eject policy : real (tray open/media absent until load or target restart)
echo [V252] Logs  : %OUTJSONL%
echo [V252] Running media-profile preflight...
python "%~dp0hlds_gdr_media_profile_preflight.py" ^
  --config "%CONFIG%" ^
  --iso "%MEDIA%" ^
  --strict-image ^
  --require-media-profile ^
  --expect-redump ^
  --json "%LOGBASE%_media_profile_preflight_report.json" ^
  --md "%LOGBASE%_media_profile_preflight_report.md"
if errorlevel 1 (
  echo [FATAL] media-profile preflight failed. Refusing to start iSCSI target.
  exit /b 10
)
python "%~dp0gdr_host_iscsi_pdu_bridge.py" ^
  --dynamic-media-config "%CONFIG%" ^
  --dynamic-media-media "%MEDIA%" ^
  --dynamic-media-lba-delta "306080" ^
  --dynamic-media-view-mode "stateful_xbox" ^
  --dynamic-media-eject-policy "real" ^
  --serve ^
  --host "0.0.0.0" ^
  --port "%PORT%" ^
  --advertise-host "%ADVERTISE%" ^
  --advertise-port "%PORT%" ^
  --max-clients "0" ^
  --accept-timeout "0.5" ^
  --target-name "iqn.2026-06.local.mn103s:gdr8050l" ^
  --out "%OUTJSONL%" ^
  --write-response-dir "%RESPDIR%" ^
  --ready-file "%LOGBASE%_ready.json" ^
  --compact-jsonl-data
endlocal
