#!/bin/sh
#---------------------------------------------------------------------------#
# Copyright (C) 1996-2001 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
	5)	HOST=$1; ARCH=$2; FULLARCH=$3; BRANCH=$4; C_COMPILER=$5 ;;
	*)	echo "Usage: $0 host arch fullarch branch c_compiler" 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`"
	PATH="$TOPDIR/mercury-latest/$FULLARCH/bin:$PATH"
	PATH="/home/$HOST/public/mercury-latest/$FULLARCH/bin:$PATH"
	PATH="$TOPDIR/autoconf-2.13/$FULLARCH/bin:$PATH"
	PATH="$TOPDIR/autoconf-2.52/$FULLARCH/bin:$PATH"
	PATH="$TOPDIR/$FULLARCH:$PATH"
	PATH="$TOPDIR/$HOST:$PATH"
	# generate_index_html needs to be in our PATH too
	PATH="$TOPDIR/test_mercury/scripts:$PATH"
else
	TOPDIR=$HOME/public
	PATH=$TOPDIR/mercury-latest/$FULLARCH/bin:$PATH
	PATH="$TOPDIR/$FULLARCH:$PATH"
	PATH="$TOPDIR/$HOST:$PATH"
fi
export PATH

# Use NFS if we can, otherwise use anonymous cvs (pserver).
CVSROOT=/home/mercury1/repository
if [ -d $CVSROOT/. ]; then
	:
else
	CVSROOT=":pserver:guest@cvs.mercury.cs.mu.oz.au:$CVSROOT"
fi
export CVSROOT

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

# Make the files writable by group mercury.
# But only do this for machines on the Mercury network.
if [ -d /home/mercury/public/. ]; then
	umask 002
else
	umask 022
fi

# Stop looping tests after 5 minutes of CPU time.
ulimit -S -t 300

# Allow tests to use up to about 250 Mb of memory
# (Most of it should build fine with 128 Mb,
# but building the --split-c-files versions of
# the libraries needs more, at least for dec-alpha-osf*.)
ulimit -S -d 250000

# directories to use

TESTDIR=$TOPDIR/test_mercury
HOSTDIR=$TESTDIR/test_dirs/$HOST
DIR=$HOSTDIR/build.$BRANCH.$C_COMPILER

# Set-up the class path needed for java testing.
CLASSPATH=$DIR/mercury/java:.
export CLASSPATH

# Host to use to build the release of the day source distribution
# for the mercury-compiler, mercury-tests, and mercury-extras distributions.
ROTD_HOST=jupiter

# Host to use to build the release of the day mercury-gcc source distribution.
GCC_ROTD_HOST=hg

set_status () {
	case "$*" in
	    "tests in grade asm_fast.agc"* | "tests in grade hlc_nest"*)
	        # These grades have known bugs which mean that they
		# do not pass all of the tests.  So we do not fail
		# if we get errors in those grades.
	    	echo "** Error in $* (expected)"
		;;
	    *)
		status=1;
		echo "** Error in $*"
		;;
	esac
}

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

#
# We set the following variables according to which
# branch we're testing.
#
# BRANCH:
# An abstract name for the branch, i.e. `0.10' or `latest'.
# This is not the same as the branch name used by CVS.
#
# TEST_ID:
# A name that identifies the version being tested.
# This should include all the relevant test
# parameters, e.g. BRANCH, HOST, C_COMPILER.
#
# INSTALL_DIR_NAME:
# The name of the directory in which to install this branch.
# (Not the full path, just the final directory name.)
# More generally used for naming stuff related to this branch.
#
# FTP_DIR_NAME:
# The name of the directory in which to put the source & binary
# distributions for this branch, on the ftp and web sites.
# (Not the full path, just the final directory name.)
#
# CHECKOUT_OPTS:
# Options to pass to cvs checkout.
#
# BASE_TAG_NAME:
# The name on which to base the cvs tag names that we add.
# We add cvs tags of the form
# `stable-$BASE_TAG_NAME-$fullname_tag' and
# `unstable-$BASE_TAG_NAME-$fullname_tag'
# for the unstable (passed bootcheck, but not tests)
# and stable (pass both bootcheck and tests) versions respectively.
#
# RELEASE_VERSION:
# The name to give this version.
#
# RELEASE_VERSION_PATTERN:
# A pattern used to delete earlier versions which
# this version will replace, if it builds successfully.
# This should usually be the same as $RELEASE_VERSION,
# but with `*' instead of $DATE.
#
INSTALL_DIR_NAME=mercury-$BRANCH
DATE=`date '+%Y-%m-%d'`
case $BRANCH in
	latest)
		CHECKOUT_OPTS=-A
		BASE_TAG_NAME=rotd
		FTP_DIR_NAME=rotd
		RELEASE_VERSION=rotd-$DATE
		RELEASE_VERSION_PATTERN=rotd-*
		;;
	0.11)
		CHECKOUT_OPTS=-rversion-0_11-branch
		BASE_TAG_NAME=version-0_11-branch
		FTP_DIR_NAME=0.11.1-beta
		RELEASE_VERSION=0.11.1-beta-$DATE
		RELEASE_VERSION_PATTERN=0.11.1-beta-*

		# This is for release 0.11.0 itself
		# (as opposed to beta releases).
		#CHECKOUT_OPTS=-rversion-0_11_0
		#BASE_TAG_NAME=version-0_11_0
		#FTP_DIR_NAME=0.11.0
		#RELEASE_VERSION=0.11.0
		#RELEASE_VERSION_PATTERN=0.11.0*
		;;
	0.10)
		CHECKOUT_OPTS=-rversion-0_10_y
		FTP_DIR_NAME=0.10.2-beta
		BASE_TAG_NAME=version-0_10_y
		RELEASE_VERSION=0.10.2-beta-$DATE
		RELEASE_VERSION_PATTERN=0.10.2-beta-*

		# This is for release 0.10.2 itself
		# (as opposed to beta releases).
		#CHECKOUT_OPTS=-rversion-0_10_2
		#BASE_TAG_NAME=version-0_10_y
		#RELEASE_VERSION=0.10.2
		#RELEASE_VERSION_PATTERN=0.10.2
		;;
