#!/bin/bash
# ffsb installer
P=ffsb-install
DEFAULT_VERSION=6.0-rc2
. $SHELLPACK_INCLUDE/common.sh
WEB_LOCATION=$WEB_LOCATION/ffsb-$VERSION
MIRROR_LOCATION="$WEBROOT/ffsb/"

# Basic argument parser
while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

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

pushd $SHELLPACK_SOURCES/ffsb-${VERSION} || die Failed to change to source directory
echo '--- ffsb-6.0-rc2-orig/parser.c	2008-10-27 20:17:05.000000000 +0000
+++ ffsb-6.0-rc2/parser.c	2013-11-06 16:28:17.897964902 +0000
@@ -203,9 +203,9 @@
 	len = strnlen(string, BUFSIZE);
 	sprintf(search_str, "%s=%%%ds\\n", string, BUFSIZE - len-1);
 	if (1 == sscanf(line, search_str, &temp)) {
-		len = strnlen(temp, 4096);
-		ret_buf = malloc(len);
-		strncpy(ret_buf, temp, len);
+		len = strnlen(temp, BUFSIZE - len);
+		ret_buf = malloc(len + 1);
+		strncpy(ret_buf, temp, len + 1);
 		return ret_buf;
 		}
 	free(line);' | patch -p1 || exit $SHELLPACK_ERROR
./configure --prefix=$SHELLPACK_SOURCES/ffsb-${VERSION}-installed
if [ $? -ne 0 ]; then
	error "$P: configure failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
make -j$NUMCPUS
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi
make install
if [ $? -ne 0 ]; then
	error "$P: make install failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

echo ffsb installed successfully
#### Description Flexible FileSystem Benchmark
#### Details ffsb 11
