#!/usr/bin/env bash
# Container entrypoint: render config from env, set the VNC password, ensure the
# Piper voice exists, then hand off to supervisord (which runs the desktop,
# bridge, and bot).
set -euo pipefail

# --- Defaults (override via .env / compose) ---
: "${VNC_PASSWORD:=javis123}"
: "${VNC_RESOLUTION:=1920x1080}"
: "${OLLAMA_BASE_URL:=http://ollama:11434}"
: "${OLLAMA_CHAT_MODEL:=qwen3:8b}"
# Auxiliary small-model calls (intent judge, tool router, weather place
# extraction, query decomposition). Default to a small fast model so the big
# chat model only runs for the actual spoken answer — the main per-turn latency
# win once the GPU is in use, since the 2-3 routing/extraction calls per command
# no longer pay the big model's cost. ollama-init pulls this model. Set it equal
# to OLLAMA_CHAT_MODEL to fold everything back onto one resident model.
: "${OLLAMA_INTENT_MODEL:=qwen2.5:3b}"
# Cap chat-model output tokens per turn (worst-case latency guard). Spoken
# answers are 1-2 sentences; 512 is safe headroom above tool-call JSON. 0 = off.
: "${OLLAMA_NUM_PREDICT:=512}"
: "${OLLAMA_EMBED_MODEL:=nomic-embed-text}"
: "${WHISPER_MODEL:=small}"
: "${WHISPER_DEVICE:=cuda}"
: "${WHISPER_COMPUTE_TYPE:=float16}"
: "${JARVIS_DB_PATH:=/data/jarvis.db}"
: "${BRIDGE_HOST:=127.0.0.1}"
: "${BRIDGE_PORT:=8765}"
: "${PIPER_VOICE:=en_GB-alan-medium}"
: "${PIPER_VOICE_DIR:=/opt/piper-voices}"
: "${TTS_PIPER_MODEL_PATH:=${PIPER_VOICE_DIR}/${PIPER_VOICE}.onnx}"

# Audio: shared PulseAudio socket so Chrome (playback) and the selfbot ffmpeg
# (capture of @DEFAULT_MONITOR@) reach the same headless daemon (run-pulse.sh).
: "${XDG_RUNTIME_DIR:=/run/user/0}"
: "${PULSE_SERVER:=unix:${XDG_RUNTIME_DIR}/pulse/native}"

export VNC_RESOLUTION OLLAMA_BASE_URL OLLAMA_CHAT_MODEL OLLAMA_NUM_PREDICT OLLAMA_INTENT_MODEL OLLAMA_EMBED_MODEL \
       WHISPER_MODEL WHISPER_DEVICE WHISPER_COMPUTE_TYPE JARVIS_DB_PATH \
       PIPER_VOICE PIPER_VOICE_DIR TTS_PIPER_MODEL_PATH BRIDGE_HOST BRIDGE_PORT \
       XDG_RUNTIME_DIR PULSE_SERVER

mkdir -p /data /app/config "$(dirname "$JARVIS_DB_PATH")"

# --- VNC password file ---
mkdir -p /root/.vnc
echo "$VNC_PASSWORD" | tigervncpasswd -f > /root/.vnc/passwd
chmod 600 /root/.vnc/passwd

# --- Render jarvis brain config from template ---
envsubst < /app/docker/jarvis-config.template.json > /app/config/jarvis.json
export JARVIS_CONFIG_PATH=/app/config/jarvis.json
# Merge persistent settings from the settings UI (on the /data volume) on top of
# the env-rendered config, so changes survive container recreate.
if [ -f /data/jarvis-settings.json ]; then
  python3 - <<'PY' || true
import json, os
try:
    base = json.load(open("/app/config/jarvis.json"))
    ov = json.load(open("/data/jarvis-settings.json"))
    if isinstance(base, dict) and isinstance(ov, dict):
        base.update(ov)
        # A stale persisted tts_engine from an earlier voice (melo/xtts, no
        # longer built into the image) would override the configured engine and
        # leave the bot silent. Reset those to the env-configured engine.
        if base.get("tts_engine") in ("melo", "xtts"):
            base["tts_engine"] = os.environ.get("TTS_ENGINE", "edge")
            print(f"[entrypoint] reset stale tts_engine -> {base['tts_engine']}")
        json.dump(base, open("/app/config/jarvis.json", "w"), ensure_ascii=False, indent=2)
        print("[entrypoint] merged persistent settings overrides")
except Exception as e:
    print(f"[entrypoint] settings merge skipped: {e}")
PY
fi

# --- Ensure the Piper voice exists (best effort) ---
bash /app/docker/download-piper.sh || echo "[entrypoint] piper download failed; TTS may be unavailable"

# --- Gemini OAuth login check (GEMINI_AUTH=oauth real-time search) ---
# The browser-only role never runs the reply engine / web search, so skip the
# check there. Otherwise warn (don't fail) when oauth is selected but no login
# has been seeded into the mounted ~/.gemini, since the path silently degrades
# to the DDG/Brave cascade.
if [ "${JARVIS_ROLE:-full}" != "browser" ] \
   && [ "${GEMINI_AUTH:-oauth}" = "oauth" ] \
   && [ ! -f /root/.gemini/oauth_creds.json ]; then
  echo "[entrypoint] 🔑 GEMINI_AUTH=oauth but no Gemini login at /root/.gemini/oauth_creds.json"
  echo "[entrypoint]    Real-time search will fall back to DDG/Brave until you seed the login."
  echo "[entrypoint]    Seed it: copy a logged-in ~/.gemini into the host's gemini-oauth dir"
  echo "[entrypoint]    (default ./docker/gemini-oauth, or set GEMINI_OAUTH_DIR). See docs/DEPLOY.md."
fi

echo "[entrypoint] display=$DISPLAY ollama=$OLLAMA_BASE_URL whisper=$WHISPER_MODEL/$WHISPER_DEVICE"
exec supervisord -c /app/docker/supervisord.conf
