#!/bin/sh
# ideaudit skill installer (POSIX sh — works under bash, dash, busybox ash).
# Usage: curl https://inite.studio/install.sh | sh
#    or: curl -fsSL https://inite.studio/install.sh | sh -s -- --target project --key ideaudit_xxx
#
# Optional --key flag: saves the key to ~/.config/ideaudit/api_key (chmod 600),
# which is where the audit-* skills look for it, and pings /mcp/install-probe
# so the dashboard onboarding wizard can flip step 2 to done.
#
# By default, installs into ~/.claude/skills/ (user-global).
# Use --target project to install into $PWD/.claude/skills/ instead.
set -e

TARGET_MODE="user"
INSTALL_KEY="${IDEAUDIT_API_KEY-}"
PROBE_URL="${IDEAUDIT_PROBE_URL-https://api.inite.studio/mcp/install-probe}"
SKILLS_URL="${IDEAUDIT_SKILLS_URL-https://inite.studio/skills.tar.gz}"

while [ $# -gt 0 ]; do
  case "$1" in
    --target=user)    TARGET_MODE="user"; shift ;;
    --target=project) TARGET_MODE="project"; shift ;;
    --target)         shift; TARGET_MODE="$1"; shift ;;
    --key=*)          INSTALL_KEY="${1#--key=}"; shift ;;
    --key)            shift; INSTALL_KEY="$1"; shift ;;
    --help|-h)
      echo "Usage: install.sh [--target user|project] [--key ideaudit_xxx]"
      echo "  user    (default) install into ~/.claude/skills/"
      echo "  project install into \$PWD/.claude/skills/"
      echo "  --key    save the key for the skills, and notify the dashboard"
      exit 0
      ;;
    *) shift ;;
  esac
done

if [ "$TARGET_MODE" = "project" ]; then
  TARGET_DIR="$PWD/.claude/skills"
else
  TARGET_DIR="$HOME/.claude/skills"
fi

TMP_DIR="$(mktemp -d)"
trap 'rm -rf "$TMP_DIR"' EXIT

echo "-> Fetching ideaudit skills from $SKILLS_URL"
if ! command -v curl >/dev/null 2>&1; then
  echo "! curl not found - please install curl." >&2
  exit 1
fi
if ! command -v tar >/dev/null 2>&1; then
  echo "! tar not found - please install tar." >&2
  exit 1
fi
curl -fsSL "$SKILLS_URL" -o "$TMP_DIR/skills.tar.gz"
mkdir -p "$TMP_DIR/extract"
tar -xzf "$TMP_DIR/skills.tar.gz" -C "$TMP_DIR/extract"

# Tarball layout: top-level `skills/` directory.
SRC="$TMP_DIR/extract/skills"
if [ ! -d "$SRC" ]; then
  echo "! Skills folder not found in tarball." >&2
  exit 1
fi

mkdir -p "$TARGET_DIR"
echo "-> Installing to $TARGET_DIR"
INSTALLED=0
for skill in "$SRC"/*/; do
  [ -d "$skill" ] || continue
  name="$(basename "$skill")"
  cp -r "$skill" "$TARGET_DIR/$name"
  INSTALLED=$((INSTALLED + 1))
  echo "   + $name"
done

# Drop the bundle VERSION file alongside the skills so audit-update
# (and the audit-* staleness check) can read a single number to detect
# drift. CHANGELOG copied too for offline reading.
if [ -f "$SRC/VERSION" ]; then
  cp "$SRC/VERSION" "$TARGET_DIR/VERSION"
fi
if [ -f "$SRC/CHANGELOG.md" ]; then
  cp "$SRC/CHANGELOG.md" "$TARGET_DIR/CHANGELOG.md"
fi

echo ""
echo "Installed $INSTALLED skills into $TARGET_DIR (bundle $(cat "$TARGET_DIR/VERSION" 2>/dev/null || echo unknown))"
echo ""

# Persist the key where the skills look for it. audit-idea and audit-quick
# both halt on a missing ~/.config/ideaudit/api_key, and until now only
# /audit-setup ever wrote that file - so anyone who followed the onboarding
# wizard or the docs got "ideaudit isn't set up" over a working MCP wire.
if [ -n "$INSTALL_KEY" ]; then
  KEY_DIR="${XDG_CONFIG_HOME:-$HOME/.config}/ideaudit"
  mkdir -p "$KEY_DIR"
  printf '%s' "$INSTALL_KEY" > "$KEY_DIR/api_key"
  chmod 600 "$KEY_DIR/api_key"
  echo "-> Saved API key to $KEY_DIR/api_key"
fi

# P1.17 - best-effort probe ping. Marks the dashboard's "Install skills" step
# as complete without conflating it with "first MCP request from this key".
# Silent on failure - install.sh is offline-resilient.
if [ -n "$INSTALL_KEY" ] && command -v curl >/dev/null 2>&1; then
  curl -fsS -o /dev/null \
    -X POST "$PROBE_URL" \
    -H "Authorization: Bearer $INSTALL_KEY" \
    -H "Content-Type: application/json" \
    --max-time 5 \
    -d '{}' 2>/dev/null || true
  echo "-> Notified dashboard."
fi

if [ -n "$INSTALL_KEY" ]; then
  echo "Next: register the MCP server with Claude Code —"
  echo "      claude mcp add ideaudit --transport http https://api.inite.studio/mcp \\"
  echo "        --scope user --header \"Authorization: Bearer \$(cat \"$KEY_DIR/api_key\")\""
  echo "      Or run /audit-setup in Claude and it will do this for you."
else
  echo "Next: generate your API key at https://inite.studio/settings/keys"
  echo "      and add the MCP server to your Claude config. See:"
  echo "      https://inite.studio/docs#connect"
fi
