#!/bin/sh
#---------------------------------------------------------------------------#
# Copyright (C) 1995-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.
#---------------------------------------------------------------------------#

usage="\
Usage: $0 [options]
Options:
	-d <dirname>, --test-dir <dirname>
		Run the tests in directory <dirname>. Multiple such options
		may be given, in which case the tests in all the named
		directories will be run. In the absence of any such options,
		all tests in all directories will be run (unless testing
		as a whole is disabled).
	-h, --help
		Display this usage message.
	-j <num-jobs>, --jobs <num-jobs>
		Run using <num-jobs> different parallel processes.
	-k, --keep-objs
		Keep the stage 2 object files even if stage 2 is successful.
	-m <mmake-args>, --mmake-args <mmake-args>
		Pass <mmake-args> as options to \`mmake'.
	-o <filename>, --output-file <filename>
		Output results to <filename>.
	-G <grade>, --grade <grade>
		Specify the grade to use in creating stages 2 and 3.
		The tests will also be executed in this grade, unless
		the --test-grade option says otherwise. Implies -r -g.
	--test-grade <grade>
		Specify the grade to use in executing the tests.
		If this is the same as the grade of stages 2 and 3, then
		we use the stage 2 runtime, library etc for the tests.
		Otherwise, we use the stage 1 runtime, library etc,
		and trust that these are compatible with the given grade.
		Implies -r -g.
	-g, --copy-boehm-gc
		Copy the boehm_gc directory instead of linking it.
		This is necessary if one wants to bootcheck a grade
		that is not compatible with the standard one.
	-r, --copy-runtime
		Copy the runtime directory instead of linking it.
		This is necessary if one wants to bootcheck a grade
		that is not compatible with the standard one.
	-p, --copy-profiler
		Copy the profiler directory instead of linking it.
		This is sometimes necessary for bootstrapping
		changes.
	-t, --no-test-suite
		By default, bootcheck will also run the test quite.
		This option prevents that.
	-T, --test-suite-only
		Do not run the bootstrap check; execute the test suite only.
		This option requires a previous bootstrap check to have left
		a working stage 2 directory.
	-2, --keep-stage-2
		Don't rebuild the stage 2 directory from scratch after
		building stage 1.  Instead use the existing stage 2 directory.
	-3, --keep-stage-3
		Don't rebuild the stage 3 directory from scratch after
		building stage 1.  Instead use the existing stage 3 directory.
	--stop-after-stage-2
		Stop after building stage 2, even if it is successful.
	--use-subdirs
		Assume intermediate files are built in subdirectories.
		(Same as the \`--use-subdirs' option to mmake and mmc.)
"

testdirs=""
jfactor=""
keep_objs=false
mmake_opts="-k"
outfile=""
runtests=true
do_bootcheck=true
grade=
test_grade=
copy_runtime=false
copy_boehm_gc=false
copy_profiler=false
keep_stage_2=false
keep_stage_3=false
stop_after_stage_2=false
if [ -d compiler/Mercury ]; then
	use_subdirs=${MMAKE_USE_SUBDIRS=yes}
else
	use_subdirs=${MMAKE_USE_SUBDIRS=no}
fi

# If you change these, you will also need to change scripts/ml.in,
# scripts/c2init.in, Mmake.common.in, tools/binary, tools/binary_step
# and tools/linear.
RT_LIB_NAME=mer_rt
STD_LIB_NAME=mer_std
TRACE_LIB_NAME=mer_trace
BROWSER_LIB_NAME=mer_browser

while [ $# -gt 0 ]; do
	case "$1" in

	-d|--test-dir)
		testdirs="$testdirs $2"; shift ;;
	-d*)
		testdirs="$testdirs ` expr $1 : '-d\(.*\)' `"; ;;

	-h|--help)
		echo "$usage"
		exit 0 ;;

	-j|--jobs)
		jfactor="-j$2"; shift ;;
	-j*)
		jfactor="-j` expr $1 : '-j\(.*\)' `" ;;
	--jobs*)
		jfactor="--jobs` expr $1 : '--jobs\(.*\)' `" ;;

	-k|--keep-objs)
		keep_objs=true ;;

	-m|--mmake)
		mmake_opts="$mmake_opts $2"; shift ;;

	-o|--output-file)
		outfile="$2"; shift ;;
	-o*)
		outfile="` expr $1 : '-o\(.*\)' `"; ;;

	-G|--grade)
		grade="$2"; shift ;;
	-G*)
		grade="` expr $1 : '-G\(.*\)' `"; ;;

	--test-grade)
		test_grade="$2"; shift ;;

	-r|--copy-runtime)
		copy_runtime=true ;;

	-g|--copy-boehm-gc)
		copy_boehm_gc=true ;;

	-p|--copy-profiler)
		copy_profiler=true ;;

	-t|--no-test-suite)
		runtests=false ;;

	-T|--test-suite-only)
		do_bootcheck=false ;;

	-2|--keep-stage-2)
		keep_stage_2=true ;;

	-3|--keep-stage-3)
		keep_stage_3=true ;;

	--stop-after-stage-2)
		stop_after_stage_2=true ;;

	--use-subdirs)
		use_subdirs=yes ;;
	--no-use-subdirs)
		use_subdirs=no ;;

	--)	
		shift; break ;;
	-*)
		echo "$0: unknown option \`$1'" 1>&2
		echo "$usage" 1>&2
		exit 1 ;;
	*)
		break ;;
	esac
	shift
done

if [ $# -ne 0 ]; then
	echo "$0: unexpected argument(s) \`$*'" 1>&2
	echo "$usage" 1>&2
	exit 1
fi

if test "$grade" != "" -a "$test_grade" = ""
then
	test_grade="$grade"
fi

case $use_subdirs in
	yes)	cs_subdir=Mercury/cs/
		;;
	no)	cs_subdir=
		;;
esac

if test "$grade" != "" -o "$test_grade" != ""
then
	copy_runtime=true
	copy_boehm_gc=true
fi

MMAKE_USE_SUBDIRS=$use_subdirs
export MMAKE_USE_SUBDIRS

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

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

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

# Turn off the debugger, since accidentally leaving it on will result
# in user interaction every time we invoke any version of the compiler
# that was compiled with tracing. This has happened to me accidentally 
# one too many times - zs.
if echo $MERCURY_OPTIONS | grep '\-Di' > /dev/null
then
	MERCURY_OPTIONS=`echo $MERCURY_OPTIONS | sed -e 's/-Di//'`
	export MERCURY_OPTIONS
fi

echo "starting at `date`"

set -x

root=`/bin/pwd | sed '
s:/mount/munkora/mercury:/home/mercury:
s:/mount/munkora/home/mercury:/home/mercury:
s:/mount/munkora/clp/mercury:/home/mercury:'`
PATH=$root/tools:$PATH
export PATH

