#!/usr/bin/env bash
# Runs INSIDE the gitweb container (uid 1000) on git-http-backend pushes.
# Only writes a signal into the bare repo; the host systemd path unit
# (/home/somhairle/git/blog.git/deploy.signal) picks it up and builds.
set -u
ZERO=0000000000000000000000000000000000000000
GITDIR=$(git rev-parse --git-dir 2>/dev/null) || exit 0
while read -r oldrev newrev refname; do
  case "$refname" in
    refs/heads/main)
      if [ "$newrev" != "$ZERO" ]; then
        if printf '%s %s\n' "$newrev" "$(date -u +%FT%TZ)" > "$GITDIR/deploy.signal" 2>/dev/null; then
          echo "blog-deploy: build signal written for $newrev" >&2
        else
          echo "blog-deploy: WARNING could not write deploy.signal" >&2
        fi
      fi
      ;;
  esac
done
exit 0
