#!/usr/bin/env bash
# route_test — run Cloverleaf route_test for a thread against a message file.
# Short, directly-invokable wrapper over nc-engine.sh route-test.
# NEEDS A LIVE ENGINE (hciroutetest). On a static fixture it dry-prints the cmd.
#
#   route_test <thread> <msgfile>                # positional (v1 form)
#   route_test --source-thread <thread> <msgfile># named form (completes thread)
#   route_test --source-thread <thread> --file <msgfile>
#
# Any extra flags (e.g. --dry-run, confirm=yes) pass through to nc-engine.
set -o pipefail
_self="${BASH_SOURCE[0]}"; [ -L "$_self" ] && _self="$(readlink "$_self")"
. "$(cd "$(dirname "$_self")" && pwd)/_nc_common.sh"

if [ "${1:-}" = "-h" ] || [ "${1:-}" = "--help" ]; then
  awk 'NR==1{next} /^#/{sub(/^# ?/,""); print; next} {exit}' "${BASH_SOURCE[0]}"
  exit 0
fi

lib="$(_nc_resolve_lib)" || { echo "route_test: lib/ toolkit not found (set LARRY_LIB_DIR or LARRY_HOME)" >&2; exit 1; }

# Accept either positional <thread> <file> or the named --source-thread/--file
# forms (the named form is what tab-completion targets). Unrecognized flags are
# collected and passed through to nc-engine route-test.
thread=""; file=""; pass=()
while [ $# -gt 0 ]; do
  case "$1" in
    --source-thread|--thread) shift; thread="$1" ;;
    --file)                   shift; file="$1" ;;
    -*)                       pass+=("$1") ;;
    *) if [ -z "$thread" ]; then thread="$1"
       elif [ -z "$file" ]; then file="$1"
       else pass+=("$1"); fi ;;
  esac
  shift
done

[ -n "$thread" ] || { echo "route_test: missing thread (route_test <thread> <msgfile>)" >&2; exit 2; }
[ -n "$file" ]   || { echo "route_test: missing message file (route_test <thread> <msgfile>)" >&2; exit 2; }
exec bash "$lib/nc-engine.sh" route-test "$thread" "$file" "${pass[@]}"
