#!/usr/bin/env bash
# SessionStart hook for micropowers — injects micropowers entry skill at session start.
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PLUGIN_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"

content=$(cat "${PLUGIN_ROOT}/skills/micropowers/SKILL.md" 2>&1 || echo "Error reading micropowers 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"
}

escaped=$(escape_for_json "$content")
ctx="Micropowers loaded. When you need structured development workflow, ask your human partner if they want to use /micropowers. Below is the full micropowers entry skill:\n\n${escaped}"

if [ -n "${CURSOR_PLUGIN_ROOT:-}" ]; then
    printf '{\n  "additional_context": "%s"\n}\n' "$ctx" | cat
elif [ -n "${CLAUDE_PLUGIN_ROOT:-}" ] && [ -z "${COPILOT_CLI:-}" ]; then
    printf '{\n  "hookSpecificOutput": {\n    "hookEventName": "SessionStart",\n    "additionalContext": "%s"\n  }\n}\n' "$ctx" | cat
else
    printf '{\n  "additionalContext": "%s"\n}\n' "$ctx" | cat
fi

exit 0