# Try the command given by the environment variable RMSTAGECMD to delete
# stage directories. Since this command may not always work due to
# security considerations (.rhost files), we execute /bin/rm -fr afterwards
# in any case.
if test "$RMSTAGECMD" = ""
then
	RMSTAGECMD="/bin/rm -fr"
fi

if $do_bootcheck
then
	if mmake $mmake_opts MMAKEFLAGS=$jfactor all
	then
		echo "building of stage 1 successful"
	else
		cd $root/library;
		mmake depend
		cd $root/browser;
		mmake depend
		cd $root/compiler;
		mmake depend
		cd $root/profiler;
		mmake depend
		cd $root
		if mmake $mmake_opts MMAKEFLAGS=$jfactor all
		then
			echo "building of stage 1 successful"
		else
			echo "building of stage 1 not successful"
			exit 1
		fi
	fi

	# this is only for a transition period,
	# until all stage1 dirs have been removed
	/bin/rm -fr stage1

	MERCURY_COMPILER=$root/compiler/mercury_compile
	export MERCURY_COMPILER
	MERCURY_INT_DIR=$root/stage2/library
	export MERCURY_INT_DIR

	[ -d stage2 ] || mkdir stage2
	if $keep_stage_2
	then
		echo keeping existing stage2
	else
		# We try to do the removal of the stage 2 directory in parallel
		# since recursive rm's across NFS can be quite slow ...
		$RMSTAGECMD $root/stage2/compiler < /dev/null &
		$RMSTAGECMD $root/stage2/library < /dev/null &
		wait
		$RMSTAGECMD $root/stage2/* < /dev/null
		/bin/rm -fr $root/stage2/* < /dev/null
		/bin/rm -fr $root/stage2/.[a-zA-Z]* < /dev/null
	fi

	set +x
	echo linking stage 2... 1>&2
	cd stage2
	mkdir compiler
	cd compiler
	# Break up the links into several chunks.
	# This is needed to cope with small limits
	# on the size of argument vectors.

	$LN_S $root/compiler/[a-h]*.m .
	$LN_S $root/compiler/[i-o]*.m .
	$LN_S $root/compiler/[p-s]*.m .
	$LN_S $root/compiler/[t-z]*.m .
	$LN_S $root/compiler/*.pp .	

	cp $root/compiler/Mmake* .
	cd $root/stage2
	mkdir library
	cd library
	$LN_S $root/library/library.nu.nl.in .
	$LN_S $root/library/[a-l]*.m .
	$LN_S $root/library/[m-z]*.m .
	$LN_S $root/library/*.nl .
	cp $root/library/Mmake* .
	$LN_S $root/library/$STD_LIB_NAME.init .
	cd $root/stage2
	mkdir browser
	cd browser
	$LN_S $root/browser/*.m .
	cp $root/browser/Mmake* .
	$LN_S $root/browser/$BROWSER_LIB_NAME.init .
	cd $root/stage2
	if $copy_runtime
	then
		# Remove symbolic link to the stage 1 runtime if it's present,
		# which it can be with the -2 option.
		rm -f runtime
		mkdir runtime
		cd runtime
		$LN_S $root/runtime/*.h .
		$LN_S $root/runtime/*.c .
		$LN_S $root/runtime/*.in .
		cp $root/runtime/Mmake* .
		$LN_S $root/runtime/machdeps .
		cd $root/stage2
		rm -f trace
		mkdir trace
		cd trace
		$LN_S $root/trace/*.h .
		$LN_S $root/trace/*.c .
		cp $root/trace/Mmake* .
		cd $root/stage2
	else
		$LN_S $root/runtime .
		$LN_S $root/trace .
	fi
	if $copy_boehm_gc
	then
		# Remove symbolic link to the stage 1 gc if it's present,
		# which it can be with the -2 option.
		rm -f boehm_gc
		mkdir boehm_gc
		cd boehm_gc
		$LN_S $root/boehm_gc/*.h .
		$LN_S $root/boehm_gc/*.c .
		$LN_S $root/boehm_gc/*.s .
		$LN_S $root/boehm_gc/include .
		cp $root/boehm_gc/Mmake* .
		cp $root/boehm_gc/Makefile .
		$LN_S $root/boehm_gc/machdeps .
		cd $root/stage2
	else
		$LN_S $root/boehm_gc .
	fi
	$LN_S $root/doc .
	$LN_S $root/scripts .
	$LN_S $root/util .
	if $copy_profiler
	then
		mkdir profiler
		cd profiler
		$LN_S $root/profiler/*.m .
		cp $root/profiler/Mmake* .
		cd $root/stage2
	else
		$LN_S $root/profiler .
	fi
	$LN_S $root/conf* .
	$LN_S $root/aclocal.m4 .
	$LN_S $root/VERSION .
	$LN_S $root/.*.in .
	rm -f config*.log
	cp $root/Mmake* .
	if test -f $root/Mmake.stage.params
	then
		/bin/rm -f Mmake.params
		cp $root/Mmake.stage.params Mmake.params
	fi
	if test "$grade" != ""
	then
		echo "GRADE = $grade" >> Mmake.params
	fi
	cd $root

	set -x

	MMAKE_VPATH=.
	export MMAKE_VPATH
	MMAKE_DIR=$root/scripts
	export MMAKE_DIR

	# Use the new mmake to build stage 2
	MMAKE=$MMAKE_DIR/mmake

	if (cd stage2 && $MMAKE $mmake_opts $jfactor runtime)
	then
		echo "building of stage 2 runtime successful"
	else
		echo "building of stage 2 runtime not successful"
		exit 1
	fi

	if (cd stage2 && $MMAKE $mmake_opts depend_library depend_browser \
		depend_compiler depend_profiler)
	then
		echo "building of stage 2 dependencies successful"
	else
		echo "building of stage 2 dependencies not successful"
		exit 1
	fi

	# the `RM_C=:' ensures that the `.c' files do not get deleted

	if (cd stage2/library && $MMAKE $mmake_opts $jfactor RM_C=: mercury)
	then
		echo "building of stage 2 library successful"
	else
		echo "building of stage 2 library not successful"
		exit 1
	fi

	if (cd stage2/browser && $MMAKE $mmake_opts $jfactor RM_C=: library)
	then
		echo "building of stage 2 browser successful"
	else
		echo "building of stage 2 browser not successful"
		exit 1
	fi

	if (cd stage2 && $MMAKE $mmake_opts $jfactor trace)
	then
		echo "building of stage 2 trace successful"
	else
		echo "building of stage 2 trace not successful"
		exit 1
	fi

	if (cd stage2/compiler && $MMAKE $mmake_opts $jfactor RM_C=: \
		mercury_compile)
	then
		echo "building of stage 2 compiler successful"
	else
		echo "building of stage 2 compiler not successful"
		exit 1
	fi

	if (cd stage2 && $MMAKE $mmake_opts MMAKEFLAGS=$jfactor all)
	then
		echo "building of stage 2 successful"
	else
		echo "building of stage 2 not successful"
		exit 1
	fi

	if $stop_after_stage_2
	then
		echo "stopping after building stage 2"
		exit 0
	fi

	# We can remove the library object files now,
	# but we will keep the compiler objects for a while longer
	if $keep_objs
	then
		true
	else
		find $root/stage2/library -name "*.o" -print |
		xargs /bin/rm -f
	fi

	MERCURY_COMPILER=$root/stage2/compiler/mercury_compile
	export MERCURY_COMPILER
	MERCURY_INT_DIR=$root/stage3/library
	export MERCURY_INT_DIR

	[ -d stage3 ] || mkdir stage3
	if $keep_stage_3
	then
		echo keeping existing stage3
	else
		# We try to do the removal of the stage 3 directory in parallel
		# since recursive rm's across NFS can be quite slow ...
		$RMSTAGECMD $root/stage3/compiler < /dev/null &
		$RMSTAGECMD $root/stage3/library < /dev/null &
		wait
		$RMSTAGECMD $root/stage3/* < /dev/null
		/bin/rm -fr $root/stage3/* < /dev/null
		/bin/rm -fr $root/stage3/.[a-zA-Z]* < /dev/null
	fi

	echo linking stage 3... 1>&2
	set +x
	cd stage3
	mkdir compiler
	cd compiler
	# Break up the links into several chunks.
	# This is needed to cope with small limits
	# on the size of argument vectors.
	$LN_S $root/compiler/[a-h]*.m .
	$LN_S $root/compiler/[i-o]*.m .
	$LN_S $root/compiler/[p-s]*.m .
	$LN_S $root/compiler/[t-z]*.m .
	$LN_S $root/compiler/*.pp .	

	cp $root/compiler/Mmake* .
	cd $root/stage3
	mkdir library
	cd library
	$LN_S $root/library/library.nu.nl.in .
	$LN_S $root/library/[a-l]*.m .
	$LN_S $root/library/[m-z]*.m .
	$LN_S $root/library/*.nl .
	cp $root/library/Mmake* .
	$LN_S $root/library/$STD_LIB_NAME.init .
	cd $root/stage3
	mkdir browser
	cd browser
	$LN_S $root/browser/*.m .
	cp $root/browser/Mmake* .
	$LN_S $root/browser/$BROWSER_LIB_NAME.init .
	cd $root/stage3
	$LN_S $root/boehm_gc .
	$LN_S $root/doc .
	$LN_S $root/runtime .
	$LN_S $root/trace .
	$LN_S $root/scripts .
	$LN_S $root/util .
	$LN_S $root/profiler .
	$LN_S $root/conf* .
	$LN_S $root/aclocal.m4 .
	$LN_S $root/VERSION .
	$LN_S $root/.*.in .
	rm -f config*.log
	cp $root/stage2/Mmake* .
	cd $root
	set -x

	MMAKE_VPATH=.
	export MMAKE_VPATH
	MMAKE_DIR=$root/scripts
	export MMAKE_DIR

	# Use the new mmake to build stage 3
	MMAKE=$MMAKE_DIR/mmake

	if (cd stage3 && $MMAKE $mmake_opts depend_library depend_browser \
		depend_compiler)
	then
		echo "building of stage 3 dependencies successful"
	else
		echo "building of stage 3 dependencies not successful"
		exit 1
	fi

	if 	(cd stage3/library &&
		$MMAKE $mmake_opts $jfactor all-ints &&
		$MMAKE $mmake_opts $jfactor cs)
	then
		echo "building of stage 3 library successful"
	else
		echo "building of stage 3 library initially not successful"
		df .
		# try again, in case the failure cause was transient
		if 	(cd stage3/library &&
			$MMAKE $mmake_opts $jfactor all-ints &&
			$MMAKE $mmake_opts $jfactor cs)
		then
			echo "building of stage 3 library successful"
		else
			echo "building of stage 3 library not successful"
			exit 1
		fi
	fi

	# We delay deleting the stage 2 compiler objects until now,
	# so that if (a) an error manifests itself during the creation
	# of the stage 3 library, and (b) this error can be fixed by
	# changing the runtime, a bootcheck -2, which requires a relink,
	# will not have to expensively recreate the stage 2 compiler objects.

	if $keep_objs
	then
		true
	else
		find $root/stage2/compiler -name "*.o" -print |
		xargs /bin/rm -f
	fi

	if (cd stage3/browser && $MMAKE $mmake_opts $jfactor cs)
	then
		echo "building of stage 3 browser successful"
	else
		echo "building of stage 3 browser initially not successful"
		df .
		# try again, in case the failure cause was transient
		if (cd stage3/browser && $MMAKE $mmake_opts $jfactor cs)
		then
			echo "building of stage 3 browser successful"
		else
			echo "building of stage 3 browser not successful"
			exit 1
		fi
	fi

	if (cd stage3/compiler && $MMAKE $mmake_opts $jfactor cs)
	then
		echo "building of stage 3 compiler successful"
	else
		echo "building of stage 3 compiler initially not successful"
		df .
		# try again, in case the failure cause was transient
		if (cd stage3/compiler && $MMAKE $mmake_opts $jfactor cs)
		then
			echo "building of stage 3 compiler successful"
		else
			echo "building of stage 3 compiler not successful"
			exit 1
		fi
	fi

	diff_status=0

	exec 3>&1		# save stdout in fd 3
	if [ -n "$outfile" ]
	then
		exec > "$outfile"	# redirect stdout to $outfile
	fi

	for dir in library browser compiler; do
		for file in stage2/$dir/${cs_subdir}*.c; do
		    diff -u $file stage3/$dir/${cs_subdir}`basename $file` ||
			diff_status=1
		done
	done

	exec >&3		# restore stdout from fd 3
	if [ $diff_status -ne 0 ]; then
		echo "error - stage 2 and stage 3 differ!"
	else
		echo "stage 2 and stage 3 compare ok"
		echo "removing stage 3..."
		# We try to do the removal of the stage 3 directory in parallel
		# since recursive rm's across NFS can be quite slow ...
		$RMSTAGECMD $root/stage3/compiler < /dev/null &
		$RMSTAGECMD $root/stage3/library < /dev/null &
		wait
		$RMSTAGECMD $root/stage3/* < /dev/null
		/bin/rm -fr $root/stage3/* < /dev/null
		/bin/rm -fr $root/stage3/.[a-zA-Z]* < /dev/null
	fi

	echo "finishing stage3 at `date`"
else
	diff_status=0
	echo "building of stages 1 and 2 skipped"
fi

if $runtests
then
	# Use everything from stage 2, the options say that the tests
	# should be done in grade different from the grade of stage 2,
	# in which case use everything from stage 1, trusting the user
	# that the grade of the tests and the grade of stage 1 are compatible.

	MERCURY_COMPILER=$root/stage2/compiler/mercury_compile
	export MERCURY_COMPILER

	if test "$test_grade" = "$grade"
	then
		stage2_insert="stage2/"
	else
		stage2_insert=""
	fi

	MERCURY_INT_DIR=$root/${stage2_insert}library
	export MERCURY_INT_DIR

	MERCURY_LIBS="
		$root/${stage2_insert}trace/lib$TRACE_LIB_NAME.a
		$root/${stage2_insert}browser/lib$BROWSER_LIB_NAME.a
		$root/${stage2_insert}library/lib$STD_LIB_NAME.a
		$root/${stage2_insert}runtime/lib$RT_LIB_NAME.a
		$root/${stage2_insert}boehm_gc/libgc.a
		-lm"
	export MERCURY_LIBS

	MERCURY_ALL_C_INCL_DIRS="-I$root/${stage2_insert}trace
		-I$root/${stage2_insert}runtime
		-I$root/${stage2_insert}boehm_gc
		-I$root/${stage2_insert}boehm_gc/include"
	export MERCURY_ALL_C_INCL_DIRS

	MMAKE_DIR="$root/${stage2_insert}scripts"
	export MMAKE_DIR

	MERCURY_MOD_LIB_MODS="
		$root/${stage2_insert}browser/$BROWSER_LIB_NAME.init
		$root/${stage2_insert}library/$STD_LIB_NAME.init
		$root/${stage2_insert}runtime/$RT_LIB_NAME.init"
	export MERCURY_MOD_LIB_MODS

		# for mkinit, mmc, mgnuc, ml etc
	PATH=$root/${stage2_insert}util:$root/${stage2_insert}scripts:$PATH
	export PATH

	# We need to give tests/debugger access to the mdbrc and mdb_doc
	# files in the doc and scripts directories, without hardcoding their
	# pathnames. We must also compensate for scripts/mdbrc having hardcoded
	# within it the *installed* pathname of mdb_doc and not its current
	# pathname.
	cat $root/doc/mdb_doc > $root/scripts/test_mdbrc
	sed -e '/^source/d' $root/scripts/mdbrc >> $root/scripts/test_mdbrc
	MERCURY_DEBUGGER_INIT=$root/scripts/test_mdbrc
	export MERCURY_DEBUGGER_INIT

	if test "$test_grade" != ""
	then
		test_grade_opt="-g $test_grade"
	else
		test_grade_opt=""
	fi

	test_status=0
	if test -d ../tests
	then
		tests_prefix="../"
	elif test -d tests
	then
		tests_prefix=""
	else
		echo "cannot find test directory"
		test_status=1
	fi

	if test "$test_status" = 0
	then
		cp $root/doc/mdb_command_test.inp ${tests_prefix}tests/debugger
		if test "$testdirs" = ""
		then
			cd ${tests_prefix}tests
			./runtests $jfactor $test_grade_opt
		else
			for testdir in $testdirs
			do
				cd $root/${tests_prefix}tests/$testdir
				./runtests $jfactor $test_grade_opt
			done
		fi
		test_status=$?
		cd $root
	fi
else
	test_status=0
fi

echo "finishing at `date`"

if test "$diff_status" = 0 -a "$test_status" = 0
then
	exit 0
else
	exit 1
fi
