#!/bin/bash
# This script installs the page fault test
P=pft-install
DEFAULT_VERSION=0.12x
. $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/gormanm/pft
MIRROR_LOCATION="$WEBROOT/pft/$TARFILE"

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

install-depends libnuma-devel

# Patch it if necessary
pushd $SHELLPACK_SOURCES/pft-${VERSION}-installed
if [ ! -e /usr/include/numa.h ]; then
	echo WARNING: PFT REQUIRES NUMA.H AND IT IS NOT AVAILABLE
	echo WORKING AROUND, BUT MAY NOT BEHAVE AS EXPECTED
echo "
diff --git a/Makefile b/Makefile
index bd88168..2c87f71 100644
--- a/Makefile
+++ b/Makefile
@@ -22,7 +22,7 @@ CFLAGS  = \$(COPT) \$(DEFS) \$(INCLS) \$(ECFLAGS)
 
 LDOPTS	= #-dnon_shared
 # comment out '-lnuma' for platforms w/o libnuma -- laptops?
-LDLIBS	= -lpthread -lrt -lnuma
+LDLIBS	= -lpthread -lrt
 LDFLAGS = \$(CMODE) \$(LDOPTS) \$(ELDFLAGS)
 
 HDRS    =
@@ -32,7 +32,7 @@ OBJS    = pft.o
 EXTRAHDRS =
 
 # Include 'numa_stubs.o' for platforms w/o libnuma -- laptops?
-EXTRAOBJS = /usr/include/numa.h /usr/include/numaif.h
+EXTRAOBJS = 
 
 PROGS	= pft
 
diff --git a/numa_stubs.h b/numa_stubs.h
new file mode 100644
index 0000000..e2aa947
--- /dev/null
+++ b/numa_stubs.h
@@ -0,0 +1,32 @@
+#ifndef __NUMA_STUBS_H
+#define __NUMA_STUBS_H
+
+static inline int numa_bitmask_isbitset(const void *bmp, unsigned int n)
+{
+	return 1;
+}
+
+static inline void *numa_allocate_cpumask(void)
+{
+	return NULL;
+}
+
+static inline int numa_sched_getaffinity(pid_t pid, void *mask)
+{
+	return 0;
+}
+
+static inline int numa_num_task_cpus(void)
+{
+	return 32;
+}
+
+#define MPOL_PREFERRED 0
+static inline int mbind(void *addr, unsigned long len, int mode,
+		unsigned long *nodemask, unsigned long maxnode,
+		unsigned flags)
+{
+	return 0;
+}
+
+#endif
diff --git a/pft.c b/pft.c
index 2ea0986..5544803 100644
--- a/pft.c
+++ b/pft.c
@@ -26,8 +26,6 @@
 #include <ctype.h>
 #include <errno.h>
 #include <fcntl.h>
-#include <numa.h>
-#include <numaif.h>
 #include <pthread.h>
 #include <sched.h>
 #include <stdarg.h>
@@ -38,6 +36,7 @@
 #include <unistd.h>
 
 #include \"version.h\"
+#include \"numa_stubs.h\"
 
 #if defined(USE_RUSAGE_THREAD) && !defined(RUSAGE_THREAD)
 #define RUSAGE_THREAD 1
" | patch -p1 || exit -1
fi

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

echo pft installed successfully
#### Description pft
#### Details pft 12