esac
case "$C_COMPILER" in
	gcc)	TEST_ID=$BRANCH.$HOST
        	TEST_ERROR_FILE="$TESTDIR/logs/test-$BRANCH-$HOST.test_errs"
        	FAILED_TESTS_FILE="$TESTDIR/logs/test-$BRANCH-$HOST.failed_tests"
		;;
	*)	TEST_ID=$BRANCH.$HOST.$C_COMPILER
		TEST_ERROR_FILE="$TESTDIR/logs/test-$BRANCH-$HOST-$C_COMPILER.test_errs"
		FAILED_TESTS_FILE="$TESTDIR/logs/test-$BRANCH-$HOST-$C_COMPILER.failed_tests"
		;;
esac
rm -f $TEST_ERROR_FILE $FAILED_TESTS_FILE

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

case $HOST in
	# test things at different optimization levels...

	$ROTD_HOST|earth|jupiter)	
			case $BRANCH in
			    latest)
				OPTIMIZE="-O5 --intermodule-optimization --constraint-propagation"
				;;

			    0.10)
				# There are some performance problems with
				# deforestation on the 0.10 branch.
				# The compiler takes forever to compile
				# module_qual__process_assert with
				# `--intermodule-optimization'
				# and `--deforestation'.
				# `--constraint-propagation' does not
				# work on the release branch.
				OPTIMIZE="-O5 --intermodule-optimization --no-deforestation"
				;;
			esac
			;;

	murlibobo)	OPTIMIZE=-O5 ;;
	taifun) 	OPTIMIZE=-O1 ;;
	mundroo) 	OPTIMIZE=-O2 ;;
	quicksilver) 	OPTIMIZE=-O0 ;;
	hg) 		OPTIMIZE=-O4 ;;
	roy) 		OPTIMIZE=-O4 ;;	# hlc
	hydra)		OPTIMIZE=-O2 ;;
	ender)		OPTIMIZE=-O3 ;;
	aral)		OPTIMIZE=-O5 ;;
	venus) 		case "$C_COMPILER" in
				gcc)	OPTIMIZE=-O5 ;;
				*)	OPTIMIZE=-O2 ;;
			esac ;;
	mars)		case "$C_COMPILER" in
				gcc)	OPTIMIZE=-O5 ;;
				*)	OPTIMIZE=-O2 ;;
			esac ;;
	ceres)		OPTIMIZE=-O5 ;;	# hlc
	kryten) 	OPTIMIZE=-O1 ;;	# currently not used
	rimmer) 	OPTIMIZE=-O1 ;; # currently not used
esac

# on some systems, 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
LN="ln"
LN_S="ln -s"

# flags for building the compiler
INSTALL_MCFLAGS="$OPTIMIZE --opt-space"
INSTALL_MGNUCFLAGS=""
INSTALL_CFLAGS=""
INSTALL_MLFLAGS=""

# flags for running the tests
TEST_MCFLAGS="$OPTIMIZE --opt-space"
TEST_MGNUCFLAGS=""
TEST_CFLAGS=""
TEST_MLFLAGS=""

case $FULLARCH in
	i*86-*-linux*|i*86-*-freebsd*|i*86-*-solaris2.*)
		# On x86 Unix, 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...)
		case $BRANCH in
		    0.11)
			TEST_MCFLAGS="$TEST_MCFLAGS --pic-reg"
			;;
		    *)
			TEST_MCFLAGS="$TEST_MCFLAGS --mercury-linkage shared --pic-reg"
		    	;;
		esac
		TEST_MGNUCFLAGS="$TEST_MGNUCFLAGS --pic-reg"
		TEST_MLFLAGS="$TEST_MLFLAGS --shared"
		;;
	*)
		# On other platforms shared libraries is the default
		;;
esac

case $HOST in hg)
	INSTALL_MGNUCFLAGS="$INSTALL_MGNUCFLAGS --c-debug"
	INSTALL_MLFLAGS="$INSTALL_MLFLAGS --c-debug"
	;;   
esac

# This list of hosts for which we install into /home/$HOST rather than
# /home/mercury should match the corresponding list in run_all_tests_from_cron.
# So if you modify this list of hosts, you need to also modify
# run_all_tests_from_cron.
case $HOST in
	hg|ender|roy|venus|earth|mars|ceres|aral|jupiter)
		INSTALL_DIR=/home/$HOST/public/$INSTALL_DIR_NAME/$FULLARCH ;;
	*)		
		INSTALL_DIR=$TOPDIR/$INSTALL_DIR_NAME/$FULLARCH ;;
esac
case "$C_COMPILER" in
	gcc)	;;
	*)	INSTALL_DIR="$INSTALL_DIR-$C_COMPILER" ;;
esac

BETA_FTPHOST=ftp.mercury.cs.mu.oz.au
BETA_FTPDIR=/home/ftp/pub/mercury/beta-releases/$FTP_DIR_NAME
BETA_WEBDIR_TOP=/home/mercury/public/installed_w3/download/files
BETA_WEBDIR=$BETA_WEBDIR_TOP/beta-releases/$FTP_DIR_NAME

STABLE=$HOSTDIR/$INSTALL_DIR_NAME-stable
UNSTABLE=$HOSTDIR/$INSTALL_DIR_NAME-unstable

# XXX This probably isn't needed anymore. -fjh.
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.
		case $BRANCH in
		latest) INSTALL_DIR=$TOPDIR/.a ;;
		0.8)	INSTALL_DIR=$TOPDIR/.b ;;
		0.9)	INSTALL_DIR=$TOPDIR/.c ;;
		0.10)	INSTALL_DIR=$TOPDIR/.d ;;
		*)
			echo "unknown BRANCH \`$BRANCH'!"
			exit 1
			;;
		esac
		;;
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
	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
	ender) 		PARALLEL= ;; 	# one CPU
	roy) 		PARALLEL= ;; 	# one CPU
	venus) 		PARALLEL= ;; 	# one CPU
	earth) 		PARALLEL= ;; 	# one CPU
	mars) 		PARALLEL= ;; 	# one CPU
	ceres) 		PARALLEL= ;; 	# one CPU
	aral) 		PARALLEL= ;; 	# one CPU
	*)		PARALLEL= ;;
