#! /bin/sh

# the directory containing the results for this test run
resultdir=

# the directory containing the base test results to compare against
baseresultdir=

# the directory containing the last test results to compare against
oldresultdir=

# contains the name of the JVM to run on (hard-coded later on)
jvm=

# what architecture are we running on
arch=

# timestamp for this set of results
timestamp=`date +%Y%m%d%H%M%S`

# directory containing the last set of results (if it exists)
oldtimestamp=

# run diffs against base and last test run?
lastregression=
baseregression=


if [ -z $RESULT_HOME ]; then
	echo "You must set RESULT_HOME before running this script"
	exit 1
fi

# check architecture
temp=`uname -m`
case $temp in
	i*86 ) 
	arch="x86";;
	* ) 
	arch="$temp";;
esac

# find old set of results
oldresults=`ls $RESULT_HOME | tail -n 1`

setup()
{
	# See if there are previous test run results
	if [ -d $RESULT_HOME/$oldresults/$arch/$jvm ]
		then
			oldresultdir="$RESULT_HOME/$oldresults/$arch/$jvm"
		else
			oldresultdir=""
	fi
	
	# Find the directory containing the base test run results
	if [ -d $RESULT_HOME/base/$arch/Sun ]
		then
			baseresultdir="$RESULT_HOME/base/$arch/Sun"
		else
			baseresultdir=""
	fi

	# Create the results directory for this test run
	mkdir -p "$RESULT_HOME/results-$timestamp/$arch/$jvm"
	resultdir="$RESULT_HOME/results-$timestamp/$arch/$jvm"

	# Set up the argument line for runtests
	argline="-os linux -ws gtk -arch $arch -noclean -results $resultdir"
	
	if [ -z $oldresultdir ]
		then
			lastregression="false"
		else
			lastregression="true"
			argline="$argline -oldresults $oldresultdir"
	fi
	if [ -z $baseresultdir ]
		then
			baseregression="false"
		else
			baseregression="true"
			argline="$argline -baseresults $baseresultdir"
	fi
	argline="$argline ant"
}

runtests()
{
	echo -e "TEST CHANGE SUMMARY $timestamp\n" >> $resultdir/summary
	./runtests $argline > $resultdir/driverlog 2>&1 || echo -e "An error occurred while running the test driver.\nThis may be serious, or maybe not; see driverlog for details.\n" >> $resultdir/summary
}

summarize()
{
	if [ "x$lastregression" = "xtrue" ]; then	
		echo -e "\nCHANGE SUMMARY RELATIVE TO LAST TEST RUN:" >> $resultdir/summary
		echo "-----------------------------------------" >> $resultdir/summary
		cat $resultdir/diffs-last/summary >> $resultdir/summary
	fi

	if [ "x$baseregression" = "xtrue" ]; then
		echo -e "\nCHANGE SUMMARY RELATIVE TO BASE:" >> $resultdir/summary
		echo "--------------------------------" >> $resultdir/summary
		cat $resultdir/diffs-base/summary >> $resultdir/summary
	fi	
}

run_ibm()
{
	# Run using IBM
	jvm="IBM"
	export JAVA_HOME=/opt/IBMJava2-141
	setup
	runtests
	summarize
}

run_sun()
{
	# Run using Sun
	jvm="Sun"
	export JAVA_HOME=/usr/java/j2sdk1.4.2_01
	setup
	runtests
	summarize
}

run_libgcj()
{
	# Run using libgcj (use Sun JVM for the test driver)
	jvm="libgcj"
	export JAVA_HOME=/usr/java/j2sdk1.4.2_01
	setup
	argline="$argline -native"
	runtests
	summarize

	# These tests currently don't run, so just do a startup smoke test
	cd eclipse
	exec ./eclipse -vm ./gcjlauncher || echo -e "\n*** Startup smoke test failed for native eclipse\n" >> $resultdir/summary &
	sleep 20
	killall eclipse gcjlauncher
	cd ..
}

# Run the tests!
run_ibm
run_sun
run_libgcj
