#!/bin/bash
P=wis-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/antonblanchard/will-it-scale.git
MIRROR_LOCATION="$WEBROOT/wis/"

# 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

install-depends hwloc-devel

LINESTART=`grep -n "==== BEGIN align-alloc.patch" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END align-alloc.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 align-alloc.patch
fi
echo Extracting $SHELLPACK_TEMP/align-alloc.patch
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/align-alloc.patch

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

pushd $SHELLPACK_SOURCES/wis-${VERSION}-installed || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done
cat $SHELLPACK_TEMP/align-alloc.patch | patch -p1
sed -i -e 's/tmpfs/tmp/' tests/fallocate1.c
sed -i -e 's/\/tmp\/willitscale/willitscale/' tests/*.c
sed -i -e 's/\/tmp\/shared_area/shared_area/' main.c
make -j$NUMCPUS 
if [ $? -ne 0 ]; then
	error "$P: make failed"
	popd > /dev/null
	exit $SHELLPACK_ERROR
fi

echo will-it-scale installed successfully
exit $SHELLPACK_SUCCESS

==== BEGIN align-alloc.patch ====
diff --git a/tests/read3.c b/tests/read3.c
index fdbd33086caf..0faf23ba9195 100644
--- a/tests/read3.c
+++ b/tests/read3.c
@@ -17,7 +17,11 @@ void testcase(unsigned long long *iterations, unsigned long nr)
 	int fd = mkostemp(tmpfile, O_DIRECT);
 	char *p;
 
+#ifdef _ISOC11_SOURCE
 	buf = aligned_alloc(getpagesize(), FILESIZE);
+#else
+	posix_memalign(&buf, getpagesize(), FILESIZE);
+#endif
 	memset(buf, 0, FILESIZE);
 	assert(fd >= 0);
 	assert(write(fd, buf, FILESIZE) == FILESIZE);
diff --git a/tests/read4.c b/tests/read4.c
index cb097e753800..3f6e437e0bd4 100644
--- a/tests/read4.c
+++ b/tests/read4.c
@@ -28,7 +28,11 @@ void testcase(unsigned long long *iterations, unsigned long nr)
 	int fd = open(tmpfile, O_DIRECT|O_RDONLY);
 	char *p;
 
-	p = aligned_alloc(getpagesize(), BUFLEN);
+#ifdef _ISOC11_SOURCE
+        p = aligned_alloc(getpagesize(), FILESIZE);
+#else
+        posix_memalign(&p, getpagesize(), FILESIZE);
+#endif
 
 	while (1) {
 		int ret = read(fd, p, BUFLEN);
==== END align-alloc.patch ====
#### Description will-it-scale
#### Details wis 10
