#!/bin/sh
#
# A program to test different versions of the compiler.

usage="Usage: speedtest [-c cmd] [-dns] batchname"
limit=12
cmd="mmc -C -O2 --grade asm_fast.gc llds_out.m typecheck.m make_hlds.m modules.m code_info.m polymorphism.m"
debug=false
table_io=false
size=false
framesizefile=""

while test $# -gt 0
do
	case $1 in

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

	-d)
		debug=true ;;

	-t)
		table_io=true ;;

	-f)
		framesizefile="$2" ; shift ;;
	-f*)
		framesizefile="` expr $1 : '-f\(.*\)' `" ;;

	-n)
		limit="$2" ; shift ;;
	-n*)
		limit="` expr $1 : '-n\(.*\)' `" ;;

	-s)
		size=true ;;

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

	*)	break ;;

	esac
	shift
done

if test $# != 1
then
	echo $usage
	exit 1
fi

batch=$1

root=`/bin/pwd`
files=`ls batch/$batch.mercury_compile.*`

trap 'gzip $root/batch/$batch.mercury_compile.*[0-9] > /dev/null 2>&1; exit 0' 0 1 2 3 15 

if test -x /usr/ucb/echo
then
	ECHO=/usr/ucb/echo
else
	ECHO=echo
fi

for file in $files
do
	case $file in
	*.gz)
		gunzip $file
		file=batch/`basename $file .gz`
		;;
	esac

	paramfile=`echo $file | sed 's/mercury_compile/params/'`
	if test -r $paramfile
	then
		cat $paramfile
	fi

	if $size
	then
		size $file
	fi

	MERCURY_COMPILER=$root/$file
	export MERCURY_COMPILER
	cd arena
	count=1
	while test $count -le $limit
	do
		if test "$framesizefile" != ""
		then
			rm $framesizefile > /dev/null 2>&1
		fi

		briefname=`echo "$file" | sed "s:batch/$batch.::"`
		$ECHO -n "$briefname "
		if $debug
		then
			if $table_io
			then
				(echo "table_io start" ; echo "c" ) | $root/tools/dotime mdb $cmd
			else
				echo "c" | $root/tools/dotime mdb $cmd
			fi
		else
			$root/tools/dotime $cmd
		fi

		if test -s Deep.data
		then
			mv Deep.data ../batch/Deep.data.`basename $file .gz`.run$count
		fi

		if test "$count" -eq 1 -a "$framesizefile" != ""
		then
			echo
			cat $framesizefile
			echo
		fi

		count=`expr $count + 1`
	done

	cd $root
	gzip $file
done

exit 0
