#!/bin/sh

hostid=$1
target=$2
bpw=$3
cflags=$4
CC=cc

if [ $# -lt 3 ]; then
	echo
	echo "Usage:   bin/test.sh host-id target bytes-per-word [cflags]"
	echo "Example: bin/test.sh unix-armv6 unxav6 4"
	echo
	exit 1
fi

if [ ! -f tx-${hostid} ]; then
	echo "--- Triple test, building stage 0"
	bin/build.sh ${hostid} ${hostid}
	if [ $? != 0 ]; then exit 1; fi
fi
echo "--- Triple test, building stage 1"
ln -fs txemtsrc.t txemit.t  
ln -fs targets/cg${target}.t cg.t
ln -fs targets/tx${target}.t t3x.t
./tx-${hostid} txtrn tx-unix2
${CC} ${cflags} -DBPW=${bpw} -s -o tx-unix2 tx-unix2.s targets/unixlib.c && \
	rm tx-unix2.s
echo "--- Triple test, building stage 2"
./tx-unix2 txtrn tx-unix3
${CC} ${cflags} -DBPW=${bpw} -s -o tx-unix3 tx-unix3.s targets/unixlib.c && \
	rm tx-unix3.s
cmp tx-unix2 tx-unix3 && rm tx-unix2
echo So far, so good!
echo "--- Running test suite"
./tx-unix3 programs/test test
${CC} ${cflags} -DBPW=${bpw} -s -o test test.s targets/unixlib.c && rm test.s
ln -fs txemtbin.t txemit.t  
ln -fs cgtcvm.t cg.t
ln -fs txtcvm.t t3x.t
./test && rm -f tx-unix3 test
