#!/usr/bin/env bash
# Push current workspace to hermes Gitea (backup / deployment source)
# Usage: bash scripts/push-to-hermes.sh
#
# The hermes remote uses an API token embedded in the URL.
# Token is stored in /root/.gitea-replit-token on the VPS.
# Re-fetch and re-add the remote if this is a fresh Replit session.

set -e
HERMES_IP="143.198.27.163"
GITEA_PORT="3000"
GITEA_USER="admin"
GITEA_REPO="timmy-tower"
SSH_KEY="${HOME}/.ssh/id_ed25519"

# Fetch token from VPS each run (avoids storing token in git)
echo "[hermes] Fetching push token from VPS..."
TOKEN=$(ssh -o StrictHostKeyChecking=no -i "$SSH_KEY" "root@$HERMES_IP" \
  "cat /root/.gitea-replit-token")

REMOTE_URL="http://${GITEA_USER}:${TOKEN}@${HERMES_IP}:${GITEA_PORT}/${GITEA_USER}/${GITEA_REPO}.git"

# Add/update remote
git remote remove hermes 2>/dev/null || true
git remote add hermes "$REMOTE_URL"

# Push all branches and tags
echo "[hermes] Pushing all branches..."
git push hermes --all --no-verify
git push hermes --tags --no-verify

echo "[hermes] Done. Gitea UI: http://${HERMES_IP}:${GITEA_PORT}/${GITEA_USER}/${GITEA_REPO}"
