#!/usr/bin/env bash
# Description: Unsafe usage scanner

# Preserve original behavior (no strict mode)
# Unified help handler
SCRIPT_NAME="$(basename "$0")"
DESC="$(grep -m1 '^# Description:' "$0" | sed 's/^# Description:[[:space:]]*//')"
print_help() { echo "Usage: $SCRIPT_NAME"; [ -n "$DESC" ] && echo "$DESC"; exit 0; }
case "${1:-}" in -h|--help|help) print_help ;; esac

find_repo_root() {
  local d
  d="$(cd "$(dirname "${BASH_SOURCE[0]}")"/.. && pwd)"
  while [ "$d" != "/" ]; do
    if [ -f "$d/Cargo.toml" ]; then echo "$d"; return; fi
    d="$(dirname "$d")"
  done
  echo "."
}
ROOT="$(find_repo_root)"; cd "$ROOT" || exit 1

if command -v cargo-geiger >/dev/null 2>&1; then
  cargo geiger || {
    echo '[fallback] cargo-geiger failed; grep for unsafe blocks:'
    grep -RIn --exclude-dir=target --exclude-dir=.git --exclude-dir=_fec_backup* '\\bunsafe[[:space:]]*{' src 2>/dev/null || echo 'No unsafe blocks found (grep)'
  }
else
  echo '[fallback] grep for unsafe blocks:'
  grep -RIn --exclude-dir=target --exclude-dir=.git --exclude-dir=_fec_backup* '\\bunsafe[[:space:]]*{' src 2>/dev/null || echo 'No unsafe blocks found (grep)'
fi
