#!/bin/sh
#---------------------------------------------------------------------------#
# Copyright (C) 1996-1999 The University of Melbourne.
# This file may only be copied under the terms of the GNU General
# Public License - see the file COPYING in the Mercury distribution.
#---------------------------------------------------------------------------#

# This script is used to run the nightly tests.
# It is invoked from the `run_test' script.

echo "test_mercury starting at `date`" 1>&2

case $# in
	3)	HOST=$1; ARCH=$2; FULLARCH=$3 ;;
	*)	echo "Usage: $0 host arch fullarch" 1>&2; exit 1 ;;
esac

PATH="$HOME/bin/$ARCH`awk '/^[^#]/{printf ":%s",$0;}' /home/pgrad/fjh/.path`"
PATH="/home/mercury/public/mercury-0.8/$FULLARCH/bin:$PATH"
PATH="/home/mercury/public/mercury-latest/$FULLARCH/bin:$PATH"
PATH="/home/$HOST/public/mercury-latest/$FULLARCH/bin:$PATH"
PATH="/home/mercury/public/nuprolog/$FULLARCH/bin:$PATH"
PATH="/home/mercury/public/gcc-2.7.2/$FULLARCH/bin:$PATH"
PATH="/home/mercury/public/egcs-1.1b/$FULLARCH/bin:$PATH"
PATH="/home/mercury/public/autoconf-2.4/$FULLARCH/bin:$PATH"
PATH="/home/mercury/public/sicstus3/$HOST:$PATH"
PATH="/home/mercury/public/$FULLARCH:$PATH"
PATH="/home/mercury/public/$HOST:$PATH"
export PATH

CVSROOT=/home/staff/zs/imp
export CVSROOT

# tell gcc to use /tmp, not /usr/tmp
TMPDIR=/tmp
export TMPDIR

# Make the files writable by group mercury.
umask 002

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

DATE=`date '+%Y-%m-%d'`

RELEASE_VERSION=rotd-$DATE
CHECKOUT_OPTS=-A

# To build one of the official releases, uncomment the following two lines
# which specify the release number and the cvs checkout option needed to
# retrieve it.
### RELEASE_VERSION=0.8.1
### CHECKOUT_OPTS=-rversion-0_8_1

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

case $HOST in
	# test things at different optimization levels...
	murlibobo)	OPTIMIZE=-O5 ;;
	kryten) 	OPTIMIZE=-O1 ;;
	taifun) 	OPTIMIZE=-O1 ;;
	rimmer) 	OPTIMIZE=-O1 ;;
	quicksilver) 	OPTIMIZE=-O3 ;;
	hg) 		OPTIMIZE=-O4 ;;
	hydra)		OPTIMIZE=-O2 ;;
	munta)		OPTIMIZE=-O0 ;;
esac

# on hg, the file system we're using does not support links
case $HOST in
	hg)		LN="cp -r"; LN_S="cp -r" ;;
	*)		LN="ln"; LN_S="ln -s" ;;
esac

TEST_MCFLAGS="$OPTIMIZE --opt-space"
TEST_CFLAGS=""

case $FULLARCH in
	i?86-*-linux*)
		# On x86 Linux, we need to set the following flags to enable
		# shared libraries (and we need to use shared libraries to
		# avoid running out of disk space...)
		TEST_MGNUCFLAGS=--pic-reg
		TEST_MLFLAGS=--shared
		;;
	*)
		# On other platforms shared libraries is the default
		TEST_MGNUCFLAGS=""
		TEST_MLFLAGS=""
		;;
esac

# directories to use

TESTDIR=/home/mercury/public/test_mercury
DIR=$TESTDIR/test_dirs/$HOST

case $HOST in
	hg)	INSTALL_DIR=/home/hg/public/mercury-latest/$FULLARCH ;;
	*)	INSTALL_DIR=/home/mercury/public/mercury-latest/$FULLARCH ;;
