#!/bin/sh
# FE bare repo hook: android_cast.git/hooks/post-receive
# After push to git://10.7.0.10/android_cast, trigger immediate Gitea pull sync on BE.
#
# Install on Gentoo FE (adjust paths):
#   cp fe-android_cast-post-receive.example /var/git/android_cast.git/hooks/post-receive
#   chmod +x /var/git/android_cast.git/hooks/post-receive
#
# Pick ONE trigger method below (uncomment).

# --- A) SSH to BE (recommended if FE has alpine-be key) ---
# GITEA_SYNC_CMD='ssh alpine-be sudo /var/www/localhost/htdocs/apps/app/androidcast_project/android_cast/examples/crash_reporter/backend/scripts/gitea/gitea_sync_mirror.sh android_cast'

# --- B) curl Gitea API on BE loopback via FE→BE nginx/internal URL ---
# Create a long-lived token in Gitea UI (admin, scope: write repository).
# GITEA_SYNC_CMD='curl -fsS -X POST -H "Authorization: token YOUR_TOKEN" http://artc0.intra.raptor.org:3000/api/v1/repos/AndroidCast/android_cast/mirror-sync'

# --- C) curl public apps URL (only if you add a protected internal endpoint) ---
# GITEA_SYNC_CMD='curl -fsS -X POST -H "Authorization: Bearer ..." https://apps.f0xx.org/.../gitea_mirror_sync'

: "${GITEA_SYNC_CMD:=}"
while read -r _old _new _ref; do
  [ "$_new" = "0000000000000000000000000000000000000000" ] && continue
  case "$_ref" in
    refs/heads/next|refs/heads/master) ;;
    *) continue ;;
  esac
  if [ -n "$GITEA_SYNC_CMD" ]; then
    sh -c "$GITEA_SYNC_CMD" </dev/null >/dev/null 2>&1 &
  fi
done
exit 0
