#!/usr/bin/env bash
# agent-config — thin wrapper for the event4u/agent-config CLI.
#
# Auto-generated by the package installer. Delegates to the master CLI
# in this priority:
#   1. $AGENT_CONFIG_MASTER (escape hatch for tests / custom checkouts)
#   2. ./node_modules/@event4u/agent-config/scripts/agent-config (npm install)
#   3. globally-installed `agent-config` on $PATH (npm i -g)
#   4. `npx --yes @event4u/agent-config@latest` (last-resort, network)
#
# Do NOT edit — this file is gitignored and regenerated on every install.
#
# Usage:
#   ./agent-config help
#   ./agent-config mcp:render
#   ./agent-config roadmap:progress
#
# See: https://github.com/event4u-app/agent-config

set -euo pipefail

SELF_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Step-7 Phase 3 — pin the project root for the master CLI so subdir
# invocations of ``./agent-config`` (or symlinks to it) do not re-walk
# the anchor tree. The wrapper lives at the project root by definition,
# so SELF_DIR *is* the canonical root. Honor a pre-set value as an
# escape hatch (test harnesses, supervisors that already pinned it).
export AGENT_CONFIG_PROJECT_ROOT="${AGENT_CONFIG_PROJECT_ROOT:-$SELF_DIR}"

locate_master() {
  if [[ -n "${AGENT_CONFIG_MASTER:-}" && -x "$AGENT_CONFIG_MASTER" ]]; then
    printf '%s' "$AGENT_CONFIG_MASTER"
    return 0
  fi
  local local_npm="$SELF_DIR/node_modules/@event4u/agent-config/scripts/agent-config"
  if [[ -x "$local_npm" ]]; then
    printf '%s' "$local_npm"
    return 0
  fi
  local on_path
  if on_path="$(command -v agent-config 2>/dev/null)" && [[ -n "$on_path" && "$on_path" != "$SELF_DIR/agent-config" ]]; then
    printf '%s' "$on_path"
    return 0
  fi
  return 1
}

if MASTER="$(locate_master)"; then
  exec "$MASTER" "$@"
fi

if command -v npx >/dev/null 2>&1; then
  exec npx --yes @event4u/agent-config@latest "$@"
fi

cat >&2 <<EOF
❌  agent-config: master CLI not found.
    Tried, in order:
      - \$AGENT_CONFIG_MASTER
      - ./node_modules/@event4u/agent-config/scripts/agent-config
      - agent-config on \$PATH (npm i -g @event4u/agent-config)
      - npx @event4u/agent-config@latest (npx not installed)

    Install via:
      npx @event4u/agent-config init            # one-shot setup
      npm install -g @event4u/agent-config      # global CLI
EOF
exit 127
