#!/usr/bin/env bash
# Run scripts/deploy.sh after a push to origin/master.
# Git has no native post-push hook; pre-push runs before the push completes.
# If deploy fails the push is aborted, which is safer than deploying after a
# push that might have been rejected anyway.
#
# Install: ln -sf ../../scripts/hooks/pre-push .git/hooks/pre-push

set -e

remote_name="$1"

# Only deploy when pushing to the Gitea origin.
if [ "$remote_name" != "origin" ]; then
    exit 0
fi

should_deploy=0
while read -r _local_ref _local_sha remote_ref _remote_sha; do
    if [ "$remote_ref" = "refs/heads/master" ]; then
        should_deploy=1
    fi
done

if [ "$should_deploy" -ne 1 ]; then
    exit 0
fi

repo_root="$(git rev-parse --show-toplevel)"
exec "$repo_root/scripts/deploy.sh"