esac

# Version of the mercury compiler to use for bootstrapping
# Normally we use the compiler from same location that we will install in.
# But occaisionally this setting will get overridden below.
BOOTSTRAP_MERCURY_COMPILER=$INSTALL_DIR/lib/mercury/bin/$FULLARCH/mercury_compile

# The following can be edited and uncommented if you need to use
# a different bootstrap compiler to re-bootstrap a host which
# has gotten too out-of-date.
#case $HOST in mars)
#	BOOTSTRAP_MERCURY_COMPILER=$TOPDIR/$INSTALL_DIR_NAME/$FULLARCH/lib/mercury/bin/$FULLARCH/mercury_compile
#		;;
#esac

# df (disk free) command
DF=df
case $HOST in
	kryten|taifun|mundroo|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=$HOSTDIR/lockfile
if [ -f $lockfile ]; then
	echo "Directory $HOSTDIR 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

ulimit -a > $HOSTDIR/ulimit
env > $HOSTDIR/env

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

status=0

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

case $HOST in
	murlibobo)
		CONFIG_OPTS="--enable-inefficient-grades" ;;
	kryten|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" ;;
	ender)
		# Test out these options, to make sure they continue
		# to work
		CONFIG_OPTS="--enable-hlc-prof-grades \
			--enable-new-mercuryfile-struct" ;;
	roy|ceres)
		# On these hosts, test the high-level back-end more:
		# bootstrap the compiler in hlc.gc,
		# use hlc.gc as the default grade,
		# and install & test various other hlc-related grades.
		# Also test the asm_fast.gc.tr.rt grade.
		CONFIG_OPTS=""
		DEFAULT_GRADE="hlc.gc"
		export DEFAULT_GRADE
		INSTALL_GRADE="hlc.gc"
		INSTALL_LIBGRADES="hlc.gc.prof hlc.gc.memprof \
			hlc.gc.tr hlc.par.gc asm_fast.gc asm_fast.gc.tr.debug"
		# Test the asm_fast.gc.tr.rt grade, but not on the 0.10 branch,
		# since the asm_fast.gc.tr.rt grade is not yet implemented
		# in the 0.10 branch.
		# Likewise for `hlc' and `hlc.agc'
		#	
		# The hl.* grades don't work on the 0.10 branch because
		# we can only install the header files for one grade, and the 
		# headers for hl.gc and hlc.gc are different. This has been
		# fixed on the main branch.
		case $BRANCH in
			0.10)	;;
			*)	INSTALL_LIBGRADES="hlc hlc.agc $INSTALL_LIBGRADES \
					asm_fast.gc.tr.rt hl.gc hl.gc.prof \
					hl.gc.memprof hl.gc.tr hl.par.gc" ;;
		esac
		;;
	hg)
		CONFIG_OPTS=""
		case "$C_COMPILER" in asm)
			# Build the compiler in grade hlc.gc, because
			# --target asm requires an hl* grade.
			INSTALL_GRADE="hlc.gc"
			;;
		esac
		;;
	venus)
		CONFIG_OPTS=""
		case "$C_COMPILER" in lcc)
			# For grade none.gc.tr.debug, lcc 4.1 gets an
			# internal error when compiling library/hash_table.c.
			# So we need to disable that grade.
			INSTALL_LIBGRADES="none  \
				none.gc.prof none.prof none.gc.memprof \
				none.gc.tr \
				hlc.gc hlc.par.gc"
			;;
		esac
		;;
	mars)
		# Portable .NET is installed on mars, and the
		# test in configure.in doesn't test whether the
		# ilasm it finds is suitable for use with Mercury.
		CONFIG_OPTS="--disable-dotnet-grades"

		# Test --use-subdirs
		MMAKE_USE_SUBDIRS=yes
		export MMAKE_USE_SUBDIRS

		case "$C_COMPILER" in lcc)
			# For grade none.gc.tr.debug, lcc 4.1 gets an
			# internal error when compiling library/hash_table.c.
			# So we need to disable that grade.
			INSTALL_LIBGRADES="none  \
				none.gc.prof none.prof none.gc.memprof \
				none.gc.tr \
				hlc.gc hlc.par.gc"
			;;
		esac
		;;
	earth|jupiter)
		# ensure that the nogc grades get installed and tested
		# on at least one host
		CONFIG_OPTS="--enable-nogc-grades"
		;;
	aral)
		#
		# Aral has limited disk space, and not having to expend it
		# on grades not needed by its primary user (zs) helps to
		# conserve it.
		#
		INSTALL_LIBGRADES="asm_fast.gc asm_fast.gc.debug.tr \
			asm_fast.gc.mm asm_fast.gc.mm.debug \
			asm_fast.gc.profdeep hlc.gc"
		;;
	*)	
		CONFIG_OPTS="" ;;
esac


	# The deep profiler needs to be included in the source distribution.
case $HOST in $ROTD_HOST)
	CONFIG_OPTS="$CONFIG_OPTS \
	    --enable-deep-profiler=$TOPDIR/installed_w3/cgi-bin"
	if test "$INSTALL_LIBGRADES" != ""; then
		INSTALL_LIBGRADES="$INSTALL_LIBGRADES asm_fast.gc.profdeep"
	fi
	;;
esac

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

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

install_anyway=false

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

: clean up any directories from previous test runs

cd $HOSTDIR
rm -rf build.*/mercury
rm -rf build.*/tests
rm -rf build.*/mercury-cvs
rm -rf build.*/mercury-gcc
# Note that we do NOT want to clean out the GCC directory,
# because checking out the GCC sources from the FSF's repository in
# California takes a *long* time and a *lot* of network bandwidth,
# and we're currently using a fixed snapshot anyway, so we don't
# bother to check it out at all.
### rm -rf build.*/gcc

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

