#!/usr/bin/env bash
set -euo pipefail

repo_root="$(git rev-parse --show-toplevel 2>/dev/null || pwd)"
check_script="$repo_root/scripts/check-release-version.sh"

if [[ ! -x "$check_script" ]]; then
    echo "pre-push: release check script not found at '$check_script'" >&2
    exit 1
fi

status=0

while read -r local_ref local_sha remote_ref remote_sha; do
    # No refs being pushed
    if [[ -z "${local_ref:-}" ]]; then
        continue
    fi

    if [[ "$local_ref" == refs/tags/* ]]; then
        if ! "$check_script" "$local_ref"; then
            status=1
        fi
    fi

done

exit $status
