#!/bin/sh
set -e

# For example, put this in x.gdb
#   break adns__revparse_label
#   break adns__revparse_done
#   run
# and then
#   ADNS_TEST_DEBUG='./gdbwrap -n x.gdb' ./r1test tcpptr
#
# gdbwrap can be passed arguments like
#    *.gdb     passed to gdb with -x
#    -n        exit with exit status 5 which makes r1test think "skipped"
#
# other arguments are taken to be the program name provided by
# whatever has been told to use gdbwrap (eg r1test)

while true; do
	case "$1" in
	*.gdb)	  xgdb="-x $1"; shift;;
	-n)	  x5='exit 5'; shift;;
	*)	  break;;
	esac
done

exe=$1; shift

exec 5<&0 6>&1 7>&2
exec 0<>/dev/tty 1>&0 2>&0

set +e
ex="set args <&5 >&6 2>&7"
for a in "$@"; do
	ex="$ex '$a'"
done
gdb --return-child-result -ex "$ex" $xgdb $exe
$x5
