#!/bin/sh
# goal-prompts installer — https://goal-prompts.vercel.app
#
# Installs the full goal-prompts catalog as Claude Code slash commands.
# Files land in .claude/commands/goal/ as goal-<slug>.md, so the commands
# surface as /goal-bug-hunt, /goal-prune-audit, … (a commands subdirectory
# does not namespace). Prefer real /goal:<slug> names? Install the plugin:
#   /plugin marketplace add GhostlyGawd/goal-prompts   then install "goal"
#
# Re-run any time to update. Uninstall: rm -rf .claude/commands/goal
# Want just one brief? BRIEF picks it by id or slug and leaves any other
# installed briefs untouched, e.g.
#   BRIEF=01 sh install        or    BRIEF=bug-hunt sh install
# Forks/tests: BASE overrides the download origin, e.g.
#   BASE=https://audits.your-co.internal sh install
set -e
BASE="${BASE:-https://goal-prompts.vercel.app}"
DEST=".claude/commands"

if [ ! -d .git ]; then
  printf 'note: no .git here — installing into %s/.claude/commands anyway\n' "$PWD"
fi
mkdir -p "$DEST"
TMP=$(mktemp)
trap 'rm -f "$TMP"' EXIT
printf 'Downloading goal prompts…\n'
curl -fsSL "$BASE/commands.tar.gz" -o "$TMP"

sha256() {
  if command -v sha256sum >/dev/null 2>&1; then sha256sum "$1" | cut -d' ' -f1
  elif command -v shasum >/dev/null 2>&1; then shasum -a 256 "$1" | cut -d' ' -f1
  elif command -v openssl >/dev/null 2>&1; then openssl dgst -sha256 "$1" | awk '{print $NF}'
  fi
}
GOT=$(sha256 "$TMP")
WANT=$(curl -fsSL "$BASE/checksums.txt" 2>/dev/null | grep ' commands.tar.gz' | cut -d' ' -f1 || true)
if [ -z "$GOT" ]; then
  printf '⚠ NOT verified — no sha256sum, shasum, or openssl on this system; installing unverified.\n'
elif [ -z "$WANT" ]; then
  printf '⚠ NOT verified — could not fetch checksums.txt; installing unverified.\n'
elif [ "$WANT" != "$GOT" ]; then
  printf 'ERROR: checksum mismatch — refusing to install.\n' >&2
  exit 1
else
  printf 'Checksum verified.\n'
fi

# Per-brief install (BRIEF=<id or slug>): extract one command from the same
# verified tarball, leaving everything else in $DEST/goal alone.
if [ -n "$BRIEF" ]; then
  SLUG="$BRIEF"
  case "$BRIEF" in
    [0-9][0-9]|[0-9][0-9][0-9])
      # ids resolve to slugs through catalog.json ("id" precedes "slug"
      # inside each brief object — the build writes keys sorted)
      SLUG=$(curl -fsSL "$BASE/catalog.json" 2>/dev/null | awk -v id="$BRIEF" '
        index($0, "\"id\": \"" id "\"") { found = 1 }
        found && /"slug":/ { gsub(/[" ,]/, "", $2); print $2; exit }')
      ;;
  esac
  if [ -z "$SLUG" ] || ! tar -tzf "$TMP" "goal/goal-$SLUG.md" >/dev/null 2>&1; then
    printf 'ERROR: unknown brief "%s" — browse ids and slugs at %s\n' "$BRIEF" "$BASE" >&2
    exit 1
  fi
  tar -xzf "$TMP" -C "$DEST" "goal/goal-$SLUG.md" "goal/.version"
  VER=$(cat "$DEST/goal/.version" 2>/dev/null || echo '?')
  printf 'Installed /goal-%s (goal-prompts v%s) into %s/goal/ — other installed briefs untouched.\n' "$SLUG" "$VER" "$DEST"
  printf 'Whole catalog: re-run without BRIEF.  Uninstall this brief: rm %s/goal/goal-%s.md\n' "$DEST" "$SLUG"
  printf 'Catalog: %s\n' "$BASE"
  exit 0
fi

if [ -d "$DEST/goal" ]; then
  printf 'Existing %s/goal found — replacing it (local edits to those files will be lost).\n' "$DEST"
  rm -rf "$DEST/goal"
fi
tar -xzf "$TMP" -C "$DEST"
N=$(ls "$DEST/goal" | grep -c '\.md$')
VER=$(cat "$DEST/goal/.version" 2>/dev/null || echo '?')
printf 'Installed %s commands (goal-prompts v%s) into %s/goal/\n\n' "$N" "$VER" "$DEST"
printf 'In Claude Code, try:\n'
printf '  /goal-product-improvement  the flagship discovery audit\n'
printf '  /goal-bug-hunt             find what is already broken\n'
printf '  /goal-audit-triage         the agent names the audits this repo needs\n'
printf '  /goal-roadmap-synthesis    run last — merges all reports\n\n'
printf 'Prefer real /goal:<slug> names? Install the Claude Code plugin instead:\n'
printf '  /plugin marketplace add GhostlyGawd/goal-prompts   then install "goal"\n\n'
printf 'Update: re-run this installer.  Uninstall: rm -rf %s/goal\n' "$DEST"
printf "What's new since v%s: %s/changelog\n" "$VER" "$BASE"
printf 'Catalog: %s\n' "$BASE"
