#!/bin/bash
P=simoop-install
DEFAULT_VERSION=0
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
GIT_LOCATION=git://git.kernel.org/pub/scm/linux/kernel/git/mason/simoop.git
MIRROR_LOCATION="$WEBROOT/simoop/"

# 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

# Unconditionally fetch the tar to find out the real version number
TARFILE=simoop-${VERSION}.tar.gz
git_fetch $GIT_LOCATION simoop-${VERSION}-installed $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE
cd $SHELLPACK_SOURCES
tar -xf $TARFILE
if [ $? -ne 0 ]; then
	error "$P: tar xf simoop-${VERSION}.tar.gz 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 simoop-${VERSION}-installed
pushd simoop-${VERSION}-installed > /dev/null || die Failed to rename tar

pushd $SHELLPACK_SOURCES/simoop-${VERSION}-installed || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done
LINESTART=`grep -n "==== BEGIN fix-overrun.patch" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END fix-overrun.patch" $0 | tail -1 | awk -F : '{print $1}'`
if [ "$LINEEND" = "" ]; then
	LINECOUNT=`wc -l $0 | awk '{print $1}'`
fi
if [ "$LINESTART" = "" ]; then
	die Failed to find start of file fix-overrun.patch
fi
echo Extracting $SHELLPACK_TEMP/fix-overrun.patch
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/fix-overrun.patch
cat $SHELLPACK_TEMP/fix-overrun.patch | patch -p1 || die Failed to apply fix-overrun patch
rm $SHELLPACK_TEMP/fix-overrun.patch
make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

echo simoop installed successfully
exit $SHELLPACK_SUCCESS

==== BEGIN fix-overrun.patch ====
diff --git a/simoop.c b/simoop.c
index 2069c0816c9d..3671c837c9b0 100644
--- a/simoop.c
+++ b/simoop.c
@@ -265,7 +265,7 @@ static void parse_options(int ac, char **av)
 		print_usage();
 		exit(1);
 	}
-	paths = malloc(sizeof(char *) * total_paths + 1);
+	paths = malloc(sizeof(char *) * (total_paths + 1));
 	paths[total_paths] = NULL;
 	for (i = 0; i < total_paths; i++) {
 		paths[i] = strdup(av[optind++]);
@@ -1148,7 +1148,6 @@ static void sleep_for_runtime(struct thread_data *worker_threads_mem)
 	unsigned long long delta;
 	unsigned long long rate_delta;
 	unsigned long long instant_delta;
-	unsigned long long runtime_usec = runtime * 1000000;
 	unsigned long long warmup_usec = warmup_seconds * 1000000;
 	double work_done = 0;
 	double instant_work_done = 0;
@@ -1187,7 +1186,7 @@ static void sleep_for_runtime(struct thread_data *worker_threads_mem)
 
 		instant_allocstalls = allocstalls;
 		instant_work_done = work_done;
-		if (delta < runtime_usec)
+		if (delta < (unsigned long long)runtime * 1000000)
 			sleep(interval_seconds);
 		else
 			break;
==== END fix-overrun.patch ====
#### Description simoop
#### Details simoop 10