esac

BETA_FTPHOST=ftp.mercury.cs.mu.oz.au
BETA_FTPDIR=/home/ftp/pub/mercury/beta-releases
BETA_WEBDIR=/local/dept/w3/unsupported/docs/mercury/download/files/beta-releases

STABLE=$DIR/mercury-latest-stable
UNSTABLE=$DIR/mercury-latest-unstable

case $ARCH in
	alpha)
		# due to a bug in the DEC loader, INSTALL_DIR should be
		# as short as possible, to avoid overflow of fixed length
		# buffers for -rpath options. The above definition is too long.
		INSTALL_DIR=/home/mercury/public/.a	# for mercury-latest
		### INSTALL_DIR=/home/mercury/public/.b	# for 0.8
		;;
esac

PATH="$INSTALL_DIR/bin:$PATH"

# $PARALLEL: flag to pass to GNU make for parallel make
PARALLEL=
case $HOST in
	kryten) 	PARALLEL=-j2 ;; # out of three CPUs
	rimmer) 	PARALLEL=-j2 ;; # out of two CPUs
	munta)		PARALLEL=-j2 ;; # out of four CPUs
	mundook) 	PARALLEL=-j1 ;; # out of two CPUs
	murlibobo)	PARALLEL=-j6 ;; # out of eight CPUs
	hydra)		PARALLEL=-j2 ;; # out of two CPUs
	quicksilver) 	PARALLEL= ;; 	# one CPU
	hg) 		PARALLEL= ;; 	# one CPU
	*)		PARALLEL= ;;
esac

# version of the mercury compiler to use for bootstrapping
case $HOST in
	hg)
		BOOTSTRAP_MERCURY_COMPILER=/home/$HOST/public/mercury-latest/$FULLARCH/lib/mercury/bin/$FULLARCH/mercury_compile
		;;
	*)
		BOOTSTRAP_MERCURY_COMPILER=/home/mercury/public/mercury-latest/$FULLARCH/lib/mercury/bin/$FULLARCH/mercury_compile
		;;
esac

# df (disk free) command
DF=df
case $HOST in
	kryten|taifun|rimmer)	DF="df -k" ;;
esac

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

# check we've got a reasonable amount of free disk space -- no point
# starting if we'll only run out of disk space.

[ -d $DIR ] || mkdir -p $DIR
FREE=`$DF $DIR | awk '
	NR == 2 && NF > 4 { print $4; exit; }
	NR == 3 { print $3; }
'`
if [ "$FREE" -lt 10000 ]; then
	echo "Insufficient disk space on $DIR" 1>&2
	$DF $DIR
	exit 1
fi

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

# to make sure we don't try to run two tests in parallel,
# we use a lock file in the test directory

lockfile=$DIR/lockfile
if [ -f $lockfile ]; then
	echo "Directory $DIR is locked:" 1>&2
	cat $lockfile 1>&2
	echo "Perhaps the previous test is still running?" 1>&2
	echo "(Remove $lockfile manually if necessary.)" 1>&2
	exit 1
fi
trap 'rm -f $lockfile; exit 1' 1 2 3 13 15
trap 'exit_status=$?; rm -f $lockfile; exit $exit_status' 0
echo $HOST > $lockfile

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

status=0

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

case $HOST in
	murlibobo)
		CONFIG_OPTS="--enable-all-grades" ;;
	kryten)		
		# On kryten & taifun, there is a bug in readline which causes
		# it to go into an infinite loop when reading from a file.
		#CONFIG_OPTS="--enable-all-grades --without-readline" ;;
		CONFIG_OPTS="--without-readline" ;;
	taifun)		
		# On kryten & taifun, there is a bug in readline which causes
		# it to go into an infinite loop when reading from a file.
		CONFIG_OPTS="--without-readline" ;;
	munta)		
		# On munta, we need to disable the external debugger,
		# because it uses sockets, and munta doesn't have
		# the static versions of the socket libraries
		# installed.  Likewise for the -non_shared version
		# of the dl library.  Unfortunately our configure script
		# doesn't detect these problems, so we need to pass these
		# options manually.
		CONFIG_OPTS="--disable-extern-debug --disable-dynamic-link" ;;
	*)	
		CONFIG_OPTS="" ;;
