#!/bin/sh
# codepremise kit installer — run from the root of a git repo:
#   curl -fsSL https://codepremise.dev/install.sh | sh
set -e

BASE="${CODEPREMISE_BASE_URL:-https://codepremise.dev}"

if [ ! -d .git ]; then
  echo "Run this from the root of a git repository." >&2
  exit 1
fi

echo "Installing the codepremise kit from $BASE …"

# ---- the map itself ----
mkdir -p .codepremise/nodes
if [ ! -f .codepremise/README.md ]; then
  cat > .codepremise/README.md <<'EOF'
# .codepremise — business-logic map

One JSON file per business-logic claim, under nodes/. Written and maintained
by coding agents at generation time; reviewed by humans as the primary
artifact of a pull request. See the codepremise skill for the format.
EOF
fi

# ---- validator (schema + integrity + hash truthfulness; use in CI too) ----
curl -fsSL "$BASE/kit/codepremise-validate.mjs" -o .codepremise/validate.mjs

# ---- ONE skill for every agent (Agent Skills open standard) ----
# canonical location: .agents/skills/ — read by Codex, Cursor, and 40+ tools
mkdir -p .agents/skills/codepremise .agents/skills/codepremise-bootstrap
curl -fsSL "$BASE/kit/SKILL.md" -o .agents/skills/codepremise/SKILL.md
curl -fsSL "$BASE/kit/BOOTSTRAP.md" -o .agents/skills/codepremise-bootstrap/SKILL.md

# Claude Code discovers skills under .claude/skills/ — link to the same files
mkdir -p .claude/skills
if [ ! -e .claude/skills/codepremise ]; then
  ln -s ../../.agents/skills/codepremise .claude/skills/codepremise 2>/dev/null || {
    mkdir -p .claude/skills/codepremise
    cp .agents/skills/codepremise/SKILL.md .claude/skills/codepremise/SKILL.md
  }
fi
if [ ! -e .claude/skills/codepremise-bootstrap ]; then
  ln -s ../../.agents/skills/codepremise-bootstrap .claude/skills/codepremise-bootstrap 2>/dev/null || {
    mkdir -p .claude/skills/codepremise-bootstrap
    cp .agents/skills/codepremise-bootstrap/SKILL.md .claude/skills/codepremise-bootstrap/SKILL.md
  }
fi

# ---- enforcement hooks (per-agent wiring, same shared script) ----
# hooks are what force the skill to be honored: they fire on every edit and
# point the agent back at the skill when the map goes stale
mkdir -p .claude/hooks .codex/hooks .cursor/hooks
curl -fsSL "$BASE/kit/codepremise-check.py" -o .claude/hooks/codepremise-check.py
cp .claude/hooks/codepremise-check.py .codex/hooks/codepremise-check.py
cp .claude/hooks/codepremise-check.py .cursor/hooks/codepremise-check.py
chmod +x .claude/hooks/codepremise-check.py .codex/hooks/codepremise-check.py .cursor/hooks/codepremise-check.py

if [ ! -f .claude/settings.json ]; then
  curl -fsSL "$BASE/kit/claude-settings.json" -o .claude/settings.json
  echo "  created .claude/settings.json with the codepremise-check hook"
else
  echo "  NOTE: .claude/settings.json already exists — add this hook manually:"
  echo '        PostToolUse · matcher "Edit|Write|MultiEdit" · command ".claude/hooks/codepremise-check.py"'
fi
if [ ! -f .codex/hooks.json ]; then
  curl -fsSL "$BASE/kit/codex-hooks.json" -o .codex/hooks.json
else
  echo "  NOTE: .codex/hooks.json already exists — add a PostToolUse hook running .codex/hooks/codepremise-check.py"
fi
if [ ! -f .cursor/hooks.json ]; then
  curl -fsSL "$BASE/kit/cursor-hooks.json" -o .cursor/hooks.json
else
  echo "  NOTE: .cursor/hooks.json already exists — add an afterFileEdit hook running ./hooks/codepremise-check.py"
fi

echo ""
echo "Done. Installed:"
echo "  .codepremise/                  — the map (agents fill it as they work)"
echo "  .codepremise/validate.mjs      — validator + impact query (run it in CI)"
echo "  .agents/skills/codepremise/    — ONE skill, every agent (Agent Skills standard)"
echo "  .agents/skills/codepremise-bootstrap/ — seed the map for an existing repo:"
echo "                                   tell your agent 'bootstrap the codepremise map'"
echo "  .claude/skills/ (links)        — Claude Code discovery of the same skill"
echo "  .claude|.codex|.cursor hooks   — enforce it: they fire on every edit and"
echo "                                   point the agent back at the skill"
echo ""
echo "Commit these files, let your agent work, then open any PR at:"
echo "  $BASE/<owner>/<repo>/pull/<number>"
