#!/usr/bin/env bash
# Retires stagit now that foxygit is live and serving git.kristoffersson.info.
#
# Run as root:
#   sudo bash /home/mrfox/foxygit/retire-stagit.sh
#
# What it does:
#   1. Removes the post-receive hook (the template used for new repos, plus
#      every existing repo's own copy) that regenerated stagit's static HTML
#      on every push. Push/pull itself is untouched: git doesn't need a
#      post-receive hook to function at all, this hook only ever triggered
#      stagit regeneration.
#   2. Deletes the old static output at /var/www/git (Caddy's
#      git.kristoffersson.info block already points at /var/www/foxygit
#      instead, since deploy.sh ran, so nothing serves this anymore).
#   3. Removes the stagit/stagit-index binaries from /usr/local/bin.
#
# Does NOT touch /home/mrfox/stagit (the source checkout) or anything in it
# -- add-key.sh, setup-server.sh, git-shell-commands/create and the rest of
# the SSH / push-pull tooling are left exactly as they are.

set -euo pipefail

if [ "$(id -u)" -ne 0 ]; then
    echo "run as root: sudo bash $0" >&2
    exit 1
fi

TEMPLATE_HOOK="/var/git/templates/hooks/post-receive"
REPOS_DIR="/var/git/repos"
HTML_DIR="/var/www/git"

echo "==> 1/3  removing post-receive hooks (stagit regeneration trigger)"
if [ -f "$TEMPLATE_HOOK" ]; then
    rm -f "$TEMPLATE_HOOK"
    echo "    removed $TEMPLATE_HOOK (new repos won't get it anymore)"
else
    echo "    $TEMPLATE_HOOK already gone"
fi
if [ -d "$REPOS_DIR" ]; then
    found=0
    while IFS= read -r -d '' hook; do
        rm -f "$hook"
        echo "    removed $hook"
        found=1
    done < <(find "$REPOS_DIR" -maxdepth 3 -path '*/hooks/post-receive' -print0)
    [ "$found" = "0" ] && echo "    no per-repo post-receive hooks found"
else
    echo "    $REPOS_DIR not found, skipping"
fi

echo "==> 2/3  removing old static output: $HTML_DIR"
if [ -d "$HTML_DIR" ]; then
    rm -rf "$HTML_DIR"
    echo "    removed"
else
    echo "    already gone"
fi

echo "==> 3/3  removing stagit binaries"
rm -fv /usr/local/bin/stagit /usr/local/bin/stagit-index

cat <<'EOF'

Done. Stagit is fully retired:
  - no more regeneration on push (hooks removed, template + per-repo copies)
  - old static HTML removed (/var/www/git)
  - stagit/stagit-index binaries removed

/home/mrfox/stagit (source checkout, add-key.sh, setup-server.sh,
git-shell-commands/create) was left untouched -- your SSH/push/pull
tooling still works exactly as before.
EOF