: 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; }
# we need to manually remove this copy of dl.m to avoid spurious cvs
# conflicts when switching between the 0.10 branch (where it is in cvs)
# and the main branch (where it is generated automatically).
rm -f mercury/extras/dynamic_linking/dl.m
cvs checkout $CHECKOUT_OPTS mercury tests || { false; exit 1; }
case "$C_COMPILER" in
	asm)	with_asm=true ;;
	*)	case $HOST in
			hg)	with_asm=true ;;
			*)	with_asm=false ;;
		esac
		;;
esac
case "$with_asm" in
	true)
		# Check out the Mercury gcc back-end interface sources
		cvs checkout $CHECKOUT_OPTS mercury-gcc || { false; exit 1; }
		# Check out the main gcc sources.
		# See <http://gcc.gnu.org/cvs.html>.
		#
		# Once the gcc sources have been checked out here,
		# the Mercury autoconfiguration script should find them
		# automatically, so we don't need to bother explicitly
		# passing `--enable-gcc-back-end' to configure.
		#
		# Note that the `cvs login' command, namely
		# `cvs -d :pserver:anoncvs@gcc.gnu.org:/cvs/gcc login'
		# must be done manually, since there is no easy way to pass
		# the password (`anoncvs') from a script.
		# Once it is done once, it will be saved in the user's
		# ~/.cvspass file, so it doesn't need to be done again.
		#
		# This is commented out, to save bandwidth,
		# since currently we're using a fixed snapshot
		# (`-r gcc_3_2_release') anyway.
		# Perhaps this should eventually change to use
		# `-r gcc_latest_snapshot'.
		date
		#cvs -d :pserver:anoncvs@gcc.gnu.org:/cvs/gcc -z 9 \
		# 	checkout -r gcc_3_2_release gcc || { false; exit 1; }
		#date
		# Link the Mercury gcc back-end interface sources
		# into the appropriate place in the gcc sources
		pwd
		(cd gcc/gcc && rm -f mercury && \
			ln -s ../../mercury-gcc mercury) \
			|| { false; exit 1; }
		# Now configure and build the gcc directory.
		# The gcc cvs repository includes some automatically
		# generated files; we need to touch them here before
		# running configure.
		# XXX We should perhaps also do `make distclean' before
		# configure.
		(cd gcc && 
			contrib/gcc_update --touch &&
			sh configure --enable-languages=mercury &&
			make) || set_status "gcc checkout"
		date
		;;
esac
[ -d mercury/extras/clpr/CVS ] ||
(cd mercury/extras && cvs checkout $CHECKOUT_OPTS clpr) ||
	set_status "clpr checkout"

: 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 = $INSTALL_MCFLAGS" >> Mmake.params
	echo "EXTRA_MGNUCFLAGS = $INSTALL_MGNUCFLAGS" >> Mmake.params
	echo "EXTRA_CFLAGS = $INSTALL_CFLAGS" >> Mmake.params
	echo "EXTRA_MLFLAGS = $INSTALL_MLFLAGS" >> Mmake.params
	case "$INSTALL_GRADE" in
		"")	;;
		*)	echo "GRADE = $INSTALL_GRADE" >> Mmake.params
			;;
	esac
	case "$INSTALL_LIBGRADES" in
		"")	;;
		*)	echo "LIBGRADES = $INSTALL_LIBGRADES" >> Mmake.params
			;;
	esac
# fi

# We allow "asm" as a special value for the C_COMPILER
# variable; it means to build with `--target asm'.
# $CC holds the name of the real C compiler.

case "$C_COMPILER" in
	asm)
		CC=gcc
		target_opts="--target asm"
		;;
	*)
		CC="$C_COMPILER"
		target_opts=""
		;;
esac
export CC

autoconf || { false; exit 1; }
rm -f config.cache
./configure --prefix=$INSTALL_DIR $CONFIG_OPTS || { false; exit 1; }
mmake $target_opts depend $PARALLEL || { false; exit 1; }
mmake $target_opts realclean MMAKEFLAGS=$PARALLEL || { false; exit 1; }
./configure --prefix=$INSTALL_DIR $CONFIG_OPTS || { false; exit 1; }
mmake $target_opts depend $PARALLEL || { false; exit 1; }
version=`mmake $target_opts version` || { false; exit 1; }
fullarch=`mmake $target_opts fullarch` || { false; exit 1; }
tools/bootcheck $target_opts --keep-objs --copy-runtime --copy-profiler \
	--no-test-suite $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

    # Previously we had lots of special case code here for installing on
    # IRIX systems.   We don't currently have any IRIX boxes, and this 
    # code was a bit of a pain to maintain.  If we ever get an IRIX box
    # again, look back in the CVS archives for version 1.114 to see
    # the necessary code.

    # Note that we don't pass $target_opts when doing `mmake install_grades';
    # that wouldn't work, since many of the grades can only be built with
    # `--target c'.
    mmake $target_opts install_main MMAKEFLAGS=$PARALLEL || \
    		set_status "install_main"
    mmake install_grades MMAKEFLAGS=$PARALLEL || \
    		set_status "install_grades"
    case "$INSTALL_GRADE" in
	hl*)
	    	# --split-c-files is not supported for the hl* grades
	    	;;
	*)	mmake install_split_library \
			LIBGRADES=$INSTALL_GRADE MMAKEFLAGS=$PARALLEL || \
    			set_status "install_split_library"
		;;
    esac

	# If you want to add extra grades to install on specific hosts,
	# do it here.  Any grade which is tested later, but which is not
	# installed above, should be installed here.
    case $HOST in
    	hg)	mmake install_grades LIBGRADES="asm_fast.par.gc" || \
    			set_status "install asm_fast.par.gc"
		;;
	*)
		;;
    esac

    case $install_anyway in
        false)	cd ../.. || set_status "cd ../.." ;;
        true)	cd .. || set_status "cd .." ;;
    esac

    [ -d $TESTDIR/logs ] || mkdir -p $TESTDIR/logs
    case $status in
        0) 
	    date >> $TESTDIR/logs/install."$TEST_ID"
	    rm -f $TESTDIR/logs/install-failed."$TEST_ID"
	    ;;
        *)
	    date >> $TESTDIR/logs/install-failed."$TEST_ID"
	    ;;
    esac
