@echo off
setlocal enabledelayedexpansion
rem ============================================================
rem  OpenCADStudio <-> WorkBuddy bridge launcher (Windows)
rem
rem  Double-click to start HTTP(8080) + WebSocket relay(9090)
rem  OUTSIDE the agent sandbox, so it survives conversation turns.
rem
rem  Optional: start_services.bat "E:\path\to\OpenCADStudio"
rem  Optional: set OCS_PYTHON=C:\path\to\python.exe
rem ============================================================

set "ROOT=%~1"
if "%ROOT%"=="" set "ROOT=%~dp0.."

set "PY=%OCS_PYTHON%"
if not defined PY (
  for %%P in (python.exe) do if not defined PY set "PY=%%~$PATH:P"
)
if not defined PY (
  set "PY=%USERPROFILE%\.workbuddy\binaries\python\versions\3.13.12\python.exe"
)

if not exist "%PY%" (
  echo [ERROR] Python not found.
  echo         Set OCS_PYTHON to your python.exe and retry.
  pause
  exit /b 1
)

echo ============================================
echo  OpenCADStudio bridge
echo    root   = %ROOT%
echo    python = %PY%
echo ============================================
echo.

start "OCS-HTTP"  /min "%PY%" -u "%~dp0serve_app.py" --root "%ROOT%" --port 8080
start "OCS-Relay" /min "%PY%" -u "%~dp0ws_relay.py" 9090

timeout /t 3 /nobreak >nul
"%PY%" "%~dp0services.py" status --root "%ROOT%"

echo.
echo Open the bridge page in WorkBuddy side panel:
echo    http://127.0.0.1:8080/agent/host.html
echo.
pause
