#!/usr/bin/env bash
# Codex SessionStart hook for spark plugin

set -euo pipefail

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

using_spark_content=$(cat "${PLUGIN_ROOT}/skills/using-spark/SKILL.md" 2>&1 || echo "Error reading using-spark 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_spark_escaped=$(escape_for_json "$using_spark_content")
session_context="<EXTREMELY_IMPORTANT>\nYou have spark.\n\n**Below is the full content of your 'spark:using-spark' skill - your introduction to using skills. For all other skills, follow the Codex skill-loading instructions in that skill:**\n\n${using_spark_escaped}\n</EXTREMELY_IMPORTANT>"

printf '{\n  "hookSpecificOutput": {\n    "hookEventName": "SessionStart",\n    "additionalContext": "%s"\n  }\n}\n' "$session_context" | cat

exit 0
