#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
SWARM_DIR="$(cd "$ROOT_DIR/../swarm" && pwd)"

cd "$ROOT_DIR"

if [[ -z "${GEMINI_API_KEY:-}" ]]; then
  echo "== Loading GEMINI_API_KEY from swarm keyvault =="
  GEMINI_API_KEY="$(cd "$SWARM_DIR" && bun run keys get GEMINI_API_KEY | grep -E '^(AIza|AIza[[:alnum:]_-]+)$' | tail -n 1)"
  export GEMINI_API_KEY
fi

OBS_WS_CONFIG="/home/nandi/.config/obs-studio/plugin_config/obs-websocket/config.json"
if [[ -z "${OBS_WS_PASSWORD:-}" && -f "$OBS_WS_CONFIG" ]]; then
  echo "== Loading OBS websocket password from OBS config =="
  OBS_WS_PASSWORD="$(python3 - <<'PY'
import json
path = '/home/nandi/.config/obs-studio/plugin_config/obs-websocket/config.json'
with open(path, 'r', encoding='utf-8') as f:
    data = json.load(f)
print(data.get('server_password', ''))
PY
)"
  export OBS_WS_PASSWORD
fi

echo "== Live stack preflight =="
bun run scripts/check-live-stack.ts

echo
echo "== Starting live loop =="
echo "This will subscribe to Jetstream chat and attempt Gemini/OpenVT/OBS integration using current env vars."
echo

exec bun run src/index.ts --live
