#!/usr/bin/env bash
# SessionStart hook for superpowers plugin

set -euo pipefail

# Determine plugin root directory
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

# Check if legacy skills directory exists and build warning
warning_message=""
legacy_skills_dir="${HOME}/.config/superpowers/skills"
if [ -d "$legacy_skills_dir" ]; then
  warning_message="\n\n⚠️ **Warning:** Superpowers now uses Claude Code's skills system. Custom skills in ~/.config/superpowers/skills will not be read. Move custom skills to ~/.claude/skills instead. To make this message go away, remove ~/.config/superpowers/skills"
fi

# Read using-superpowers content
skill_path="${PLUGIN_ROOT}/skills/using-superpowers/SKILL.md"
if [ ! -f "$skill_path" ]; then
  using_superpowers_content="Error reading using-superpowers skill"
else
  using_superpowers_content=$(cat "$skill_path" 2>&1 || echo "Error reading using-superpowers skill")
fi

# Ensure loaded file path is scoped to plugin root skills directory.
case "$skill_path" in
  "${PLUGIN_ROOT}/skills/"*) ;;
  *)
  using_superpowers_content="Error: invalid skill path"
  ;;
esac

sanitize_context() {
  local s="$1"
  s="${s//<EXTREMELY_IMPORTANT>/}"
  s="${s//<EXTREMELY-IMPORTANT>/}"
  s="${s//<HARD-GATE>/}"
  s="${s//<SUBAGENT-STOP>/}"
  s="${s//<important-reminder>/}"
  s="${s//<\/EXTREMELY_IMPORTANT>/}"
  s="${s//<\/EXTREMELY-IMPORTANT>/}"
  s="${s//<\/HARD-GATE>/}"
  s="${s//<\/SUBAGENT-STOP>/}"
  s="${s//<\/important-reminder>/}"
  printf '%s' "$s"
}

using_superpowers_content="$(sanitize_context "$using_superpowers_content")"
if [ "${#using_superpowers_content}" -gt 51200 ]; then
  using_superpowers_content="${using_superpowers_content:0:51200}\n\n[truncated for safety]"
fi

# Escape string for JSON embedding using bash parameter substitution.
# Each ${s//old/new} is a single C-level pass - orders of magnitude
# faster than the character-by-character loop this replaces.
escape_for_json() {
    local s="$1"
    s="${s//\\/\\\\}"
    s="${s//\"/\\\"}"
    s="${s//$'\n'/\\n}"
    s="${s//$'\r'/\\r}"
    s="${s//$'\t'/\\t}"
    printf '%s' "$s"
}

using_superpowers_escaped=$(escape_for_json "$using_superpowers_content")
warning_escaped=$(escape_for_json "$warning_message")
session_context="## Superpowers Context\nYou have superpowers.\n\n**Below is the full content of your 'superpowers:using-superpowers' skill. For all other skills, use the Skill tool:**\n\n${using_superpowers_escaped}\n\n${warning_escaped}"

# Output context injection as JSON.
# Cursor hooks expect additional_context.
# Claude Code hooks expect hookSpecificOutput.additionalContext.
# Claude Code reads BOTH fields without deduplication, so we must only
# emit the field consumed by the current platform to avoid double injection.
#
# Uses printf instead of heredoc (cat <<EOF) to work around a bash 5.3+
# bug where heredoc variable expansion hangs when content exceeds ~512 bytes.
# See: https://github.com/obra/superpowers/issues/571
if [ -n "${CURSOR_PLUGIN_ROOT:-}" ]; then
  # Cursor sets CURSOR_PLUGIN_ROOT (may also set CLAUDE_PLUGIN_ROOT) — emit additional_context
  printf '{\n  "additional_context": "%s"\n}\n' "$session_context"
elif [ -n "${CLAUDE_PLUGIN_ROOT:-}" ]; then
  # Claude Code sets CLAUDE_PLUGIN_ROOT — emit only hookSpecificOutput
  printf '{\n  "hookSpecificOutput": {\n    "hookEventName": "SessionStart",\n    "additionalContext": "%s"\n  }\n}\n' "$session_context"
else
  # Other platforms — emit additional_context as fallback
  printf '{\n  "additional_context": "%s"\n}\n' "$session_context"
fi

exit 0
