#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
cd "$SCRIPT_DIR"

BIN="${BIN:-./main.exe}"
TTY_PATH="${TTY_PATH:-/tmp/ttyADC_data}"
LIB_DIR="${LIB_DIR:-$HOME/.local/lib}"

if [[ ! -x "$BIN" ]]; then
  echo "Binary '$BIN' not found or not executable. Run ./build_main.sh first." >&2
  exit 1
fi

if [[ -d "$LIB_DIR" ]]; then
  export LD_LIBRARY_PATH="${LIB_DIR}${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}"
fi

exec "$BIN" \
  profile:phase \
  clock:internal \
  internal_ref_hz:2000000 \
  start:di_syn2_rise \
  stop:di_syn2_fall \
  sample_clock_hz:max \
  range:5 \
  duration_ms:100 \
  packet_limit:0 \
  do1_toggle_per_frame \
  do1_raw_tty_marked \
  "tty:${TTY_PATH}" \
  "$@"
