#!/bin/bash
# Build and run lf from the current repo's source tree.
# Usage: scripts/dev-lf [args...]   e.g.  scripts/dev-lf design
#
# Run directly — do NOT use `uv run scripts/dev-lf` (uv mangles args
# and breaks TTY passthrough for interactive agents).
set -e
set -o pipefail

REPO="$(git rev-parse --show-toplevel)"
export RUST_LOG="${RUST_LOG:-lf=debug,loopflow=debug}"
cargo build -p loopflow --bin lf --manifest-path "$REPO/Cargo.toml" </dev/null 2>&1 | grep -E '^   Compiling|^    Finished|^error' >&2
echo "running: $REPO/target/debug/lf $*" >&2

# Mirror the lf() shell function so directives (cd after land, wt create) work.
# dev-lf runs as a child process so it can't change the parent shell's cwd,
# but it prints the directive so the user can copy-paste.
directive_file="$(mktemp)"
exit_code=0
LOOPFLOW_DIRECTIVE_FILE="$directive_file" "$REPO/target/debug/lf" "$@" || exit_code=$?

if [[ -s "$directive_file" ]]; then
    while IFS= read -r line; do
        echo "$line" >&2
    done < "$directive_file"
fi

rm -f "$directive_file"
exit "$exit_code"
