@echo off
title Cortana RESTORE - full oracle chain
REM NOTE: the Desktop launcher this file calls (start-qwythos-9b.bat) and the
REM "qwythos" banner below are legacy names. The brain it starts is the
REM Qwen3-14B Q4_K_M from the kiosk guide, alias aux on :8081. Verify with
REM /v1/models, not with file names: stale names linger on purpose here.
echo ============================================
echo   Cortana oracle chain - RESTORE
echo   [1] Brain qwythos :8081  (GPU1, ~60-90 s load)
echo   [2] Kiosk server  :8443 + TLS :8444  (~15 s boot)
echo   [3] Any device    https://<LAN-IP>:8444  (iPhone/iPad/desktop - no chrome flags)
echo   [4] Legacy tablet http://<LAN-IP>:8443  (auto-reconnects)
echo ============================================
echo.

REM ================= 1) BRAIN :8081 =================
%SystemRoot%\System32\netstat.exe -ano | %SystemRoot%\System32\findstr.exe "LISTENING" | %SystemRoot%\System32\findstr.exe /C:":8081 " >nul 2>&1
if errorlevel 1 goto BRAINDOWN
echo [1/3] Brain :8081 already UP - skip.
goto KIOSKCHECK

:BRAINDOWN
%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq llama-server.exe" 2>nul | %SystemRoot%\System32\find.exe /I "llama-server.exe" >nul
if not errorlevel 1 (
  echo [1/3] llama-server.exe present but :8081 not answering yet - waiting...
  goto BRAINWAIT
)
echo [1/3] Starting qwythos brain - new window will open...
start "Qwythos Brain :8081" cmd /c call "%USERPROFILE%\Desktop\start-qwythos-9b.bat"

:BRAINWAIT
set BR=000
set /a N=0
:BRAINLOOP
set /a N+=1
for /f %%C in ('%SystemRoot%\System32\curl.exe -s -m 3 -o nul -w "%%{http_code}" http://127.0.0.1:8081/v1/models') do set BR=%%C
if "%BR%"=="200" goto BRAINOK
if %N% GEQ 120 goto BRAINTO
%SystemRoot%\System32\timeout.exe /t 2 /nobreak >nul
goto BRAINLOOP
:BRAINTO
echo [1/3] WARNING: :8081 not answering after ~4 min.
echo        Check the "Qwythos Brain" window, then run Cortana-STOP and retry.
goto KIOSKCHECK
:BRAINOK
echo [1/3] Brain UP - model API answering on :8081.

:KIOSKCHECK
REM ================= 2) KIOSK :8443 + TLS :8444 (one process) =================
%SystemRoot%\System32\netstat.exe -ano | %SystemRoot%\System32\findstr.exe "LISTENING" | %SystemRoot%\System32\findstr.exe /C:":8443 " >nul 2>&1
if errorlevel 1 goto KIOSKDOWN
echo [2/3] Kiosk :8443 already UP - skip.
goto SUMMARY

:KIOSKDOWN
echo [2/3] Starting kiosk server - new window will open...
start "Cortana Kiosk :8443/:8444" cmd /c call "C:\oracle\start-cortana-server.bat"
set KI=000
set /a M=0
:KIOSKLOOP
set /a M+=1
for /f %%C in ('%SystemRoot%\System32\curl.exe -s -m 3 -o nul -w "%%{http_code}" http://127.0.0.1:8443/') do set KI=%%C
if "%KI%"=="200" goto KIOSKOK
if %M% GEQ 60 goto KIOSKTO
%SystemRoot%\System32\timeout.exe /t 2 /nobreak >nul
goto KIOSKLOOP
:KIOSKTO
echo [2/3] WARNING: :8443 not answering after ~2 min. Check the "Cortana Kiosk" window.
goto SUMMARY
:KIOSKOK
echo [2/3] Kiosk UP - serving on :8443 and TLS :8444.

:SUMMARY
echo.
echo ================= STATUS =================
%SystemRoot%\System32\netstat.exe -ano | %SystemRoot%\System32\findstr.exe "LISTENING" | %SystemRoot%\System32\findstr.exe /C:":8081 " >nul 2>&1 && echo   Brain  :8081 ..... UP || echo   Brain  :8081 ..... DOWN
%SystemRoot%\System32\netstat.exe -ano | %SystemRoot%\System32\findstr.exe "LISTENING" | %SystemRoot%\System32\findstr.exe /C:":8443 " >nul 2>&1 && echo   Kiosk  :8443 ..... UP || echo   Kiosk  :8443 ..... DOWN
set TL=000
for /f %%C in ('%SystemRoot%\System32\curl.exe -sk -m 3 -o nul -w "%%{http_code}" https://127.0.0.1:8444/') do set TL=%%C
if "%TL%"=="200" (echo   TLS    :8444 ..... UP) else (echo   TLS    :8444 ..... DOWN - certs missing? check C:\oracle\certs)
echo.
echo   Clients: any device  https://<LAN-IP>:8444   (accept self-signed cert once per device)
echo            tablet      http://<LAN-IP>:8443    (auto-reconnects in ~3 s, or reload)
if not defined NOPAUSE pause
