#!/bin/sh
# Pre-commit checks for claude-code-plugins.
#
# 1. If the catalog source-of-truth (.claude-plugin/marketplace.extended.json)
#    is staged, regenerate ALL derived artifacts BEFORE lint-staged runs:
#      - .claude-plugin/marketplace.json     (CLI catalog, auto-stripped fields)
#      - plugins/**/package.json             (npm tracking artifacts for new plugins)
#      - README.md AUTO-TOC sentinel block   (browse-by-category nav)
#    Then auto-stage any newly-modified files so the commit stays consistent.
#
#    Catches the failure mode that bit PR #665 three times: catalog edit lands
#    without the derived regeneration, CI gates fail one-by-one across multiple
#    rounds.
#
# 2. Standard pre-commit gates after the regen: lint-staged + audit-harness.

# Step 1: Catalog-edit auto-regen
if git diff --cached --name-only | grep -qx '.claude-plugin/marketplace.extended.json'; then
  echo "→ marketplace.extended.json is staged; regenerating derived files..."
  pnpm run sync-marketplace || {
    echo "✗ sync-marketplace failed. Fix the error and re-stage."
    exit 1
  }
  # Stage any files the regen touched (idempotent — git ignores no-op stages).
  git add .claude-plugin/marketplace.json README.md 2>/dev/null
  # Plugin package.json files only get created for NEW plugins; safe to stage all.
  git add 'plugins/**/package.json' 2>/dev/null
  echo "✓ Derived files regenerated and staged."
fi

# Step 2: Standard gates
pnpm exec lint-staged && pnpm exec audit-harness verify

# E4.14 refuse-to-start pre-flight (blueprint 727 § 18.7): refuse to proceed
# while a live-shaped plaintext credential sits in this box's MCP configs
# (repo .mcp.json + ~/.claude.json project registration). The sanctioned fix
# is SOPS (.sops.env) + scripts/sops-env at launch. Fast: two small JSON reads.
node scripts/check-mcp-plaintext-creds.mjs --all-local || {
  echo "✗ plaintext MCP credential on this box — encrypt it before continuing." >&2
  exit 1
}
