#!/bin/sh
# pinsay-skill-version: 0.0.0-dev+skills.4df629adbc31
# PinSay — install the AI skills (pinsay-init + pinsay-feedback) into your AI
# tool's skills directory.
#
# Self-configuring: this script is served pre-filled with your PinSay server URL,
# and the skills it downloads are pre-filled too — nothing to edit.
#
# Usage:
#   curl -fsSL <server>/install.sh | sh                       # → .claude/skills/
#   curl -fsSL <server>/install.sh | sh -s -- .cursor/rules   # custom directory
set -e

SERVER="https://app.pinsay.dev"
DIR="${1:-.claude/skills}"

echo "PinSay: installing skills from $SERVER into $DIR/"

curl -fsSL --create-dirs "$SERVER/pinsay-init.md" -o "$DIR/pinsay-init/SKILL.md"
echo "  ok  pinsay-init      ($DIR/pinsay-init/SKILL.md)   — add the widget to an app"
curl -fsSL --create-dirs "$SERVER/skill.md" -o "$DIR/pinsay-feedback/SKILL.md"
echo "  ok  pinsay-feedback  ($DIR/pinsay-feedback/SKILL.md)   — list / apply comments"
curl -fsSL --create-dirs "$SERVER/skills/apply.md" -o "$DIR/pinsay-feedback/apply.md"
curl -fsSL --create-dirs "$SERVER/skills/translate.md" -o "$DIR/pinsay-feedback/translate.md"
curl -fsSL --create-dirs "$SERVER/skills/advanced.md" -o "$DIR/pinsay-feedback/advanced.md"
echo "  ok  pinsay-feedback sub-skills (apply.md, translate.md, advanced.md) in $DIR/pinsay-feedback/"

# Also link .agents/ to the same files — a tool-agnostic convention some AI coding agents read
# from directly, so a non-Claude agent finds the skill without the .claude-specific path. A
# SYMLINK, not a copy: $DIR stays the one real location to edit/update — .agents/ just points at
# it, so the two can never drift out of sync. ".agents/<skill>/" is always exactly 2 segments
# deep, so "../../$DIR/..." reaches repo root then back down $DIR regardless of $DIR's own depth.
# Skipped when $DIR already IS .agents (custom-dir invocation) to avoid linking it to itself.
if [ "$DIR" != ".agents" ]; then
  mkdir -p .agents/pinsay-init .agents/pinsay-feedback
  ln -sf "../../$DIR/pinsay-init/SKILL.md" .agents/pinsay-init/SKILL.md
  ln -sf "../../$DIR/pinsay-feedback/SKILL.md" .agents/pinsay-feedback/SKILL.md
  echo "  ok  linked .agents/pinsay-init/SKILL.md and .agents/pinsay-feedback/SKILL.md -> $DIR/"
fi

# --- AI apply-tool credentials -------------------------------------------------
# The pinsay-feedback skill authenticates with a long-lived personal API key (not
# email/password) and reads it from a gitignored .pinsay/credentials.env. Scaffold
# both files now so this critical step is never forgotten:
#   credentials.env          real value (gitignored — never committed)
#   credentials.env.example  committable template documenting the key
mkdir -p .pinsay

cat > .pinsay/credentials.env.example <<'EOF'
# PinSay personal API key — copy to credentials.env and fill in.
# Find/copy yours from your PinSay profile page, or the dashboard's quick-start guide.
PINSAY_API_KEY=pnsy_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
EOF
echo "  ok  credentials.example  (.pinsay/credentials.env.example)   — template (gitignored, re-created per clone)"

if [ -f .pinsay/credentials.env ]; then
  echo "  ok  credentials          (.pinsay/credentials.env already exists — left untouched)"
else
  cat > .pinsay/credentials.env <<'EOF'
# PinSay personal API key (gitignored — NEVER commit). Fill this in before
# pulling/applying feedback, or login will fail. Copy it from your PinSay profile
# page, or the dashboard's quick-start guide.
PINSAY_API_KEY=
EOF
  echo "  ok  credentials          (.pinsay/credentials.env)   — ⚠️  FILL IN PINSAY_API_KEY"
fi

# Gitignore .pinsay/ (secrets: credentials.env) but keep
# stack.json AND config.json committable — neither is a secret (stack.json is detected
# frontend/backend/aiTools; config.json is the CLI's project config) and every developer/agent needs
# them via normal git. Everything else in .pinsay/ and the AI skill files are served by the server
# and re-installed per clone, so they stay out of the repo (see docs/ON-DISK-CONTRACT.md).
#
# The pattern is `.pinsay/*`, NOT `.pinsay/`. Git does not descend into an excluded DIRECTORY, so
# with the directory form it never considers the files inside it and all four `!` lines below are
# inert — every one of these four was silently ignored, the opposite of what this block says it
# does. Excluding the directory's CONTENTS instead leaves the negations able to re-include.
touch .gitignore
# Migrate a .gitignore written by an earlier version, which would otherwise keep winning.
if grep -qxF '.pinsay/' .gitignore; then
  sed -i.bak 's|^\.pinsay/$|.pinsay/*|' .gitignore && rm -f .gitignore.bak
fi
grep -qxF '.pinsay/*' .gitignore || echo '.pinsay/*' >> .gitignore
grep -qxF '!.pinsay/stack.json' .gitignore || echo '!.pinsay/stack.json' >> .gitignore
grep -qxF '!.pinsay/config.json' .gitignore || echo '!.pinsay/config.json' >> .gitignore
# Only config.json + stack.json are committed. credentials.env.example and the AI skill
# files are served by the server and re-installed per clone (`npx @pinsay/cli init` joins;
# `update` re-fetches) — drop the negations an earlier version added for them.
for stale in '!.pinsay/pinsay.sh' '!.pinsay/credentials.env.example'; do
  if grep -qxF "$stale" .gitignore; then
    grep -vxF "$stale" .gitignore > .gitignore.tmp && mv .gitignore.tmp .gitignore
  fi
done
for skilldir in '.claude/skills/pinsay-init/' '.claude/skills/pinsay-feedback/' '.agents/pinsay-init/' '.agents/pinsay-feedback/'; do
  grep -qxF "$skilldir" .gitignore || echo "$skilldir" >> .gitignore
done

echo ""
echo "Done. Next:"
echo "  1. Fill PINSAY_API_KEY in .pinsay/credentials.env — copy it from your PinSay profile"
echo "     page, or the dashboard's quick-start guide."
echo "  2. Run the 'pinsay-init' skill in your AI tool to add the widget to your app — its last step"
echo "     detects the tech stack and writes the committable .pinsay/stack.json."
echo "  3. To check comments: npx @pinsay/cli list"
echo "  4. To apply comments: npx @pinsay/cli apply"
