#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]:-$0}")" && pwd)"
PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

using_kicad_content=$(cat "${PLUGIN_ROOT}/skills/using-kicad/SKILL.md" 2>&1 || echo "Error reading using-kicad skill")

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_kicad_escaped=$(escape_for_json "$using_kicad_content")

session_context="<EXTREMELY_IMPORTANT>\nYou have KiCad design skills.\n\n**Below is the full content of your 'kicad:using-kicad' skill - your introduction to the KiCad design pipeline. For all other skills, use the 'Skill' tool:**\n\n${using_kicad_escaped}\n</EXTREMELY_IMPORTANT>"

if [ -n "${CLAUDE_PLUGIN_ROOT:-}" ]; then
  cat <<EOF
{
  "hookSpecificOutput": {
    "hookEventName": "SessionStart",
    "additionalContext": "${session_context}"
  }
}
EOF
else
  cat <<EOF
{
  "additional_context": "${session_context}"
}
EOF
fi

exit 0
