#!/usr/bin/env bash
# entrypoint.sh — fetch IndexTTS-2 weights on first run, then exec server.
#
# IndexTTS2() expects checkpoint files pre-staged under model_dir; if
# they're missing, the constructor sys.exit(1)s. Pull them via
# huggingface_hub.snapshot_download on first start. ~5-7 GB; subsequent
# starts skip the download because config.yaml is the gate.

set -e

MODEL_DIR="${INDEX_TTS_MODEL_DIR:-/app/checkpoints}"
mkdir -p "${MODEL_DIR}"

if [ ! -f "${MODEL_DIR}/config.yaml" ]; then
    echo "[index-tts] downloading IndexTTS-2 weights to ${MODEL_DIR} (~5-7 GB, one-time)"
    python3 - <<EOF
from huggingface_hub import snapshot_download
snapshot_download(
    repo_id="IndexTeam/IndexTTS-2",
    local_dir="${MODEL_DIR}",
)
EOF
    echo "[index-tts] download complete"
fi

exec "$@"
