#!/usr/bin/env bash
# AI-PHONE LIVE — pull the latest code from Gitea into this directory.
# Runtime assets (.env, DEVOPSAPI/.env, models/, chroma_db/, recordings/,
# cloned_voices.json) are git-ignored and are preserved across pulls.
set -euo pipefail
cd "$(dirname "$(readlink -f "$0")")"

echo "[update] current: $(git log -1 --oneline)"
echo "[update] fetching from Gitea..."
git fetch --prune origin

# fast-forward only; fails loudly if the deploy dir has diverging local commits
if git merge-base --is-ancestor HEAD origin/main 2>/dev/null; then
    git pull --ff-only origin main
    echo "[update] now at: $(git log -1 --oneline)"
    echo "[update] done. Apply with:  ./stop.sh && ./start.sh"
else
    echo "[update] ERROR: local branch has diverged from origin/main."
    echo "          This deploy dir should never have local commits."
    echo "          Inspect with 'git status' before forcing anything."
    exit 1
fi
