#!/usr/bin/env bash
# Quick verifier to ensure this repository contains only AutoNAS artifacts
# Scans for common autoSMART markers and suspicious files.

set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$ROOT_DIR"

echo "[verify-project] Repository: $ROOT_DIR"

patterns=(
  'autoSMART'
  'autosmart'
  'smart-collector'
  'SmartCollector'
  'smartctl .*postgres'  # autosmart DB pipelines pattern
)

exclude_dirs=(
  '.git'
  '.venv'
  'node_modules'
  'dist'
  'build'
)

found=0

# Build a find command that prunes excluded directories
find_cmd=(find .)
for d in "${exclude_dirs[@]}"; do
  find_cmd+=( -path "*/$d/*" -prune -o )
done
find_cmd+=( -type f ! -path "*/scripts/verify-project.sh" -print0 )

for pat in "${patterns[@]}"; do
  if "${find_cmd[@]}" | xargs -0 grep -EIn --color=never "$pat" > /tmp/_verify_hits 2>/dev/null; then
    if [ -s /tmp/_verify_hits ]; then
      echo "[verify-project] Suspicious matches for pattern: $pat"
      cat /tmp/_verify_hits
      echo ""
      found=1
    fi
  fi
done

rm -f /tmp/_verify_hits || true

# Also check for suspicious filenames accidentally copied over
suspect_files=(
  'smart-collector-daemon.pl'
  'deploy-production.sh'
  'monitor-cluster.sh'
)

for name in "${suspect_files[@]}"; do
  if find . -type f -name "$name" | grep -q .; then
    echo "[verify-project] Suspicious file present: $name"
    found=1
  fi
done

if [ "$found" -eq 0 ]; then
  echo "[verify-project] OK: No autoSMART contamination detected."
  exit 0
else
  echo "[verify-project] WARNING: Potential contamination detected. Review above matches."
  exit 2
fi
