#!/usr/bin/env bash
# Start the MIDI listener (foreground — the viewer streams stdout live).
# Run via the node's ▶ button → /api/run-script {stream:true}.
# Kills any already-running listener first so events.jsonl never has two writers.
set -uo pipefail
HARNESS="$HOME/Documents/midi-harness"
cd "$HARNESS" || { echo "repo not found: $HARNESS" >&2; exit 1; }

make build >/dev/null 2>&1 || true
[ -x bin/listener ] || { echo "bin/listener missing — make build failed (brew install rtmidi?)" >&2; exit 1; }

pkill -f "$HARNESS/bin/listener" 2>/dev/null || true

echo "starting listener — notes will scroll here as you play"
exec "$HARNESS/bin/listener" "$HARNESS/events.jsonl"
