#!/usr/bin/env bash
# Combined-image entrypoint: run co-located Supertonic TTS + the voice bridge.
# Both live in this one container so the bridge reaches TTS on localhost (the
# co-location latency win — Fly process groups run on separate machines).
set -euo pipefail

# Co-located Supertonic TTS, loopback only. The bridge reaches it via SUPERTONIC_URL
# (default http://127.0.0.1:7788). Weights were baked into the image at build time.
supertonic serve --host 127.0.0.1 --port 7788 --model "${SUPERTONIC_MODEL:-supertonic-3}" &
supertonic_pid=$!

# The bridge — Telnyx connects here over wss:// on $PORT (8080).
python bridge.py &
bridge_pid=$!

# If EITHER process exits, tear the container down so Fly restarts it cleanly
# (a half-dead container with only one process up would silently break calls).
wait -n "$supertonic_pid" "$bridge_pid"
exit_code=$?
echo "[entrypoint] a process exited (code=$exit_code) — stopping container for restart" >&2
kill "$supertonic_pid" "$bridge_pid" 2>/dev/null || true
exit "$exit_code"
