#!/usr/bin/env bash
# Auto-generated by nerftools: SessionStart hook.
#
# Greets the agent with a one-time reminder that the plugin is installed
# and what it covers. Appends additional, env-gated reminders for each
# PreToolUse check that's currently enabled. Appends a warning if the
# runtime bash is too old for the PreToolUse redirect hook to work.

set -uo pipefail

_PLUGIN_NAME="nerftools"
_PREFIX="nerf-"

_intro="The \`${_PLUGIN_NAME}\` plugin is installed. It provides safety-constrained wrappers for az, gh, git, kubectl, report, and more. Prefer these wrappers over raw bash for commands they cover. Load the \`${_PLUGIN_NAME}\` skill for the full overview, or any \`${_PREFIX}<group>\` skill (e.g. \`nerf-az-account\`) for specific tools. A pre-bash redirect hook may prompt you to use a wrapper when one is available."

_msg="$_intro"

# Reminder for the current-version check, when enabled. The agent gets a
# heads-up that mismatched-version invocations will be rejected, plus the
# specific version they should always use (self-derived from this hook's
# own location). If self-derive fails, still emit a degraded reminder so
# the agent isn't surprised by a deny later.
case "${NERF_ENABLE_CURRENT_VERSION_HOOK:-}" in
  1 | [tT][rR][uU][eE] | [yY][eE][sS] | [oO][nN])
    _self=$(realpath "$0" 2>/dev/null || echo "")
    _ver_dir=""
    if [[ -n "$_self" ]]; then
      _ver_dir=$(dirname "$(dirname "$_self")")
    fi
    _version=""
    if [[ -n "$_ver_dir" && -d "$_ver_dir/skills" && -d "$_ver_dir/.claude-plugin" ]]; then
      _version=$(basename "$_ver_dir")
    fi
    if [[ -n "$_version" ]]; then
      _msg="${_msg}"$'\n\n'"Current-version enforcement is enabled for \`${_PLUGIN_NAME}\`. Always invoke its tools from this session's plugin path (version ${_version}); the PreToolUse hook will reject calls that point at a different version. If you see paths in your context that reference an older version, ignore them and use the current paths."
    else
      _msg="${_msg}"$'\n\n'"Current-version enforcement is enabled for \`${_PLUGIN_NAME}\`, but the active version could not be determined from this hook's path. Mismatched-version calls will still be rejected by the PreToolUse hook. If you see a deny related to versions, treat it as authoritative and escalate to the user."
    fi
    ;;
esac

if [[ "${BASH_VERSINFO[0]:-0}" -lt 4 ]]; then
  _msg="${_msg}"$'\n\n'"Warning: the pre-bash redirect hook is disabled because this system's bash is too old (found ${BASH_VERSION:-unknown}; bash 4+ required). Tell the user to install a newer bash (\`brew install bash\` on macOS, or via the system package manager on Linux) to enable it. Generated wrapper scripts may also fail until bash is upgraded."
fi

# Emit JSON via jq when available; otherwise fall back to a static notice
# that at least tells someone how to fix things.
if command -v jq >/dev/null 2>&1; then
  jq -nc --arg m "$_msg" \
    '{hookSpecificOutput: {hookEventName: "SessionStart", additionalContext: $m}}'
else
  printf '%s\n' '{"hookSpecificOutput":{"hookEventName":"SessionStart","additionalContext":"nerftools: jq is required for nerftools hooks but is not installed. Tell the user to install jq."}}'
fi
