#!/usr/bin/env bash
set -euo pipefail
REMOTE_HOST="root@143.198.27.163"
REMOTE_FILE="/tmp/gemma4-llamacpp/gemma-4-e4b-it-Q4_K_M.gguf"
MODEL_FILE="$HOME/models/gemma4-llamacpp/gemma-4-e4b-it-Q4_K_M.gguf"

echo "Waiting for fully-synced local Gemma4 GGUF at: $MODEL_FILE"
while true; do
  REMOTE_SIZE=$(ssh "$REMOTE_HOST" "python3 - <<'PY'
from pathlib import Path
p=Path('$REMOTE_FILE')
print(p.stat().st_size if p.exists() else 0)
PY" 2>/dev/null || echo 0)
  LOCAL_SIZE=$(python3 - <<'PY'
from pathlib import Path
p=Path.home()/'models'/'gemma4-llamacpp'/'gemma-4-e4b-it-Q4_K_M.gguf'
print(p.stat().st_size if p.exists() else 0)
PY
)
  echo "[$(date '+%Y-%m-%d %H:%M:%S')] local=$LOCAL_SIZE remote=$REMOTE_SIZE"
  if [ "${REMOTE_SIZE:-0}" -gt 0 ] && [ "$LOCAL_SIZE" = "$REMOTE_SIZE" ]; then
    break
  fi
  sleep 15
done
exec bash "$HOME/.timmy/scripts/run_gemma4_llamacpp_server.sh"