esac

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

# The stage2 compiler got installed, so we don't need to set
# MERCURY_COMPILER now.
# In fact, we should unset it, so it isn't set to something
# inappropriate by some other part of this script.
# Instead we just need to make sure that the compiler that we
# just installed is first in the PATH.
# From here-on in, we use the compiler that we just built and
# installed, not the bootstrap compiler.

unset MERCURY_COMPILER

PATH="$INSTALL_DIR/bin:$PATH"

GRADES="`(cd $DIR/mercury && mmake echo_libgrades)`"
case $HOST in
   hg)
   	GRADES="$GRADES asm_fast.par.gc"
	;;
esac

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

: test and install the CLPR interface

# CLPR requires trailing, so here we only test the trailing grades.
# XXX The CLPR interface currently also requires GCC (see the #error in
# extras/clpr/cfloat.m), so we test it only if we're using GCC.
case "$C_COMPILER" in
    gcc)
	for grade in $GRADES; do
	    case "$grade" in *.tr*)
		echo "test clpr stuff for grade $grade" 1>&2
		{
			echo "GRADE = $grade"
			echo "EXTRA_MCFLAGS = $INSTALL_MCFLAGS"
			echo "EXTRA_MGNUCFLAGS = $INSTALL_MGNUCFLAGS"
			echo "EXTRA_CFLAGS = $INSTALL_CFLAGS"
			echo "EXTRA_MLFLAGS = $INSTALL_MLFLAGS"
		} > mercury/extras/Mmake.params &&
		(cd mercury/extras/clpr &&
			mmake realclean $PARALLEL MMAKEFLAGS=$PARALLEL &&
			mmake -k depend	 $PARALLEL MMAKEFLAGS=$PARALLEL &&
			mmake -k 	$PARALLEL MMAKEFLAGS=$PARALLEL &&
			mmake -k check	 $PARALLEL MMAKEFLAGS=$PARALLEL &&
			mmake install LIBGRADES=$grade \
					$PARALLEL MMAKEFLAGS=$PARALLEL &&
			mmake realclean $PARALLEL MMAKEFLAGS=$PARALLEL) ||
				set_status "clpr tests in grade $grade"
		echo "test trailed_update stuff for grade $grade" 1>&2
		(cd mercury/extras/trailed_update &&
			mmake realclean $PARALLEL MMAKEFLAGS=$PARALLEL &&
			mmake -k depend	 $PARALLEL MMAKEFLAGS=$PARALLEL &&
			mmake -k	 $PARALLEL MMAKEFLAGS=$PARALLEL &&
			mmake -k check	 $PARALLEL MMAKEFLAGS=$PARALLEL &&
			mmake realclean $PARALLEL MMAKEFLAGS=$PARALLEL) ||
			    set_status "trailed_update tests in grade $grade"
		;;
	    esac
	done
	;;
    *)
	echo "not testing clpr, since the CLP(R) interface requires GCC"
	;;
esac

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

: create distribution

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

