#!/bin/sh
#
# This script runs the nightly bootstrap tests.
# It is invoked directly from cron, so it runs
# without a proper environment (PATH, etc.) set up.
#
# NB. Although this file is under CVS control,
# the copy that is used by cron does not get
# automatically updated from the CVS repository.
# After any modifications, the master copy
# in /home/mercury/public/test_mercury/scripts
# should be updated manually.

#-----------------------------------------------------------------------------#

tmpfile=/tmp/run_test$$
tmpscript=/tmp/test_mercury$$
mail_subject=/tmp/test_mercury_mail$$
trap 'rm -f $tmpfile $tmpscript $mail_subject; exit 1' 1 2 3 13 15

#-----------------------------------------------------------------------------#

ulimit -c 0

C_COMPILER=gcc
BRANCH=
case $# in
	1)	HOST=$1 ;;
	2)	HOST=$1; C_COMPILER=$2 ;;
	3)	HOST=$1; C_COMPILER=$2; BRANCH=$3 ;;
	*)	echo "Usage: $0 host [c-compiler] [branch]" 1>&2; exit 1 ;;
esac

#-----------------------------------------------------------------------------#

if [ -d /home/mercury/public/. ]; then
	TOPDIR=/home/mercury/public
	PATH="$HOME/bin/$ARCH`awk '/^[^#]/{printf ":%s",$0;}' $TOPDIR/cron/.path`"
else
	TOPDIR=$HOME/public
fi
PATH="$TOPDIR/mercury-latest/$FULLARCH/bin:$PATH"
PATH="$TOPDIR/$HOST:$PATH"
PATH="$HOME/bin:$PATH"
export PATH

CVSROOT=/home/mercury1/repository
if [ -d $CVSROOT/. ]; then
	:
else
	CVSROOT=:pserver:guest@cvs.mercury.cs.mu.oz.au:$CVSROOT
fi
export CVSROOT

#-----------------------------------------------------------------------------#

# Redirect all output to $tmpfile to stop cron mailing
# content-free messages to mercury@cs.mu.oz.au.
{
tm_dir=$TOPDIR/test_mercury

[ -d $TOPDIR/test_mercury/scripts ] || mkdir -p $TOPDIR/test_mercury/scripts
cd $TOPDIR/test_mercury/scripts
cvs checkout mercury/tools/test_mercury
cp mercury/tools/test_mercury $tmpscript

cvs checkout mercury/tools/generate_index_html
cvs checkout mercury/config.guess

FULLARCH="`./mercury/config.guess`"
ARCH="`expr $FULLARCH : '\([^-]*\)-.*' `"

#-----------------------------------------------------------------------------#

release_branch=0.11
case "$BRANCH" in
    "")
        DAY=`date '+%e'`
        case $HOST in
	    taifun|mundroo)
	    	# For these hosts we only run 3 times a week;
		# twice during the week and once on the weekend.
		# During the week, test the main branch.
		# On the weekend, test the release branch.
		case `date '+%w'` in
			0|6)	BRANCH=$release_branch ;;
			*)	BRANCH=latest ;;
		esac
		;;
	    murlibobo|hg|hydra|mars)
		#
		# For these hosts:
		# On odd-numbered days, test the release branch.
		# On even-numbered days, test the main branch.
		#
		case $DAY in
			*[13579]) BRANCH=$release_branch ;;
			*)	  BRANCH=latest ;;
		esac
		;;
	    aral)
		#
		# Aral has limited disk space, and not having to expend it
		# on the release branch helps conserve it.
		#
	    	BRANCH=latest
		;;
	    *)
		#
		# For the remaining hosts, do the reverse:
		# On odd-numbered days, test the main branch.
		# On even-numbered days, test the release branch.
		#
		case $DAY in
			*[13579]) BRANCH=latest ;;
			*)	  BRANCH=$release_branch ;;
		esac
		;;
        esac
        ;;
esac

