#! /bin/sh

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

# 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 +%s`

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

# Check architecture
case `uname -m` in
  i[0-9]*86)
    arch=x86 ;;
  ia64)
    arch=ia64 ;;
  ppc)
    arch=ppc ;;
  x86_64)
    arch=x86_64 ;;
  *)
    echo "Unrecognized architecture: `uname -m`" 1>&2
    exit 1 ;;
esac

setup()
{
	# 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"
}

runtests()
{
	echo -e "TEST RUN $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
}

pinhole_extract()
{
	cd pinhole-extractor
	CLASSPATH=pinhole-extractor.jar:lib/rh-postgresql3.jar:lib/xerces.jar:lib/activation-20030319.jar:lib/mail-20031006.jar
	export CLASSPATH
	$JAVA_HOME/bin/java pinhole.PinholeExtractor -i $timestamp \
		-d "Nightly build" \
		-u jdbc:postgresql://toasty.toronto.redhat.com/pinhole?user=pinhole&password=pinhole \
		$resultdir/xml/*
	cd ..
}

run_ibm()
{
	# Run using IBM
	jvm="IBM"
	export JAVA_HOME=/usr/lib/jvm/java-1.4.1-ibm
	setup
	runtests
	pinhole_extract
}

run_libgcj()
{
	# Run using libgcj (use IBM JVM for the test driver)
	jvm="libgcj"
	export JAVA_HOME=/usr/lib/jvm/java-1.4.1-ibm
	setup
	argline="$argline -native"
	runtests
}

# FIXME: Remove the Oprofile plugins; 
# they cause problems with the UI tests
rm -rf test-eclipse/plugins/com.redhat.eclipse.oprofile*


# Check out and build pinhole-extractor
cvs -d /home/handcock/cvs co pinhole-extractor
cd pinhole-extractor && ant
cd ..

# Start up Xvfb instance to run the tests headless
/usr/X11R6/bin/Xvfb :1 -screen 0 1600x1200x32 &
export DISPLAY=:1.0

# Run the tests!
run_ibm
run_libgcj

# Clean up Xvfb
kill `ps -C Xvfb -o pid=`
