#!/bin/bash
P=sysbench-install
DEFAULT_VERSION=0.4.12
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

VERSION=0.4.12
DBUSER=root
DBPASSWORD=shellpack-mysqlbench
DBDRIVER=postgres

# 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
		;;
	--dbdriver)
		DBDRIVER=$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

WEB_LOCATION=http://www.skynet.ie/~mel/mmtests-archive/sysbench
MIRROR_LOCATION=$WEBROOT/sysbench

# Select database driver
case $DBDRIVER in
mariadb)
	echo Checking mariadb installation
	DBDRIVER_VERSION=10.2.14
	CONFIGURE_ARGS=
	;;
postgres)
	echo Checking postgres installation
	DBDRIVER_VERSION=9.6.1
	CONFIGURE_ARGS="--without-mysql --with-pgsql"
	;;
*)
	die Unrecognised dbdriver $DBDRIVER
	;;
esac

# Build database and start it for sysbench config
$SHELLPACK_INCLUDE/shellpack-bench-${DBDRIVER}build --start \
        --effective_cachesize $((MEMTOTAL_BYTES*6/10)) \
        --shared_buffers $((MEMTOTAL_BYTES/4)) \
        --work_mem $((16*1048576)) || die Failed to get usable database installation

echo Building sysbench for $DBDRIVER
export PATH=$SHELLPACK_SOURCES/${DBDRIVER}build-${DBDRIVER_VERSION}-installed/bin:$PATH
# Unconditionally fetch the tar to find out the real version number
TARFILE=sysbench-${VERSION}.tar.gz
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 sysbench-${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 sysbench-${VERSION}
pushd sysbench-${VERSION} > /dev/null || die Failed to rename tar
pushd $SHELLPACK_SOURCES/sysbench-${VERSION} || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done
touch NEWS
touch AUTHORS
autoreconf -f -i || die Failed to run autoreconf
# Patch sysbench to properly insert tables
echo "--- sysbench/drivers/pgsql/drv_pgsql.c.orig	2010-12-03 14:14:59.000000000 +0000
+++ sysbench/drivers/pgsql/drv_pgsql.c	2010-12-03 14:15:23.000000000 +0000
@@ -84,7 +84,7 @@
 
 static drv_caps_t pgsql_drv_caps =
 {
-  0,
+  1,
   1,
   1,
   0,
" | patch -p0
export CFLAGS="-O2 $CFLAGS_MMTESTS_EXTRA"
eval ./configure --prefix=$SHELLPACK_SOURCES/sysbench-${VERSION}-installed $CONFIGURE_ARGS
if [ $? -ne 0 ]; then
	cp /usr/share/automake*/config.guess .
	cp /usr/share/automake*/config.sub .
	eval ./configure --prefix=$SHELLPACK_SOURCES/sysbench-${VERSION}-installed $CONFIGURE_ARGS
	if [ $? -ne 0 ]; then
		error "$P: configure failed"
		popd > /dev/null
		exit $SHELLPACK_ERROR
	fi
fi
unset CFLAGS
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 sysbench for $DBDRIVER successfully installed
exit $SHELLPACK_SUCCESS
#### Description Run the sysbench benchmark
#### Details sysbench 86
