@echo off

echo Enabling mobile hotspot
powershell -NoProfile -ExecutionPolicy Bypass -File ".\mhotspot.ps1"

echo Starting docker desktop
start /MIN "DockerDesktop" "C:\Program Files\Docker\Docker\Docker Desktop.exe"

:waitForDocker
tasklist | find /i "Docker Desktop.exe" >nul
if errorlevel 1 (
    timeout /t 1 >nul
    goto waitForDocker
)

:waitForDaemon
docker info >nul 2>&1
if errorlevel 1 (
    timeout /t 1 >nul
    goto waitForDaemon
)

echo Starting docker compose
docker compose up -d --remove-orphans

timeout /t 3 >nul

{{OBS_INSTANCES}}

powershell -Command ^
"try { ^
    $response = Invoke-WebRequest -Uri http://{{GETENDPOINTS_HOSTNAME}}/api/health; ^
    if ($response.StatusCode -eq 200) { ^
        exit 0 ^
    } else { ^
        exit 1 ^
    } ^
} catch { ^
    exit 1 ^
}"

if errorlevel 1 (
    echo Starting GetEndpoints
    cd ".\getendpoints"
    start cmd /k run.bat
)

:waitForGetEndpoints
powershell -Command ^
"try { ^
    $response = Invoke-WebRequest -Uri http://{{GETENDPOINTS_HOSTNAME}}/api/health; ^
    if ($response.StatusCode -eq 200) { ^
        exit 0 ^
    } else { ^
        exit 1 ^
    } ^
} catch { ^
    exit 1 ^
}"

if errorlevel 1 (
    timeout /t 1 >nul
    goto waitForGetEndpoints
)

{{POST_REQUESTS}}

pause