#!/bin/bash
P=unixbench-install
DEFAULT_VERSION=5.1.3
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
WEB_LOCATION=http://www.skynet.ie/~mel/mmtests-archive/unixbench
MIRROR_LOCATION="$WEBROOT/unixbench/"

# Basic argument parser
TASKSET_SERVER=
TASKSET_CLIENT=
SERVERSIDE_COMMAND=none
SERVERSIDE_NAME=`date +%Y%m%d-%H%M-%S`

while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--serverside-command)
		SERVERSIDE_COMMAND=$2
		shift 2
		;;
	--serverside-name)
		SERVERSIDE_NAME=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ "$TASKSET_SERVER" != "" ]; then
	echo TASKSET_SERVER: $TASKSET_SERVER
	echo TASKSET_CLIENT: $TASKSET_CLIENT
fi
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

rm -rf $SHELLPACK_SOURCES/unixbench-${VERSION}
# Unconditionally fetch the tar to find out the real version number
TARFILE=UnixBench${VERSION}.tgz
sources_fetch $WEB_LOCATION/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE $WEB_LOCATION_ALT/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf UnixBench${VERSION}.tgz failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

# Rename directory to something we expect.
DST_DIR=`tar tf $TARFILE | head -n 1 | awk -F / '{print $1}'`
mv $DST_DIR unixbench-${VERSION}-installed
pushd unixbench-${VERSION}-installed > /dev/null || die Failed to rename tar

pushd $SHELLPACK_SOURCES/unixbench-${VERSION}-installed || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done

TEMPFILE=`mktemp`
LINECOUNT=`wc -l $0 | awk '{print $1}'`
CSTART=`grep -n "BEGIN PATCH FILE" $0 | tail -1 | awk -F : '{print $1}'`
tail -$(($LINECOUNT-$CSTART)) $0 | grep -v "^###" > $TEMPFILE.patch

cat $TEMPFILE.patch | patch -p1
RETVAL=$?
rm $TEMPFILE $TEMPFILE.patch
if [ $RETVAL -ne 0 ]; then
	exit $SHELLPACK_ERROR
fi

make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

echo unixbench installed successfully
exit $SHELLPACK_SUCCESS

==== BEGIN PATCH FILE ====
diff --git a/Run b/Run
index d615935..7322003 100755
--- a/Run
+++ b/Run
@@ -106,7 +106,7 @@ my $TESTDIR = getDir('UB_TESTDIR', $BASEDIR . "/testdir");
 
 # Configure the categories to which tests can belong.
 my $testCats = {
-    'system'    => { 'name' => "System Benchmarks", 'maxCopies' => 16 },
+    'system'    => { 'name' => "System Benchmarks", 'maxCopies' => 0 },
     '2d'        => { 'name' => "2D Graphics Benchmarks", 'maxCopies' => 1 },
     '3d'        => { 'name' => "3D Graphics Benchmarks", 'maxCopies' => 1 },
     'misc'      => { 'name' => "Non-Index Benchmarks", 'maxCopies' => 16 },
@@ -1329,7 +1329,7 @@ sub runTests {
         # If the benchmark doesn't want to run with this many copies, skip it.
         my $cat = $params->{'cat'};
         my $maxCopies = $testCats->{$cat}{'maxCopies'};
-        next if ($copies > $maxCopies);
+        next if ($maxCopies > 0 && $copies > $maxCopies);
 
         # Run the benchmark.
         my $bresult = runBenchmark($bench, $params, $verbose, $logFile, $copies);
#### Description unixbench
#### Details unixbench 7