esac

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

# Sometimes, bootstrapping problems mean that we need to install
# even if the bootstrap check failed.

install_anyway=false

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

: checkout sources and run bootcheck

echo "test_mercury starting cvs checkout at `date`" 1>&2

set -x	# trace execution

: checkout the sources
cd $DIR || { false; exit 1; }
cvs checkout $CHECKOUT_OPTS mercury tests || { false; exit 1; }
case $HOST in
	murlibobo|kryten|taifun|quicksilver|hg|hydra)
		[ -d mercury/extras/clpr/CVS ] ||
		(cd mercury/extras && cvs checkout $CHECKOUT_OPTS clpr) ||
			{ false; exit 1; }
		;;
esac

: make sure the installation directory exists
[ -d $INSTALL_DIR ] || mkdir -p $INSTALL_DIR

: update the VERSION file to specify the version we are building
sed	"s/VERSION=.*/VERSION=$RELEASE_VERSION/" \
	mercury/VERSION > mercury/VERSION.new
mv mercury/VERSION.new mercury/VERSION

: bootstrap the compiler up to stage 3 and check that the results match
MERCURY_COMPILER=$BOOTSTRAP_MERCURY_COMPILER
export MERCURY_COMPILER
cd mercury || { false; exit 1; }

# if [ -s tools/list.$HOST ]; then
# 	tools/expand_params `tools/cur_param tools $HOST` > Mmake.params
# else
	echo "MC = ../scripts/mmc" > Mmake.params
	echo "EXTRA_MCFLAGS = $TEST_MCFLAGS" >> Mmake.params
	echo "EXTRA_CFLAGS = $TEST_CFLAGS" >> Mmake.params
# fi

autoconf || { false; exit 1; }
rm -f config.cache
./configure --prefix=$INSTALL_DIR $CONFIG_OPTS || { false; exit 1; }
mmake depend $PARALLEL || { false; exit 1; }
mmake realclean MMAKEFLAGS=$PARALLEL || { false; exit 1; }
./configure --prefix=$INSTALL_DIR $CONFIG_OPTS || { false; exit 1; }
mmake depend $PARALLEL || { false; exit 1; }
version=`mmake version` || { false; exit 1; }
fullarch=`mmake fullarch` || { false; exit 1; }
tools/bootcheck -r -p -t $PARALLEL || $install_anyway || { false; exit 1; }
cd .. || { false; exit 1; }

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

# Tests can fail if linked against an old library, and compiled
# with a new compiler, so we'll install the libraries first (so
# long as we bootcheck), then run all the tests in all the grades.
#
# Eventually, when we add more support for building multiple grades,
# we can test in all grades _before_ installing, but presently this
# is quite difficult.

: install the compiler

echo "test_mercury starting installation at `date`" 1>&2
echo "current directory `pwd`" 1>&2