case $HOST in $ROTD_HOST)
	# The tests on $ROTD_HOST are run with `--intermodule-optimization',
	# so we need to do a `mmake realclean' before building the C files
	# for the source distribution. The extra compilation time for
	# `--intermodule-optimization' is probably not acceptable for the
	# source distribution.
	{ 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' $PARALLEL" tar &&
	cd .. &&
	rm -f $UNSTABLE/* &&
	cp mercury/NEWS $UNSTABLE/mercury-NEWS-$version-unstable.txt &&
	cp mercury/INSTALL $UNSTABLE/mercury-INSTALL-$version-unstable.txt &&
	mv mercury-compiler-$version.tar.gz	\
	    $UNSTABLE/mercury-compiler-$version-unstable.tar.gz &&
	mv mercury-extras-$version.tar.gz	\
	    $UNSTABLE/mercury-extras-$version-unstable.tar.gz &&
	mv mercury-tests-$version.tar.gz	\
	    $UNSTABLE/mercury-tests-$version-unstable.tar.gz
	} || set_status "making source distribution"
	# 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
case $HOST in $GCC_ROTD_HOST)
	# build the mercury-gcc source distribution
	cd $DIR &&
	rm -f	mercury-gcc/stage[1-4] \
		mercury-gcc/include \
		mercury-gcc/mercury \
		mercury-gcc/*.o &&
	mv mercury-gcc mercury-gcc-$version &&
	rm -f $UNSTABLE/mercury-gcc-$RELEASE_VERSION_PATTERN.tar.gz &&
	tar -cf - mercury-gcc-$version | gzip -9 \
		> $UNSTABLE/mercury-gcc-$version.tar.gz &&
	mv mercury-gcc-$version mercury-gcc &&
	true
	;;
esac

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

: run the regression tests on the installed compiler

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

echo "test_mercury running tests in grades $GRADES" 1>&2
for grade in $GRADES
do
	echo "test_mercury starting tests for grade $grade at `date`" 1>&2
	case $BRANCH in
	    0.10)
		./runtests -f "$TEST_MCFLAGS" -c "$TEST_CFLAGS" -g "$grade" \
		       -m "$TEST_MGNUCFLAGS" -l "$TEST_MLFLAGS" \
		       $PARALLEL || set_status "tests in grade $grade"
		;;
	    *)
#	        echo "testing grade $grade, MCFLAGS=$TEST_MCFLAGS" \
#			>> $TEST_ERROR_FILE
		mmake GRADE="$grade" \
			EXTRA_MCFLAGS="$TEST_MCFLAGS" \
			EXTRA_CFLAGS="$TEST_CFLAGS" \
			EXTRA_MGNUCFLAGS="$TEST_MGNUCFLAGS" \
			EXTRA_MLFLAGS="$TEST_MLFLAGS" \
			$PARALLEL runtests || \
			set_status "tests in grade $grade"
		if [ -f runtests.errs ]; then
			touch $TEST_ERROR_FILE
			cat runtests.errs >> $TEST_ERROR_FILE
		fi
		;;
	esac
	case $grade in
		*.memprof*)
			# we need to set LD_BIND_NOW=1
			# for profiling on DEC Alpha
			(cd benchmarks &&
			touch poly.m &&
			mmake poly.depend &&
			mmake poly \
				GRADE="$grade" \
				EXTRA_MCFLAGS="$TEST_MCFLAGS" \
				EXTRA_MGNUCFLAGS="$TEST_MGNUCFLAGS" \
				EXTRA_CFLAGS="$TEST_CFLAGS" \
				EXTRA_MLFLAGS="$TEST_MLFLAGS" &&
			LD_BIND_NOW=1 ./poly > poly.$grade.out &&
			case $grade in
			*hl*)
				# for --high-level-code, we need to
				# pass --no-demangle to mprof
				mprof --no-demangle -v -m -c \
					> poly.$grade.mprof 2>&1 &&
				{ diff -u poly.mprof-exp2 poly.$grade.mprof ||
					true; }
				;;
			*)
				mprof -v -m -c > poly.$grade.mprof 2>&1 &&
				{ diff -u poly.mprof-exp poly.$grade.mprof ||
					true; }
				;;
			esac &&
			cp Prof.CallPair poly.$grade.CallPair &&
			cp Prof.MemoryWords poly.$grade.MemoryWords &&
			cp Prof.MemoryCells poly.$grade.MemoryCells &&
			cp Prof.Decl poly.$grade.Decl) ||
			set_status "benchmarks/poly in grade *.memprof*"
			;;
	esac
	echo "test_mercury finished tests for grade $grade at `date`" 1>&2
done
case $HOST in hg)
	echo "test_mercury starting tests for --target asm at `date`" 1>&2
	case $BRANCH in
	    0.10)
		# XXX Note that some of these tests are expected to fail
		# on the 0.10 branch with `--target asm' (in particular
		# the tests of tabling and nested modules), so we don't
		# set status=1 if these tests fail.
		./runtests --target asm -f "$TEST_MCFLAGS" -c "$TEST_CFLAGS" \
			-g hlc.gc -m "$TEST_MGNUCFLAGS" -l "$TEST_MLFLAGS" \
			$PARALLEL
		;;
	    *)
		mmake --target asm GRADE=hlc.gc \
			EXTRA_MCFLAGS="$TEST_MCFLAGS" \
			EXTRA_CFLAGS="$TEST_CFLAGS" \
			EXTRA_MGNUCFLAGS="$TEST_MGNUCFLAGS" \
			EXTRA_MLFLAGS="$TEST_MLFLAGS" \
			$PARALLEL runtests \
			|| set_status "tests of --target asm in grade $grade"
		if [ -f runtests.errs ]; then
			touch $TEST_ERROR_FILE
			cat runtest.errs >> $TEST_ERROR_FILE
		fi
		;;
	esac
	;;
esac
if [ -s $TEST_ERROR_FILE ]; then
	fgrep "FAILED TEST" $TEST_ERROR_FILE > $FAILED_TESTS_FILE
fi
echo "test_mercury finished tests at `date`" 1>&2

cd ..

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

: update the unstable release

case $HOST in $ROTD_HOST)
	if [ -s $TEST_ERROR_FILE ]; then
		{ cat $FAILED_TESTS_FILE;
		  echo "";
		  echo "Test logs for the failed test cases:";
		  echo "";
		  cat $TEST_ERROR_FILE;
		} > $UNSTABLE/mercury-test-failures-$version-unstable.txt
	else 
		rm -f $UNSTABLE/mercury-test-failures-$version-unstable.txt
	fi &&
	ssh $BETA_FTPHOST "\
	    [ -d $BETA_FTPDIR ] || mkdir $BETA_FTPDIR
	    rm -f \
		$BETA_FTPDIR/mercury-compiler-$RELEASE_VERSION_PATTERN-unstable.tar.gz \
		$BETA_FTPDIR/mercury-extras-$RELEASE_VERSION_PATTERN-unstable.tar.gz \
		$BETA_FTPDIR/mercury-tests-$RELEASE_VERSION_PATTERN-unstable.tar.gz \
		$BETA_FTPDIR/mercury-*-$RELEASE_VERSION_PATTERN-unstable.txt" &&
	scp $UNSTABLE/mercury-*-$version-unstable.* \
		$BETA_FTPHOST:$BETA_FTPDIR &&
	ssh $BETA_FTPHOST "\
		echo $version > $BETA_FTPDIR/latest-unstable-version" &&
	rm -f $BETA_WEBDIR/mercury-compiler-$RELEASE_VERSION_PATTERN-unstable.tar.gz &&
	rm -f $BETA_WEBDIR/mercury-extras-$RELEASE_VERSION_PATTERN-unstable.tar.gz &&
	rm -f $BETA_WEBDIR/mercury-tests-$RELEASE_VERSION_PATTERN-unstable.tar.gz &&
	rm -f $BETA_WEBDIR/mercury-*-$RELEASE_VERSION_PATTERN-unstable.txt &&
	cp $UNSTABLE/mercury-*-$version-unstable.* $BETA_WEBDIR &&
	echo $version > $BETA_WEBDIR/latest-unstable-version \
	|| set_status "copying ROTD"
	;;
esac
case $HOST in $GCC_ROTD_HOST)
	ssh $BETA_FTPHOST "\
	  [ -d $BETA_FTPDIR ] || mkdir $BETA_FTPDIR
	  rm -f \
	    $BETA_FTPDIR/mercury-gcc-$RELEASE_VERSION_PATTERN-unstable.tar.gz" &&
	scp $UNSTABLE/mercury-gcc-$version.tar.gz \
	    $BETA_FTPHOST:$BETA_FTPDIR/mercury-gcc-$version-unstable.tar.gz  &&
	rm -f $BETA_WEBDIR/mercury-gcc-$RELEASE_VERSION_PATTERN-unstable.tar.gz &&
	cp $UNSTABLE/mercury-gcc-$version.tar.gz \
		$BETA_WEBDIR/mercury-gcc-$version-unstable.tar.gz \
	|| set_status "copying mercury-gcc ROTD"
	;;
esac

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

: build the binary distribution

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

# For the Linux systems, we need to distinguish them based
# on the version of GNU libc.
# Also we distinguish them based on what optimization level
# they were compiled with.
case $HOST in
	hg)	
		fullname=i686-pc-linux-libc2.1-gnu-O4
		;;
	venus)	
		fullname=i686-pc-linux-libc2.2-gnu-O5
		;;
	earth|jupiter)	
		fullname=i686-pc-linux-libc2.2-gnu-O5-intermod
		;;
	mars)	
		fullname=i686-pc-linux-libc2.2-gnu-O5
		;;
	ceres)	
		fullname=i686-pc-linux-libc2.3-gnu-O5-hlc
		;;
	aral)	
		fullname=i686-pc-linux-libc2.3-gnu-O5
		;;
	roy)	
		fullname=i686-pc-linux-libc2.1-gnu-O4-hlc
		;;
	quicksilver)	
		fullname=i586-pc-linux-libc2.1-gnu-O0
		;;
	hydra)	
		fullname=i686-pc-linux-libc2.1-gnu-O2
		;;
	ender)	
		fullname=i686-pc-linux-libc2.1-gnu-O3
		;;
	*)
		fullname=$fullarch
		;;
esac

# For alphas, we don't care about the differences between
# different processor revisions, since the generated
# binaries work on all of them.
case $fullname in
	alpha*-dec-osf3.2)
		fullname=alpha-dec-osf3.2
		;;
	alpha*-dec-osf5.1)
		fullname=alpha-dec-osf5.1
		;;
esac

case "$C_COMPILER" in
	gcc)	;;
	*)	fullname="$fullname-$C_COMPILER" ;;
esac

# cvs does not allow `.' or space in tag names, so we use `_' instead
fullname_tag=`echo $fullname | tr ' .' __`

# On murlibobo building the binary distribution takes a lot of CPU time.
ulimit -S -t unlimited

{
	cd $DIR/mercury &&
	mmake bindist &&
	cd .. &&
	{ [ -d $UNSTABLE ] || mkdir $UNSTABLE; } &&
	rm -f $UNSTABLE/mercury-*.$fullname.tar.gz &&
	rm -f $UNSTABLE/mercury-*.$fullname.txt &&
	cp mercury/NEWS $UNSTABLE/mercury-NEWS-$version.$fullname.txt &&
	cp mercury/bindist/bindist.INSTALL \
		$UNSTABLE/mercury-INSTALL-$version.$fullname.txt &&
	if [ -s $TEST_ERROR_FILE ]; then
		{ cat $FAILED_TESTS_FILE;
		  echo "";
		  echo "Test logs for the failed test cases:";
		  echo "";
		  cat $TEST_ERROR_FILE;
		} > $UNSTABLE/mercury-test-failures-$version.$fullname.txt
	else 
		rm -f $UNSTABLE/mercury-test-failures-$version.$fullname.txt
	fi &&
	mv mercury/mercury-$version.$fullarch.tar.gz \
		$UNSTABLE/mercury-$version.$fullname.tar.gz &&
	case "$CVSROOT" in
	    :pserver:guest* | :pserver:anon*)
		echo "Not tagging CVS repository" \
			"(no write access for anonymous cvs)"
		;;
	    *)
		echo "test_mercury tagging unstable $BASE_TAG_NAME," \
			"starting at `date`" 1>&2 &&
		cvs rtag -d unstable-$BASE_TAG_NAME-$fullname_tag \
			mercury clpr tests &&
		cvs tag unstable-$BASE_TAG_NAME-$fullname_tag mercury tests &&
		if [ -d gcc/. ] && [ -d mercury-gcc/. ]; then
			cvs rtag -d unstable-$BASE_TAG_NAME-$fullname_tag \
				mercury-gcc &&
			cvs tag unstable-$BASE_TAG_NAME-$fullname_tag \
				mercury-gcc
		else	
			true
		fi &&
		echo "test_mercury tagging unstable $BASE_TAG_NAME," \
			"finished at `date`" 1>&2
		;;
	esac
} || set_status "creating binary distribution"

case $status in
    0) {
    	cd $DIR &&
	{ [ -d $STABLE ] || mkdir $STABLE; } &&
	rm -f $STABLE/mercury*-$RELEASE_VERSION_PATTERN.$fullname.tar.gz &&
	rm -f $STABLE/mercury*-$RELEASE_VERSION_PATTERN.$fullname.txt &&
	$LN $UNSTABLE/mercury-$version.$fullname.tar.gz \
		$STABLE/mercury-$version.$fullname.tar.gz &&
	$LN $UNSTABLE/mercury-NEWS-$version.$fullname.txt \
		$STABLE/mercury-NEWS-$version.$fullname.txt &&
	$LN $UNSTABLE/mercury-INSTALL-$version.$fullname.txt \
		$STABLE/mercury-INSTALL-$version.$fullname.txt &&
	ssh $BETA_FTPHOST "\
	    [ -d $BETA_FTPDIR ] || mkdir $BETA_FTPDIR
	    rm -f \
		$BETA_FTPDIR/mercury-$RELEASE_VERSION_PATTERN.$fullname.tar.gz \
		$BETA_FTPDIR/mercury*-$RELEASE_VERSION_PATTERN.$fullname.txt" &&
	scp $STABLE/mercury*-$version.$fullname.* $BETA_FTPHOST:$BETA_FTPDIR &&
	case "$CVSROOT" in
	    :pserver:guest* | :pserver:anon*)
		echo "Not tagging CVS repository" \
			"(no write access for anonymous cvs)"
		;;
	    *)
		echo "test_mercury tagging stable $BASE_TAG_NAME," \
			"starting at `date`" 1>&2 &&
		cvs rtag -d stable-$BASE_TAG_NAME-$fullname_tag \
			mercury clpr tests &&
		cvs tag stable-$BASE_TAG_NAME-$fullname_tag mercury tests &&
		if [ -d gcc/. ] && [ -d mercury-gcc/. ]; then
			cvs rtag -d stable-$BASE_TAG_NAME-$fullname_tag \
				mercury-gcc &&
			cvs tag stable-$BASE_TAG_NAME-$fullname_tag mercury-gcc
		else	
			true
		fi &&
		echo "test_mercury tagging stable $BASE_TAG_NAME," \
			"finished at `date`" 1>&2
		;;
	esac
       } || set_status "copying binary distribution"
    ;;
esac

echo "test_mercury finished binary distribution at `date`" 1>&2
#-----------------------------------------------------------------------------#

case $status in
    0)
	: if we get this far, then it worked.
	[ -d $TESTDIR/logs ] || mkdir -p $TESTDIR/logs
	date >> $TESTDIR/logs/success."$TEST_ID"

	case $HOST in $ROTD_HOST)
	    cd $DIR &&
		# Delete older stable versions that this one replaces
	    rm -f $STABLE/mercury-*-$RELEASE_VERSION_PATTERN[0-9].tar.gz &&
	    rm -f $STABLE/mercury-*-$RELEASE_VERSION_PATTERN[0-9].txt &&
	    rm -f $STABLE/mercury-*-$version.tar.gz &&
	    rm -f $STABLE/mercury-*-$version.txt &&
	    $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 &&
	    $LN $UNSTABLE/mercury-NEWS-$version-unstable.txt \
	        $STABLE/mercury-NEWS-$version.txt &&
	    $LN $UNSTABLE/mercury-INSTALL-$version-unstable.txt \
	        $STABLE/mercury-INSTALL-$version.txt &&

	    ssh $BETA_FTPHOST "\
	    [ -d $BETA_FTPDIR ] || mkdir $BETA_FTPDIR
	    rm -f $BETA_FTPDIR/mercury-*-$RELEASE_VERSION_PATTERN[0-9].tar.gz \
	          $BETA_FTPDIR/mercury-*-$version.tar.gz \
	          $BETA_FTPDIR/mercury-*-$RELEASE_VERSION_PATTERN[0-9].txt \
	          $BETA_FTPDIR/mercury-*-$version.txt && 
	    $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 &&
	    $LN $BETA_FTPDIR/mercury-NEWS-$version-unstable.txt \
	        $BETA_FTPDIR/mercury-NEWS-$version.txt &&
	    $LN $BETA_FTPDIR/mercury-INSTALL-$version-unstable.txt \
	        $BETA_FTPDIR/mercury-INSTALL-$version.txt &&
            echo $version > $BETA_FTPDIR/latest-stable-version" &&

	    rm -f $BETA_WEBDIR/mercury-compiler-$RELEASE_VERSION_PATTERN[0-9].tar.gz &&
	    rm -f $BETA_WEBDIR/mercury-extras-$RELEASE_VERSION_PATTERN[0-9].tar.gz &&
	    rm -f $BETA_WEBDIR/mercury-tests-$RELEASE_VERSION_PATTERN[0-9].tar.gz &&
	    rm -f $BETA_WEBDIR/mercury-NEWS-$RELEASE_VERSION_PATTERN[0-9].txt &&
	    rm -f $BETA_WEBDIR/mercury-INSTALL-$RELEASE_VERSION_PATTERN[0-9].txt &&
	    $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 &&
	    $LN $BETA_WEBDIR/mercury-NEWS-$version-unstable.txt \
	        $BETA_WEBDIR/mercury-NEWS-$version.txt &&
	    $LN $BETA_WEBDIR/mercury-INSTALL-$version-unstable.txt \
	        $BETA_WEBDIR/mercury-INSTALL-$version.txt &&

            echo $version > $BETA_WEBDIR/latest-stable-version
	    ;;
	esac || set_status "updating stable mercury source distribution"

	case $HOST in $GCC_ROTD_HOST)
	    cd $DIR &&
		# Delete older stable versions that this one replaces
	    rm -f $STABLE/mercury-gcc-$RELEASE_VERSION_PATTERN[0-9].tar.gz &&
	    rm -f $STABLE/mercury-gcc-$version.tar.gz &&
	    $LN $UNSTABLE/mercury-gcc-$version.tar.gz \
	        $STABLE/mercury-gcc-$version.tar.gz &&

	    ssh $BETA_FTPHOST "\
	    [ -d $BETA_FTPDIR ] || mkdir $BETA_FTPDIR
	    rm -f $BETA_FTPDIR/mercury-gcc-$RELEASE_VERSION_PATTERN[0-9].tar.gz \
		  $BETA_FTPDIR/mercury-gcc-$version.tar.gz && 
	    $LN $BETA_FTPDIR/mercury-gcc-$version-unstable.tar.gz \
	        $BETA_FTPDIR/mercury-gcc-$version.tar.gz" &&

	    rm -f $BETA_WEBDIR/mercury-gcc-$RELEASE_VERSION_PATTERN[0-9].tar.gz &&
	    $LN $BETA_WEBDIR/mercury-gcc-$version-unstable.tar.gz \
	        $BETA_WEBDIR/mercury-gcc-$version.tar.gz
	    ;;
	esac || set_status "updating stable mercury-gcc source distribution"
	;;
    *)
	;;
esac

# Now rebuild the index files on the www site
case $HOST in 
    $ROTD_HOST)
	echo "test_mercury generating www index files, starting at `date`" 1>&2
	(
	cd $BETA_WEBDIR_TOP &&
	generate_index_html
	) || set_status "generating www index"
	echo "test_mercury generating www index files, finished at `date`" 1>&2
	;;
    *)
	;;
esac

# Now rebuild the index files on the ftp site
echo "test_mercury generating ftp index files, starting at `date`" 1>&2
ssh $BETA_FTPHOST "\
	PATH=/home/mercury/public/cron/scripts:\$PATH &&
	export PATH &&
	( [ -d $BETA_FTPDIR ] || mkdir $BETA_FTPDIR ) &&
	cd $BETA_FTPDIR &&
	generate_index_html" || set_status "generating ftp index"
echo "test_mercury generating ftp index files, finished at `date`" 1>&2

: check for success

case $status in
    0)

	echo "Installation directory: $INSTALL_DIR" 1>&2
	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

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