#!/bin/sh
# m2k-minicom — launch modemu2k with binary mode set, driving minicom.
#
# Locates the modemu2k binary in this order so the script is runnable both
# from an installed location and directly from a source clone:
#   1. Sibling of this script (installed case: $bindir/modemu2k)
#   2. Common dev build dirs adjacent to scripts/ (_build-debug, builddir, build)
#   3. $PATH
#
# Any arguments passed to this script are forwarded to modemu2k. The
# script's own -e and -c appear last on the command line, so they
# override conflicting user-supplied -e / -c (getopt "last one wins").
# Useful for enabling verbose narration:
#     m2k-minicom -v 2>/tmp/m2k.log     # then `tail -f /tmp/m2k.log`
# modemu2k refuses -v in -c mode when stderr is a TTY (verbose would
# corrupt minicom's display), so the stderr redirect is required.

set -e

scriptdir=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
for candidate in \
  "$scriptdir/modemu2k" \
  "$scriptdir/../_build-debug/modemu2k" \
  "$scriptdir/../builddir/modemu2k" \
  "$scriptdir/../build/modemu2k"
do
  if [ -x "$candidate" ]; then
    modemu2k=$candidate
    break
  fi
done
: "${modemu2k:=modemu2k}"

exec "$modemu2k" "$@" -e "AT%B0=1%B1=1&W" -c "minicom -l -tansi -con -p %s"
