set shell := ["bash", "-cu"]

BIN := "target/debug/koan"
CAPS := "cap_bpf,cap_perfmon,cap_sys_ptrace,cap_sys_admin,cap_sys_resource+eip"

# Grant capabilities so Koan can use eBPF without root.
caps:
    @if [[ "{{BIN}}" == *"/release/"* ]]; then cargo build --release; else RUSTFLAGS="-C debuginfo=1 -C force-frame-pointers=yes" cargo build; fi
    @if [ ! -e "{{BIN}}" ]; then echo "binary not found: {{BIN}}"; exit 1; fi
    sudo setcap {{CAPS}} "{{BIN}}"
    getcap "{{BIN}}"

# Build and set caps (debug by default).
build:
    @just caps

# Convenience alias for release builds.
caps-release:
    @just BIN=target/release/koan caps

# Build and set caps for release builds.
build-release:
    @just BIN=target/release/koan caps

# Remove capabilities from the binary.
caps-clear:
    @if [ ! -e "{{BIN}}" ]; then echo "binary not found: {{BIN}}"; exit 1; fi
    sudo setcap -r "{{BIN}}"
    @getcap "{{BIN}}" || true

# Reminder about memlock limits (ulimit does not persist outside this shell).
memlock-hint:
    @echo "Set memlock for your current shell: ulimit -l 262144 (or unlimited if allowed)."
    @echo "For systemd, set LimitMEMLOCK=infinity and AmbientCapabilities=CAP_BPF CAP_PERFMON CAP_SYS_RESOURCE."

# Run all tests.
test:
    cargo test --all -- --nocapture

# Run all benches.
bench:
    cargo bench --all
