#!/usr/bin/env bash
# CANONICAL launch invocation for the production ZONOS2 TTS engine (:1920, irv-ml1 3090).
# Captured 2026-07-16 from the LIVE process argv — supersedes the stale
# ~/tts-audition/harness/zonos_server.sh (which wrongly said A6000 / port 1919).
# This is the reference invocation the container CMD encodes; it also works as a
# direct native launcher until the container lands.
#
# Usage:
#   zonos2-server.sh          # foreground
#   zonos2-server.sh --bg     # detached, log to $ROOT/logs/zonos2_server.log
set -euo pipefail

ROOT="${ZONOS_ROOT:-/home/lkraven/tts-audition}"
REPO="${ZONOS_REPO:-$ROOT/models/zonos2}"       # github.com/Zyphra/Zonos2 @ 194c0a3
GPU_3090_UUID="GPU-12c15b3c-0827-0bab-b0cd-31aaa37a8e72"

export CUDA_HOME="${CUDA_HOME:-/usr/local/cuda}"
export PATH="$CUDA_HOME/bin:$PATH"
export CUDA_VISIBLE_DEVICES="${CUDA_VISIBLE_DEVICES:-$GPU_3090_UUID}"  # 3090 (operator: keep off the A6000)

cd "$REPO"
CMD=(uv run python -m zonos2
     --model-path "${ZONOS_MODEL:-Zyphra/ZONOS2}"
     --host 0.0.0.0 --port "${ZONOS_PORT:-1920}"
     --tts-default-voices-dir ./default_voices/
     --cuda-graph-max-bs "${ZONOS_CUDA_GRAPH_MAX_BS:-1}"
     --num-pages "${ZONOS_NUM_PAGES:-16384}"
     --max-running-requests "${ZONOS_MAX_RUNNING_REQUESTS:-2}"
     --memory-ratio "${ZONOS_MEMORY_RATIO:-0.3}")

if [[ "${1:-}" == "--bg" ]]; then
  mkdir -p "$ROOT/logs"
  nohup "${CMD[@]}" > "$ROOT/logs/zonos2_server.log" 2>&1 &
  echo "zonos2 engine started (bg) pid $! -> $ROOT/logs/zonos2_server.log"
else
  exec "${CMD[@]}"
fi
