#!/bin/sh
# $Id: run,v 1.2.1.2 1996/09/27 11:50:13 drh Exp $
# run .../target/os/tst/foo.s [ remotehost ]

# set -x
target=`echo $1 | awk -F/ '{ print $(NF-3) }'`
os=`echo $1 | awk -F/ '{ print $(NF-2) }'`
dir=$target/$os; idir=include/$dir
remotehost=${2-$REMOTEHOST}

case "$1" in
*symbolic/*/*.s) idir=include/mips/$os; remotehost=noexecute
esac

if [ ! -d "$target/$os" -o ! -d "$idir" ]; then
	echo 2>&1 $0: unknown combination '"'$target/$os'"'
	exit 1
fi

C=`basename $1 .s`
BUILDDIR=${BUILDDIR-./} LCC="${LCC-${BUILDDIR}/lcc} -Wo-lccdir=$BUILDDIR"
TSTDIR=${TSTDIR-${BUILDDIR}/$dir/tst}
if [ ! -d $TSTDIR ]; then mkdir -p $TSTDIR; fi

echo ${BUILDDIR}/rcc -target=$target/$os $1: 1>&2
$LCC -N -S -B${BUILDDIR}/ -I$idir -Ualpha -Usun -Uvax -Umips -Ux86 \
	-Wf-errout=$TSTDIR/$C.2 -D$target -Wf-g0 \
	-Wf-target=$target/$os -o $1 tst/$C.c
if [ $? != 0 ]; then remotehost=noexecute; fi
if [ -r $dir/tst/$C.2bk ]; then
	diff $dir/tst/$C.2bk $TSTDIR/$C.2
fi
if [ -r $dir/tst/$C.sbk ]; then
	if diff $dir/tst/$C.sbk $TSTDIR/$C.s; then exit 0; fi
fi

case "$remotehost" in
noexecute)	exit 0 ;;
""|"-")	echo "   executing" $C: 1>&2
	$LCC -o $TSTDIR/$C $1 -lm; $TSTDIR/$C <tst/$C.0 >$TSTDIR/$C.1 ;;
*)	echo "   executing" $C on $remotehost: 1>&2
	rcp $1 $remotehost:
	if expr "$remotehost" : '.*@' >/dev/null ; then
		remotehost="`expr $remotehost : '.*@\(.*\)'` -l `expr $remotehost : '\(.*\)@'`"
	fi
	rsh $remotehost "cc -o $C $C.s -lm;./$C;rm -f $C $C.[so]" <tst/$C.0 >$TSTDIR/$C.1
	;;
esac
if [ -r $dir/tst/$C.1bk ]; then
	diff $dir/tst/$C.1bk $TSTDIR/$C.1
	exit $?
fi
exit 0
