#!/bin/sh
# ../run target os test [ remotehost ]

# set -x
target=$1 os=$2 remotehost=${4-$REMOTEHOST}

cd tst
if [ ! -r "../../../include/${target}_$os" ]; then
	echo 2>&1 $0: unknown target-system combination '"'$target-$os'"'
fi
case "$target-$os" in
mips-ultrix)	TARGET=mipsel ;;
mc-next)	TARGET=next ;;
*)		TARGET=$target ;;
esac

echo $HOST-$HOSTOS/rcc $target-$os $3: 1>&2
lcc -N -S -B../../$HOST-$HOSTOS/ -I../../../include/${target}_$os -Usun -Uvax -Umips \
	-D$target -Wf-g0 -Wftarget=$TARGET ../../../tst/$3.c 2>$3.2
if [ $? != 0 ]; then remotehost=noexecute; fi
diff ../../../tst/$3.2 $3.2
if [ ! -r $3.s.bak ]; then
	mv $3.s $3.s.bak
	cp $3.s.bak $3.s
else
	if diff $3.s.bak $3.s; then exit 0; fi
fi

case "$remotehost" in
noexecute)	exit 0 ;;
""|"-")	lcc -o $3 $3.s -lm; ./$3 <../../../tst/$3.0 >$3.1 ;;
*)	rcp $3.s $remotehost:
	if expr "$remotehost" : '.*@' >/dev/null ; then
		remotehost="`expr $remotehost : '.*@\(.*\)'` -l `expr $remotehost : '\(.*\)@'`"
	fi
	rsh $remotehost "cc -o $3 $3.s -ll -lm;./$3;rm $3 $3.[so]" <../../../tst/$3.0 >$3.1
	;;
esac
if [ ! -r $3.1.bak ]; then
	mv $3.1 $3.1.bak
	cp $3.1.bak $3.1
	exit 0
fi
diff $3.1.bak $3.1
exit $?
