#!/bin/bash
set -e

REPO=/opt/bincio-auth-repo.git
DEPLOY=/opt/bincio-auth

while read oldrev newrev refname; do
  echo "--- Checking out $refname ---"
  git --work-tree=$DEPLOY --git-dir=$REPO checkout -f $newrev

  echo "--- Syncing Python deps ---"
  cd $DEPLOY
  ~/.local/bin/uv sync

  echo "--- Syncing JS deps ---"
  cd $DEPLOY/site
  npm install --silent

  echo "--- Building site (bincio.org) ---"
  PUBLIC_ACTIVITY_URL=https://activity.bincio.org \
  PUBLIC_WIKI_URL=https://wiki.bincio.org \
  PUBLIC_PLANNER_URL=https://planner.bincio.org \
  npm run build

  rsync -a --delete $DEPLOY/site/dist/ /var/www/bincio/

  echo "--- Restarting bincio-auth ---"
  systemctl restart bincio-auth || echo "WARNING: bincio-auth restart failed"

  echo "--- Done ---"
done