# While we are preparing for the release, test the release branch
# intensively -- only test the main branch on one host (the ROTD host).
#case $HOST in
#earth)	;;
#*)	BRANCH=$release_branch ;;
#esac
#
#-----------------------------------------------------------------------------#

echo run_test $HOST $C_COMPILER
echo branch $BRANCH

case "$C_COMPILER" in
	gcc)	logname="test-$BRANCH-$HOST" ;;
	*)	logname="test-$BRANCH-$HOST-$C_COMPILER" ;;
esac

[ -d $tm_dir/logs ] || mkdir -p $tm_dir/logs ]
[ -d $tm_dir/logs/old ] || mkdir -p $tm_dir/logs/old ]
[ -d $tm_dir/logs/old2 ] || mkdir -p $tm_dir/logs/old2 ]
mv -f $tm_dir/logs/old/$logname.out $tm_dir/logs/old2 2>/dev/null
mv -f $tm_dir/logs/old/$logname.out.errs $tm_dir/logs/old2 2>/dev/null
mv -f $tm_dir/logs/old/$logname.test_errs $tm_dir/logs/old2 2>/dev/null
mv -f $tm_dir/logs/$logname.out $tm_dir/logs/old 2>/dev/null
mv -f $tm_dir/logs/$logname.out.errs $tm_dir/logs/old 2>/dev/null
mv -f $tm_dir/logs/$logname.test_errs $tm_dir/logs/old 2>/dev/null
logfile="$tm_dir/logs/$logname.out"

if nice -5 sh $tmpscript $HOST $ARCH $FULLARCH $BRANCH $C_COMPILER \
		> $logfile 2>&1
then
	test_status="succeeded"
	fgrep '**' $logfile > $logfile.errs
	if [ -s $logfile.errs ]; then
		echo "test passed (with some errors supressed/ignored)"
		echo "log file summary in $logfile.errs"
	else
		rm -f $logfile.errs
		echo "test passed"
	fi
	if [ -s $logfile.test_errs ]; then
		test_status="FAILED"
		echo "Error: test passed but $logfile.test_errs not empty"
	else
		rm -f $logfile.test_errs
	fi
	echo
	INSTALL_DIR=`tail $logfile | \
		awk '/^Installation directory:/ { print $3; }' `
	cd $INSTALL_DIR/lib/mercury/bin/$FULLARCH
	ls -l mercury_compile
	size mercury_compile
	{
		echo $FULLARCH $BRANCH $HOST $C_COMPILER
		ls -l mercury_compile
		size mercury_compile
		echo
	} >> $tm_dir/logs/sizes
else
	test_status="FAILED"
	fgrep '**' $logfile > $logfile.errs
	echo test FAILED
	echo log file in $logfile
	echo log file summary in $logfile.errs
	if [ -s $logfile.test_errs ]; then
		echo test failure summary in $logfile.test_errs
	fi
fi

if [ -s $logfile.errs -o -s $logfile.test_errs ]; then
	if [ -s $logfile.errs ]; then
		# Failures building the compiler or library should
		# occur early in the log summary.
		echo first 10 lines of log file summary:
		head -10 $logfile.errs
		echo
		echo
	fi
	if [ -s $logfile.test_errs ]; then
		echo first 100 test failures:
		grep "FAILED TEST" $logfile.test_errs | head -100 -
	fi
elif [ $test_status = "FAILED" ]; then
	echo
	echo last 20 lines of log file:
	tail -20 $logfile
fi

# We can't just set a variable containing the mail subject string
# because sh (but not bash) executes compound commands with redirection
# in a sub-shell.
echo "test_mercury $test_status: $BRANCH $HOST $C_COMPILER" > $mail_subject
} > $tmpfile 2>&1

# NB we can't just pipe into `mutt' because the $mail_subject file
# needs to be written before `mutt' is started.
mutt -s "`cat $mail_subject`" fjh@cs.mu.oz.au < $tmpfile
rm -f $tmpfile $tmpscript $mail_subject

#-----------------------------------------------------------------------------#
