#!/usr/bin/env bash
set -euo pipefail

if [ "${1:-}" = "--help" ] || [ "${1:-}" = "-h" ]; then
  cat <<'USAGE'
Purpose: Detect added component files in a unified diff. New component files
         are signaled by `diff --git a/... b/<path>` plus a `new file mode`
         line followed by `--- /dev/null`. Each addition triggers a proposal
         to extend components.md.

Usage:   bash scripts/detect-component-additions.sh                  (reads stdin)
         bash scripts/detect-component-additions.sh <file>           (reads file)

Output:  one row per added component file to stdout:
           <component-name>\t<source-file>
         Diagnostics to stderr.
USAGE
  exit 0
fi

if [ "${1:-}" = "" ] || [ "${1:-}" = "-" ]; then
  body=$(cat)
elif [ -f "$1" ]; then
  body=$(cat "$1")
else
  printf 'error: file not found: %s\n' "$1" >&2
  exit 2
fi

printf '%s\n' "$body" | awk '
  /^new file mode/ { is_new = 1; next }
  /^\+\+\+ b\// && is_new {
    path = substr($0, 7)            # strip `+++ b/`
    is_new = 0
    if (path !~ /src\/components\//) next
    # ignore test/spec/stories/d.ts/index files
    if (path ~ /\.test\./ || path ~ /\.spec\./ || path ~ /\.stories\./) next
    if (path ~ /\.d\.ts$/) next
    n = split(path, parts, "/")
    base = parts[n]
    if (base == "index.ts" || base == "index.tsx" || base == "index.js" || base == "index.jsx") next
    name = base
    sub(/\.[^.]+$/, "", name)
    # `[A-Z]` is locale-dependent in awk on some POSIX locales (collates as
    # `aBcDeF...` and matches lowercase too). Use an explicit byte set.
    first = substr(name, 1, 1)
    if (first ~ /[ABCDEFGHIJKLMNOPQRSTUVWXYZ]/) {
      printf "%s\t%s\n", name, path
    }
  }
  /^diff --git/ { is_new = 0 }
'
