set shell := ["bash", "-euo", "pipefail", "-c"]

COPIES := env_var_or_default("COPIES", "50")

default: benchmark

generate:
    chmod +x generate.sh
    ./generate.sh {{COPIES}}

ensure-rescript-bin:
    yarn install

build: ensure-rescript-bin
    yarn build

clean:
    rm -rf src lib node_modules

# Full benchmark: generate, build, then time analysis.
benchmark: generate build
    @echo ""
    @echo "=== Benchmark: {{COPIES}} copies (~$$(find src -name '*.res' | wc -l) files) ==="
    @echo ""
    @echo "Sequential:"
    @dune exec rescript-tools -- reanalyze -config -ci -timing 2>&1 | grep -E "Analysis reported|=== Timing|CMT processing|File loading|Analysis:|Merging|Solving|Reporting:|Total:"

# Just time analysis (assumes already built).
time:
    @echo "Sequential:"
    @dune exec rescript-tools -- reanalyze -config -ci -timing 2>&1 | grep -E "Analysis reported|=== Timing|CMT processing|File loading|Analysis:|Merging|Solving|Reporting:|Total:"

# Benchmark with CMT cache.
time-cache: generate build
    @echo "=== Without cache ==="
    @echo "Sequential:"
    @dune exec rescript-tools -- reanalyze -config -ci -timing 2>&1 | grep -E "=== Timing|CMT processing|File loading|Total:"
    @echo ""
    @echo "=== With CMT cache (first run - cold) ==="
    @echo "Sequential:"
    @dune exec rescript-tools -- reanalyze -config -ci -timing -cmt-cache 2>&1 | grep -E "=== Timing|CMT processing|File loading|Total:"
    @echo ""
    @echo "=== With CMT cache (second run - warm) ==="
    @echo "Sequential:"
    @dune exec rescript-tools -- reanalyze -config -ci -timing -cmt-cache 2>&1 | grep -E "=== Timing|CMT processing|File loading|Total:"

# Benchmark reactive mode (simulates repeated analysis).
time-reactive: generate build
    @echo "=== Reactive mode benchmark ==="
    @echo ""
    @echo "Standard (baseline):"
    @dune exec rescript-tools -- reanalyze -config -ci -timing 2>&1 | grep -E "=== Timing|CMT processing|File loading|Total:"
    @echo ""
    @echo "Reactive mode (3 runs - first is cold, subsequent are warm):"
    @dune exec rescript-tools -- reanalyze -config -ci -timing -reactive -runs 3 >/dev/null

secret-scan-trufflehog:
    @command -v trufflehog >/dev/null && trufflehog filesystem . --only-verified || true
