#!/usr/bin/env bash
# bd-shim v1
# bd-hooks-version: 0.49.3
#
# Pre-push hook: delegates to bd for issue tracking.
#
# The previous shared push-gate invocation (scripts/pre-push-gate.sh) was
# retired by soc-g2r9. CI (.github/workflows/validate.yml + claude-code-review)
# is the sole authoritative push gate. See:
#   docs/contracts/local-pre-push-gate-retirement.md

set -euo pipefail

HOOK_STDIN_FILE="$(mktemp)"

cleanup() {
    rm -f "$HOOK_STDIN_FILE"
}
trap cleanup EXIT

cat >"$HOOK_STDIN_FILE"

run_without_git_env() {
    local var_name
    local -a env_args=(env)
    while IFS='=' read -r var_name _; do
        [[ "$var_name" == GIT_* ]] || continue
        env_args+=("-u" "$var_name")
    done < <(env)
    "${env_args[@]}" "$@"
}

# --- bd issue tracking (existing behavior) ---
if command -v bd >/dev/null 2>&1; then
    run_without_git_env bd hooks run pre-push "$@" <"$HOOK_STDIN_FILE"
fi
