#!/usr/bin/env bash
# Build with zola and deploy public/ to homelab on push to main.
set -euo pipefail

remote_host="homelab"
remote_path="/var/www/krz.sh"

# pre-push feeds "<local ref> <local sha> <remote ref> <remote sha>" per line.
# Only deploy when main is being pushed (and not deleted).
zero="0000000000000000000000000000000000000000"
deploy=0
while read -r local_ref local_sha remote_ref remote_sha; do
  if [ "$remote_ref" = "refs/heads/main" ] && [ "$local_sha" != "$zero" ]; then
    deploy=1
  fi
done

if [ "$deploy" -eq 0 ]; then
  exit 0
fi

echo "pre-push: building site with zola..."
zola build

echo "pre-push: deploying to ${remote_host}:${remote_path}..."
rsync -az --delete public/ "${remote_host}:${remote_path}/"

echo "pre-push: deploy complete."
