#!/usr/bin/env sh
# --- BEGIN CONDUIT LEFTHOOK GATE ---
# Lefthook must run before Beads so failures stop before Beads exports/stages state.
run_lefthook_pre_commit() {
  if [ "$LEFTHOOK" = "0" ] || [ "$LEFTHOOK" = "false" ]; then
    return 0
  fi

  # Run the gate on mise.toml's pinned toolchain. A shell with a stale Node on
  # PATH otherwise fails it misleadingly: pnpm only warns about the engine, and
  # Node 24's node:sqlite serves stale SELECT * columns after a migration.
  if command -v mise >/dev/null 2>&1; then
    eval "$(mise env -s bash)"
  fi

  if [ -n "$LEFTHOOK_BIN" ]; then
    "$LEFTHOOK_BIN" run pre-commit "$@"
  elif command -v lefthook >/dev/null 2>&1; then
    lefthook run pre-commit "$@"
  elif [ -x "node_modules/.bin/lefthook" ]; then
    node_modules/.bin/lefthook run pre-commit "$@"
  elif command -v pnpm >/dev/null 2>&1; then
    pnpm exec lefthook run pre-commit "$@"
  else
    echo >&2 "lefthook: not found; run pnpm install"
    return 127
  fi
}

run_lefthook_pre_commit "$@"
_lefthook_exit=$?
if [ "$_lefthook_exit" -ne 0 ]; then
  exit "$_lefthook_exit"
fi
# --- END CONDUIT LEFTHOOK GATE ---
# --- BEGIN BEADS INTEGRATION v1.3.1 ---
# This section is managed by beads. Do not remove these markers.
if command -v bd >/dev/null 2>&1; then
  export BD_GIT_HOOK=1
  _bd_timeout=${BEADS_HOOK_TIMEOUT:-300}
  case "$_bd_timeout" in
    *[!0-9]*|'') _bd_timeout_invalid=1 ;;
    *[1-9]*) _bd_timeout_invalid=0 ;;
    *) _bd_timeout_invalid=1 ;;
  esac
  if [ "$_bd_timeout_invalid" -eq 1 ]; then
    echo >&2 "beads: invalid BEADS_HOOK_TIMEOUT; using 300 seconds"
    _bd_timeout=300
  fi
  _bd_timeout_backend=none
  _bd_timeout_command=
  for _bd_timeout_candidate in timeout gtimeout; do
    if command -v "$_bd_timeout_candidate" >/dev/null 2>&1; then
      if _bd_timeout_version="$("$_bd_timeout_candidate" --version 2>/dev/null)"; then
        case "$_bd_timeout_version" in
          "timeout (GNU coreutils) "*) _bd_timeout_command=$_bd_timeout_candidate; break ;;
        esac
      fi
    fi
  done
  if [ -n "$_bd_timeout_command" ]; then
    _bd_timeout_backend=coreutils
    if "$_bd_timeout_command" -- "$_bd_timeout" bd hooks run pre-commit "$@"; then
      _bd_exit=0
    else
      _bd_exit=$?
    fi
  elif command -v perl >/dev/null 2>&1; then
    _bd_timeout_backend=perl
    if perl -e 'alarm shift; exec @ARGV' -- "$_bd_timeout" bd hooks run pre-commit "$@"; then
      _bd_exit=0
    else
      _bd_exit=$?
    fi
  else
    echo >&2 "beads: hook 'pre-commit' running without timeout; install coreutils or perl to enable BEADS_HOOK_TIMEOUT"
    if bd hooks run pre-commit "$@"; then
      _bd_exit=0
    else
      _bd_exit=$?
    fi
  fi
  if { [ "$_bd_timeout_backend" = coreutils ] && [ "$_bd_exit" -eq 124 ]; } || { [ "$_bd_timeout_backend" = perl ] && [ "$_bd_exit" -eq 142 ]; }; then
    echo >&2 "beads: hook 'pre-commit' timed out after ${_bd_timeout}s — continuing without beads"
    _bd_exit=0
  fi
  if [ "$_bd_exit" -eq 3 ]; then
    echo >&2 "beads: database not initialized — skipping hook 'pre-commit'"
    _bd_exit=0
  fi
  if [ "$_bd_exit" -ne 0 ]; then exit "$_bd_exit"; fi
fi
# --- END BEADS INTEGRATION v1.3.1 ---
