#!/bin/sh

set -x

usage="$0 [-jN] [-o filename]"

jfactor=""
mmake_opts="-k"
outfile=""

while getopts j:m:o: flag
do
	case $flag in
	j)	jfactor="-j$OPTARG" ;;

	m)	mmake_opts="$mmake_opts $OPTARG" ;;

	o)	outfile=$OPTARG ;;

	\\?)	echo $usage;
		exit 1 ;;
	esac
done

shift `expr "$OPTIND" - 1`
if [ $# -ne 0 ]
then
	echo $usage
	exit 1
fi

echo "starting at `date`"

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

root=`/bin/pwd`

# the stage 1 compiler is copied to allow it to be worked on
# in parallel with the bootcheck itself.

[ -d stage1 ] || mkdir stage1

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

# .pp files are not necessary

[ -d stage2 ] || mkdir stage2
/bin/rm -fr stage2/*
mkdir stage2/compiler
mkdir stage2/library
ln -s $root/compiler/*.m stage2/compiler
ln -s $root/compiler/*.m.in stage2/compiler
ln -s $root/compiler/Mmake* stage2/compiler
ln -s $root/library/*.m stage2/library
ln -s $root/library/*.m.in stage2/library
ln -s $root/library/*.nl stage2/library
ln -s $root/library/Mmake* stage2/library
ln -s $root/library/library.init stage2/library
ln -s $root/boehm_gc stage2/boehm_gc
ln -s $root/doc stage2/doc
ln -s $root/runtime stage2/runtime
ln -s $root/scripts stage2/scripts
ln -s $root/util stage2/util
ln -s $root/profiler stage2/profiler
ln -s $root/Mmake* stage2
ln -s $root/conf* stage2

if (cd stage2 ; mmake $mmake_opts depend)
then
	echo "building of stage 2 dependencies successful"
else
	echo "building of stage 2 dependencies not successful"
	exit 1
fi

MMAKE_VPATH=.
export MMAKE_VPATH
MMAKE_VARS=../scripts/Mmake.vars
export MMAKE_VARS
MMAKE_RULES=../scripts/Mmake.rules
export MMAKE_RULES

if (cd stage2/library ; mmake $mmake_opts $jfactor ints)
then
	echo "building of stage 2 library ints successful"
else
	echo "building of stage 2 library ints not successful"
	exit 1
fi

if (cd stage2/library ; mmake $mmake_opts $jfactor libmercury)
then
	echo "building of stage 2 library successful"
else
	echo "building of stage 2 library not successful"
	exit 1
fi

if (cd stage2/compiler ; mmake $mmake_opts $jfactor ints)
then
	echo "building of stage 2 compiler ints successful"
else
	echo "building of stage 2 compiler ints not successful"
	exit 1
fi

if (cd stage2/compiler ; mmake $mmake_opts $jfactor mercury_compile)
then
	echo "building of stage 2 compiler successful"
else
	echo "building of stage 2 compiler not successful"
	exit 1
fi

unset MMAKE_VPATH
unset MMAKE_VARS
unset MMAKE_RULES

if (cd stage2 ; mmake $mmake_opts $jfactor all)
then
	echo "building of stage 2 successful"
else
	echo "building of stage 2 not successful"
	exit 1
fi

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

# .pp files are not necessary

[ -d stage3 ] || mkdir stage3
/bin/rm -fr stage3/*
mkdir stage3/compiler
mkdir stage3/library
ln -s $root/stage2/compiler/*.m stage3/compiler
ln -s $root/stage2/compiler/*.m.in stage3/compiler
ln -s $root/stage2/compiler/Mmake* stage3/compiler
ln -s $root/stage2/library/*.m stage3/library
ln -s $root/stage2/library/*.m.in stage3/library
ln -s $root/stage2/library/*.nl stage3/library
ln -s $root/stage2/library/Mmake* stage3/library
ln -s $root/stage2/library/library.init stage3/library
ln -s $root/stage2/boehm_gc stage3/boehm_gc
ln -s $root/stage2/doc stage3/doc
ln -s $root/stage2/runtime stage3/runtime
ln -s $root/stage2/scripts stage3/scripts
ln -s $root/stage2/util stage3/util
ln -s $root/stage2/profiler stage3/profiler
ln -s $root/stage2/Mmake* stage3
ln -s $root/stage2/conf* stage3

if (cd stage3 ; mmake $mmake_opts depend)
then
	echo "building of stage 3 dependencies successful"
else
	echo "building of stage 3 dependencies not successful"
	exit 1
fi

MMAKE_VPATH=.
export MMAKE_VPATH
MMAKE_VARS=../scripts/Mmake.vars
export MMAKE_VARS
MMAKE_RULES=../scripts/Mmake.rules
export MMAKE_RULES

if (cd stage3/library ; mmake $mmake_opts $jfactor ints)
then
	echo "building of stage 3 library ints successful"
else
	echo "building of stage 3 library ints not successful"
	exit 1
fi

if (cd stage3/library ; 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

if (cd stage3/compiler ; mmake $mmake_opts $jfactor ints)
then
	echo "building of stage 3 compiler ints successful"
else
	echo "building of stage 3 compiler ints not successful"
	exit 1
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 not successful"
	exit 1
fi

exit_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 compiler; do
	for file in stage2/$dir/*.c; do
		diff -u $file stage3/$dir/`basename $file` ||
			exit_status=1
	done
done

exec >&3		# restore stdout from fd 3
if [ $exit_status -ne 0 ]; then
	echo "error - stage 2 and stage 3 differ!"
else
	echo "stage 2 and stage 3 compare ok"
fi

echo "finishing at `date`"
exit $exit_status
