#!/usr/bin/env bash
# Web vulnerability scanner using Nuclei with curated templates.
# Usage: bash scripts/web_vuln_scan.sh <url> [--severity critical,high]
set -euo pipefail

URL="${1:?Usage: $0 <url> [--severity critical,high]}"
SEVERITY="${2:-critical,high,medium}"

if ! command -v nuclei &>/dev/null; then
  echo "ERROR: nuclei not found. Install: go install github.com/projectdiscovery/nuclei/v3/cmd/nuclei@latest"
  exit 1
fi

echo "=== Web Vulnerability Scan: $URL ==="
echo "Severity filter: $SEVERITY"
echo ""

nuclei -u "$URL" \
  -severity "$SEVERITY" \
  -type http \
  -stats \
  -silent \
  -jsonl 2>/dev/null | while read -r line; do
    name=$(echo "$line" | jq -r '.info.name // "unknown"')
    sev=$(echo "$line" | jq -r '.info.severity // "unknown"')
    matched=$(echo "$line" | jq -r '.matched_at // ""')
    template=$(echo "$line" | jq -r '.template_id // ""')
    echo "  [$sev] $name — $matched ($template)"
done

echo ""
echo "Scan complete."
