#!/bin/bash
P=freqmine-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=https://github.com/davidlohr/freqmine.git
MIRROR_LOCATION="$WEBROOT/freqmine/"

install-depends gcc-c++

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

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

# Patch benchmark to make it a ro/rw IO benchmark
TEMPFILE=`mktemp`
LINECOUNT=`wc -l $0 | awk '{print $1}'`
PATCHSTART=`grep -n "BEGIN PATCH FILE" $0 | tail -1 | awk -F : '{print $1}'`
tail -$(($LINECOUNT-$PATCHSTART)) $0 | grep -v "^###" > $TEMPFILE
cat $TEMPFILE | patch -p1 || exit $SHELLPACK_FAILURE
rm $TEMPFILE

make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
echo untaring input data sets
TARFILE=inputsdat.tar.xz
cd $SHELLPACK_SOURCES/freqmine-${VERSION}-installed/inputs
tar xf $TARFILE

echo freqmine installed successfully
exit $SHELLPACK_SUCCESS

==== BEGIN PATCH FILE ====
diff --git a/buffer.cpp b/buffer.cpp
index c76043b..9784028 100644
--- a/buffer.cpp
+++ b/buffer.cpp
@@ -34,6 +34,7 @@ THE POSSIBILITY OF SUCH DAMAGE.
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include "buffer.h"
 #include "common.h"
 #include <malloc.h>
@@ -74,6 +75,7 @@ void memory::init()
 		printf("INIT: not enough memory to run this program\n");
 		exit(0);
 	}
+	memset(buffer[0], 0, sizeof(char) * BUFS_SMALL);
 	start[0] = buffer[0];
 	markbuf = buffer[0];
 	markcount = 0;
@@ -152,6 +154,7 @@ int memory::switchbuf(unsigned int i)  // creates a new buffer with size >= i
 		delete this;
 //		freebuf(); 
 	}
+	memset(buffer[bufcount], 0, sizeof(char) * rest[bufcount]);
 	start[bufcount] = buffer[bufcount]; 
 	return bufcount;
 } // switchbuf 
#### Description freqmine
#### Details freqmine 7
