#!/usr/bin/env bash
# Pre-ship quick scan, free from WhichAICoder (https://whichaicoder.com/secure-ai-coding-agents/#quick-scan)
# Runs 5 read-only checks on a git project before you show it to real users.
# It reads files only: it never changes anything and makes no network calls.
# Usage: run from your project root:  bash pre-ship-quick-scan.sh
# A clean result does NOT mean your app is secure. It is a quick first pass, not an audit.
set -u
fail=0; warn=0
ok()   { printf '  PASS  %s\n' "$1"; }
bad()  { printf '  FAIL  %s\n' "$1"; fail=$((fail+1)); }
note() { printf '  WARN  %s\n' "$1"; warn=$((warn+1)); }

if ! git rev-parse --is-inside-work-tree >/dev/null 2>&1; then
  echo "Run this from inside a git project (no git repo found here)."; exit 2
fi
cd "$(git rev-parse --show-toplevel)" || exit 2
echo "Pre-ship quick scan: $(pwd)"

# 1. Real .env files committed to git (.env.example / .env.sample / .env.template are fine)
tracked_env=$(git ls-files | grep -E '(^|/)\.env(\.[A-Za-z0-9_-]+)?$' | grep -Ev '\.(example|sample|template|dist)$' || true)
echo "1. .env files committed to git"
if [ -n "$tracked_env" ]; then bad "These are tracked by git: $(echo $tracked_env)"; echo "        Remove with: git rm --cached <file>, then rotate every key that was in it."
else ok "No real .env file is tracked."; fi

# 2. .gitignore covers .env
echo "2. .gitignore covers .env files"
if git check-ignore -q .env 2>/dev/null; then ok ".env is ignored."
else bad ".env would NOT be ignored. Add a line '.env*' (and '!.env.example') to .gitignore."; fi

# 3. Secret-looking values behind browser-exposed prefixes
echo "3. Secrets behind NEXT_PUBLIC_ / VITE_ / EXPO_PUBLIC_ / REACT_APP_ prefixes"
pub=$(grep -HnE '^(NEXT_PUBLIC_|VITE_|EXPO_PUBLIC_|REACT_APP_)[A-Z0-9_]*(SECRET|PRIVATE|SERVICE_ROLE|PASSWORD|STRIPE_SECRET|SK_)[A-Z0-9_]*=' .env* 2>/dev/null | cut -d= -f1 || true)
if [ -n "$pub" ]; then bad "Browser-exposed names that look secret (these ship to every visitor):"; echo "$pub" | sed 's/^/        /'
else ok "No secret-looking names behind a public prefix in .env files."; fi

# 4. Server-only keys referenced in client-side folders
echo "4. Server-only keys in client code"
dirs=""; for d in src app pages components public lib; do [ -d "$d" ] && dirs="$dirs $d"; done
if [ -n "$dirs" ]; then
  hits=$(git grep -nIE 'service_role|SUPABASE_SERVICE_ROLE|SERVICE_ROLE_KEY|STRIPE_SECRET_KEY' -- $dirs 2>/dev/null | grep -vE '(^|/)(api|server|functions|supabase/functions)/' | head -20 || true)
  if [ -n "$hits" ]; then note "Check that each of these runs only on the server (a service_role key bypasses Supabase RLS):"; echo "$hits" | sed 's/^/        /'
  else ok "No service_role or Stripe secret key names found outside server folders."; fi
else note "No src/, app/, pages/, components/, public/ or lib/ folder found; skipped."; fi

# 5. Live key patterns committed anywhere in tracked files
echo "5. Live API key patterns in tracked files"
keys=$(git grep -nIE '(sk_live_[0-9A-Za-z]{10,}|rk_live_[0-9A-Za-z]{10,}|sk-ant-[0-9A-Za-z_-]{20,}|sk-proj-[0-9A-Za-z_-]{20,}|AKIA[0-9A-Z]{16}|ghp_[0-9A-Za-z]{30,}|github_pat_[0-9A-Za-z_]{30,}|xox[bp]-[0-9A-Za-z-]{10,})' -- . ':!*.lock' ':!pre-ship-quick-scan.sh' 2>/dev/null | cut -c1-120 | head -20 || true)
if [ -n "$keys" ]; then bad "Possible live keys (rotate them, then remove from git history):"; echo "$keys" | sed 's/^/        /'
else ok "No common live key patterns found in tracked files."; fi

echo
echo "Result: $fail fail, $warn warn (of 5 checks)."
echo "This covers secrets only. Auth, database access (RLS), payments webhooks, cost limits and backups"
echo "are in the full pre-launch checklist in the Ship-It Kit: https://whichaicoder.com/products/"
[ "$fail" -eq 0 ] || exit 1
