#!/bin/bash
# fio installer
P=fio-install
DEFAULT_VERSION=2.99
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
WEB_LOCATION=http://brick.kernel.dk/snaps
MIRROR_LOCATION="$WEBROOT/fio"

install-depends glibc-devel

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

pushd $SHELLPACK_SOURCES/fio-${VERSION}-installed || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done
LINESTART=`grep -n "==== BEGIN mutex-fix-double-unlock.patch" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END mutex-fix-double-unlock.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 mutex-fix-double-unlock.patch
fi
echo Extracting $SHELLPACK_TEMP/mutex-fix-double-unlock.patch
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/mutex-fix-double-unlock.patch
cat $SHELLPACK_TEMP/mutex-fix-double-unlock.patch | patch -p0
make -j$NUMCPUS
if [ $? -ne 0 ]; then
LINESTART=`grep -n "==== BEGIN fio-types.patch" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END fio-types.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 fio-types.patch
fi
echo Extracting $SHELLPACK_TEMP/fio-types.patch
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/fio-types.patch
	cat $SHELLPACK_TEMP/fio-types.patch | patch -p0 || die Failed to apply fio types fix
	make -j$NUMCPUS
	if [ $? -ne 0 ]; then
		error "$P: make failed"
		popd > /dev/null
		exit $SHELLPACK_ERROR
	fi
fi

echo fio installed successfully
exit $SHELLPACK_SUCCESS

==== BEGIN fio-types.patch ====
--- oslib/libmtd_legacy.c.orig	2016-05-20 15:46:59.248153123 +0200
+++ oslib/libmtd_legacy.c	2016-05-20 15:47:03.404249326 +0200
@@ -29,6 +29,7 @@
 #include <fcntl.h>
 #include <unistd.h>
 #include <stdlib.h>
+#include <inttypes.h>
 #include <errno.h>
 #include <sys/types.h>
 #include <sys/stat.h>
==== END fio-types.patch ====
#### Description Flexible IO tester
#### Details fio 24