case $status in 0)
    case $install_anyway in
        false)	cd mercury/stage2 || { false; exit 1; }
		# we need to use the stage1 compiler to do the install
		MERCURY_COMPILER=$DIR/mercury/compiler/mercury_compile
		export MERCURY_COMPILER
		;;
        true)	cd mercury || { false; exit 1; } ;;
    esac

    case $HOST in
	munta)
		# see README.IRIX for an explanation of these contortions
		
		mmake install LIBGRADES="asm_fast.gc.prof asm_fast" \
			MMAKEFLAGS=$PARALLEL || status=1

		# mmake install_split_library LIBGRADES= MMAKEFLAGS=$PARALLEL
		#
		# the above line doesn't work, due to a bug in gcc 2.6.3;
		# the following hack avoids the bug by compiling modules.o
		# with -O1 rather than -O2
		#
		(cd library && \
		\
		PATH=../scripts:../util:$PATH \
		MMAKE_VPATH=. MMAKE_DIR=../scripts \
		../scripts/mmake EXTRA_CFLAGS="$TEST_CFLAGS -O1" \
		int.dir/*.o && \
		\
		PATH=../scripts:../util:$PATH \
		MMAKE_VPATH=. MMAKE_DIR=../scripts \
		../scripts/mmake install_split_library \
		) || status=1

		: install the shared library grades
		mmake install_grades LIBGRADES="reg.gc reg.gc.prof" \
			MMAKEFLAGS="$PARALLEL EXT_FOR_SHARED_LIB=so" || status=1

		: must reinstall a non-shared libgc.a and libgc_prof.a
		(cd boehm_gc && \
		mmake install MMAKEFLAGS=$PARALLEL && \
		mmake clean MMAKEFLAGS=$PARALLEL && \
		mmake install GRADE=asm_fast.gc.prof PROF=_prof \
		 	MMAKEFLAGS=$PARALLEL \
		) || status=1
		;;
	*)
		mmake install MMAKEFLAGS=$PARALLEL || status=1
		mmake install_split_library LIBGRADES= MMAKEFLAGS=$PARALLEL \
			|| status=1
		;;
    esac
    case $install_anyway in
        false)	cd ../.. || status=1 ;;
        true)	cd .. || status=1 ;;
    esac
esac


[ -d $TESTDIR/logs ] || mkdir -p $TESTDIR/logs
date >> $TESTDIR/logs/install.$HOST

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

: test and install the CLPR interface
case $HOST in
	murlibobo)
		# requires --enable-all-grades
		GRADES="asm_fast.gc.tr asm_fast.tr"
		;;
	hg|quicksilver|hydra|kryten|taifun)
		GRADES="asm_fast.gc.tr"
		;;
	*)
		GRADES=""
		;;
esac
for grade in $GRADES kludge_for_broken_shells
do
	if [ "$grade" != kludge_for_broken_shells ]; then
		echo "test clpr stuff for grade $grade" 1>&2
		(cd mercury/extras/clpr &&
			 mmake realclean $PARALLEL MMAKEFLAGS=$PARALLEL &&
			 mmake depend	 $PARALLEL MMAKEFLAGS=$PARALLEL \
				GRADE=$grade \
				EXTRA_MCFLAGS="$TEST_MCFLAGS" \
				EXTRA_CFLAGS="$TEST_CFLAGS" &&
			 mmake 		 $PARALLEL MMAKEFLAGS=$PARALLEL \
				GRADE=$grade \
				EXTRA_MCFLAGS="$TEST_MCFLAGS" \
				EXTRA_CFLAGS="$TEST_CFLAGS" &&
			 mmake check	 $PARALLEL MMAKEFLAGS=$PARALLEL \
				GRADE=$grade \
				EXTRA_MCFLAGS="$TEST_MCFLAGS" \
				EXTRA_CFLAGS="$TEST_CFLAGS" &&
			 mmake install	 $PARALLEL MMAKEFLAGS=$PARALLEL \
				GRADE=$grade \
				EXTRA_MCFLAGS="$TEST_MCFLAGS" \
				EXTRA_CFLAGS="$TEST_CFLAGS" &&
			 mmake realclean $PARALLEL MMAKEFLAGS=$PARALLEL) ||
				status=1
		echo "test trailed_update stuff for grade $grade" 1>&2
		(cd mercury/extras/trailed_update &&
			 mmake realclean $PARALLEL MMAKEFLAGS=$PARALLEL &&
			 mmake depend	 $PARALLEL MMAKEFLAGS=$PARALLEL \
				GRADE=$grade \
				EXTRA_MCFLAGS="$TEST_MCFLAGS" \
				EXTRA_CFLAGS="$TEST_CFLAGS" &&
			 mmake 		 $PARALLEL MMAKEFLAGS=$PARALLEL \
				GRADE=$grade \
				EXTRA_MCFLAGS="$TEST_MCFLAGS" \
				EXTRA_CFLAGS="$TEST_CFLAGS" &&
			 mmake check	 $PARALLEL MMAKEFLAGS=$PARALLEL \
				GRADE=$grade \
				EXTRA_MCFLAGS="$TEST_MCFLAGS" \
				EXTRA_CFLAGS="$TEST_CFLAGS" &&
			 mmake realclean $PARALLEL MMAKEFLAGS=$PARALLEL) ||
				status=1
	fi
done

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

: create distribution

echo "test_mercury starting to create distribution at `date`" 1>&2

MERCURY_COMPILER=$BOOTSTRAP_MERCURY_COMPILER
export MERCURY_COMPILER
case $HOST in murlibobo)
	{ cd $DIR/mercury &&
	mmake realclean MMAKEFLAGS=$PARALLEL &&
	(cd $DIR/tests &&
	mmake realclean MMAKEFLAGS=$PARALLEL) &&
	: > Mmake.params &&
	rm -f so_locations &&
	autoconf &&
	mercury_cv_low_tag_bits=2 \
	mercury_cv_bits_per_word=32 \
	mercury_cv_unboxed_floats=no \
	sh configure --prefix=$INSTALL_DIR &&
	mmake MMAKEFLAGS='EXTRA_MCFLAGS="-O5 --opt-space" -j6' tar &&
	cd .. &&
	rm -f mercury-latest-unstable/* &&
	mv mercury-compiler-$version.tar.gz	\
	    mercury-latest-unstable/mercury-compiler-$version-unstable.tar.gz &&
	mv mercury-extras-$version.tar.gz	\
	    mercury-latest-unstable/mercury-extras-$version-unstable.tar.gz &&
	mv mercury-tests-$version.tar.gz	\
	    mercury-latest-unstable/mercury-tests-$version-unstable.tar.gz
	} || status=1
	# later parts of this script assume that we've already run configure,
	# so we need to redo that
	cd $DIR/mercury && sh configure --prefix=$INSTALL_DIR
	cd $DIR
	;;
esac

: run the regression tests on the stage2 compiler

# The stage2 compiler got installed, so we don't need to set this.
# In fact, we should unset it, so it isn't set to something
# inappropriate by some other part of this script.

unset MERCURY_COMPILER

#MERCURY_COMPILER=$DIR/mercury/stage2/compiler/mercury_compile
#export MERCURY_COMPILER
case $HOST in
	mundook|murlibobo)
		GRADES="none none.gc
			reg reg.gc
			asm_fast asm_fast.gc
			asm_fast.prof asm_fast.gc.prof
			asm_fast.gc.memprof
			asm_fast.gc.tr asm_fast.tr asm_fast.gc.prof.tr
			asm_fast.gc.tr.debug
			"
		;;
# requires --enable-all-grades
#	kryten)
#		GRADES="none jump fast asm_jump asm_fast none.gc
#			jump.gc fast.gc asm_jump.gc
#			asm_fast.gc asm_fast.gc.prof asm_fast.prof asm_fast.gc.memprof
#			asm_fast.gc.tr asm_fast.tr asm_fast.gc.prof.tr asm_fast.gc.tr.debug"
#		;;
	kryten|taifun|rimmer)
		GRADES="asm_fast.gc asm_fast asm_fast.gc.prof asm_fast.prof
			asm_fast.gc.memprof asm_fast.gc.tr
			asm_fast.gc.tr.debug"
		;;
	munta)
		GRADES="reg.gc asm_fast asm_fast.gc
			reg.gc.prof asm_fast.gc.prof"
		# Don't bother with these, otherwise it would take forever...
		#	reg.prof asm_fast.prof reg.gc.memprof reg.gc.tr reg.gc.tr.debug
		;;
	hg|hydra)
		GRADES="asm_fast.gc asm_fast
			asm_fast.gc.prof asm_fast.prof
			asm_fast.gc.memprof asm_fast.gc.tr asm_fast.gc.tr.debug"
		;;
	quicksilver)
		GRADES="asm_fast.gc asm_fast asm_fast.gc.tr.debug"
		# Don't bother with these, otherwise it would take forever...
		#	asm_fast.gc.prof asm_fast.prof
		#	asm_fast.gc.memprof asm_fast.gc.tr"
		;;
esac

cd tests || { false; exit 1; }

# We don't do this anymore, because we are phasing out the Prolog supprt.
#
# rebuild the `.exp' files on kryten only
# case $HOST in
# 	kryten) ./generate_exp ;;
# 	*) ;;
# esac

for grade in $GRADES
do
	echo "test_mercury starting tests for grade $grade at `date`" 1>&2
	./runtests -f "$TEST_MCFLAGS" -c "$TEST_CFLAGS" -g "$grade" \
		-m "$TEST_MGNUCFLAGS" -l "$TEST_MLFLAGS" \
		$PARALLEL || status=1
	case $grade in
		*.memprof*)
			# we need to link statically for profiling on DEC Alpha
			(cd benchmarks &&
			touch poly.m &&
			mmake poly.depend &&
			mmake poly \
				GRADE="$grade" \
				EXTRA_MCFLAGS="$TEST_MCFLAGS" \
				EXTRA_CFLAGS="$TEST_CFLAGS" \
				EXTRA_MLFLAGS="-static" &&
			./poly > poly.$grade.out &&
			mprof -v -m -c > poly.$grade.mprof 2>&1  &&
			{ diff -u poly.mprof-exp poly.$grade.mprof || true; } &&
			cp Prof.CallPair poly.$grade.CallPair &&
			cp Prof.MemoryWords poly.$grade.MemoryWords &&
			cp Prof.MemoryCells poly.$grade.MemoryCells &&
			cp Prof.Decl poly.$grade.Decl) || status=1
			;;
	esac
done
cd ..

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

: update the unstable release

case $HOST in murlibobo)
	rsh $BETA_FTPHOST \
		"rm -f $BETA_FTPDIR/mercury-*-rotd-*-unstable.tar.gz" &&
	rcp mercury-latest-unstable/mercury-*-$version-unstable.tar.gz \
		$BETA_FTPHOST:$BETA_FTPDIR 
	rm -f $BETA_WEBDIR/mercury-*-rotd-*-unstable.tar.gz &&
	cp mercury-latest-unstable/mercury-*-$version-unstable.tar.gz \
		$BETA_WEBDIR
	;;
esac

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

: build the binary distribution

# For the Linux systems, we need to distinguish them based
# on the version of GNU libc.
case $HOST in
	hg)	
		fullname=i686-pc-linux-libc2.1-gnu
		;;
	quicksilver)	
		fullname=i586-pc-linux-libc2.1-gnu
		;;
	hydra)	
		fullname=i686-pc-linux-libc2.0-gnu
		;;
	*)
		fullname=$fullarch
		;;
esac
# cvs does not allow `.' in tag names, so we use `_' instead
fullname_tag=`echo $fullname | tr . _`

{
	cd $DIR/mercury &&
	mmake bindist &&
	cd .. &&
	{ [ -d $UNSTABLE ] || mkdir $UNSTABLE; } &&
	rm -f $UNSTABLE/mercury-*.$fullname.tar.gz &&
	mv mercury/mercury-$version.$fullarch.tar.gz \
		$UNSTABLE/mercury-$version.$fullname.tar.gz &&
	:
	echo "test_mercury tagging stable rotd, starting at `date`" 1>&2 &&
	cvs rtag -d unstable-rotd-$fullname_tag mercury clpr tests &&
	cvs tag unstable-rotd-$fullname_tag mercury tests &&
	echo "test_mercury tagging unstable rotd, finished at `date`" 1>&2
} || status=1

case $status in
    0) {
    	cd $DIR &&
	{ [ -d $STABLE ] || mkdir $STABLE; } &&
	rm -f $STABLE/mercury-*.$fullname.tar.gz &&
	$LN $UNSTABLE/mercury-$version.$fullname.tar.gz \
		$STABLE/mercury-$version.$fullname.tar.gz &&
	rsh $BETA_FTPHOST "rm -f $BETA_FTPDIR/mercury-*.$fullname.tar.gz" &&
	rcp $STABLE/mercury-$version.$fullname.tar.gz \
		$BETA_FTPHOST:$BETA_FTPDIR &&
	echo "test_mercury tagging stable rotd, starting at `date`" 1>&2 &&
	cvs rtag -d stable-rotd-$fullname_tag mercury clpr tests &&
	cvs tag stable-rotd-$fullname_tag mercury tests &&
	echo "test_mercury tagging stable rotd, finished at `date`" 1>&2
       } || status=1
    ;;
esac

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

: check for success

case $status in
    0)
	: if we get this far, then it worked.
	[ -d $TESTDIR/logs ] || mkdir -p $TESTDIR/logs
	date >> $TESTDIR/logs/successful_tests.$HOST
	case $HOST in murlibobo)
	    cd $DIR &&
		# Only delete the stable rotds.
	    rm -f $STABLE/mercury-*-rotd-*[0-9].tar.gz &&
	    $LN $UNSTABLE/mercury-compiler-$version-unstable.tar.gz \
	        $STABLE/mercury-compiler-$version.tar.gz &&
	    $LN $UNSTABLE/mercury-extras-$version-unstable.tar.gz \
	        $STABLE/mercury-extras-$version.tar.gz &&
	    $LN $UNSTABLE/mercury-tests-$version-unstable.tar.gz \
	        $STABLE/mercury-tests-$version.tar.gz &&

		# Only delete the stable rotds.
	    rsh $BETA_FTPHOST "\
	    rm -f $BETA_FTPDIR/mercury-*-rotd-*[0-9].tar.gz &&
	    $LN $BETA_FTPDIR/mercury-compiler-$version-unstable.tar.gz \
	        $BETA_FTPDIR/mercury-compiler-$version.tar.gz &&
	    $LN $BETA_FTPDIR/mercury-extras-$version-unstable.tar.gz \
	        $BETA_FTPDIR/mercury-extras-$version.tar.gz &&
	    $LN $BETA_FTPDIR/mercury-tests-$version-unstable.tar.gz \
	        $BETA_FTPDIR/mercury-tests-$version.tar.gz" &&

		# Only delete the stable rotds.
	    rm -f $BETA_WEBDIR/mercury-*-rotd-*[0-9].tar.gz &&
	    $LN $BETA_WEBDIR/mercury-compiler-$version-unstable.tar.gz \
	        $BETA_WEBDIR/mercury-compiler-$version.tar.gz &&
	    $LN $BETA_WEBDIR/mercury-extras-$version-unstable.tar.gz \
	        $BETA_WEBDIR/mercury-extras-$version.tar.gz &&
	    $LN $BETA_WEBDIR/mercury-tests-$version-unstable.tar.gz \
	        $BETA_WEBDIR/mercury-tests-$version.tar.gz 
	    ;;
	esac
	echo "test_mercury exiting successfully at `date`" 1>&2
	true
	exit 0
	;;
    *)
	: one or more tests failed
	echo "some tests failed" 1>&2
	echo "test_mercury exiting unsuccessfully at `date`" 1>&2
	false
	exit 1
	;;
esac

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