#!/usr/bin/env bash
# vaken — single-step verification: fmt --check, clippy -D warnings, build.
#
# This is the pre-push gate. CI runs the same three things (plus
# `cargo bundle`, which we skip locally because it's slow and depends
# on cargo-bundle being installed). Adding `--bundle` runs that too.
#
# Usage: ./scripts/verify.sh [--bundle]

set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
VAKEN_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"

RUN_BUNDLE=false
while [ $# -gt 0 ]; do
  case "$1" in
    --bundle) RUN_BUNDLE=true; shift ;;
    -h|--help) sed -n '1,10p' "$0"; exit 0 ;;
    *) echo "Unknown flag: $1" >&2; exit 1 ;;
  esac
done

cd "$VAKEN_DIR"

PASS=()
FAIL=()

run_step() {
  local name="$1"; shift
  echo ""
  echo "== $name =="
  if "$@"; then
    PASS+=("$name")
  else
    FAIL+=("$name")
  fi
}

run_step "fmt"    cargo fmt --all -- --check
run_step "clippy" cargo clippy --all-targets -- -D warnings
run_step "build"  cargo build --release

if [ "$RUN_BUNDLE" = true ]; then
  if ! command -v cargo-bundle >/dev/null 2>&1; then
    echo ""
    echo "cargo-bundle not installed. Install with: cargo install cargo-bundle --locked"
    FAIL+=("bundle")
  else
    run_step "bundle" cargo bundle --release
  fi
fi

echo ""
echo "=============================="
if [ ${#FAIL[@]} -eq 0 ]; then
  echo "  verify: OK (${PASS[*]})"
  echo "=============================="
  exit 0
else
  echo "  verify: FAIL"
  echo "  passed: ${PASS[*]:-none}"
  echo "  failed: ${FAIL[*]}"
  echo "=============================="
  exit 1
fi
