#!/bin/bash
# mutilate installer
P=mutilate-install
DEFAULT_VERSION=1
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi
GIT_LOCATION=git://github.com/leverich/mutilate.git
MIRROR_LOCATION="$WEBROOT/mutilate"

install-depends libevent-devel scons
install-depends gengetopt
install-depends zeromq-devel gcc-c++

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

pushd $SHELLPACK_SOURCES/mutilate-${VERSION} || die Failed to change to source directory
for FILE in `find -name "*"`; do
	touch $FILE
done

LINESTART=`grep -n "==== BEGIN gtod.patch" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END gtod.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 gtod.patch
fi
echo Extracting $SHELLPACK_TEMP/gtod.patch
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/gtod.patch
LINESTART=`grep -n "==== BEGIN scons.patch" $0 | tail -1 | awk -F : '{print $1}'`
LINEEND=`grep -n "==== END scons.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 scons.patch
fi
echo Extracting $SHELLPACK_TEMP/scons.patch
sed -n $((LINESTART+1)),$((LINEEND-1))p $0 > $SHELLPACK_TEMP/scons.patch
cat $SHELLPACK_TEMP/gtod.patch | patch -p1 || exit $SHELLPACK_FAILURE
cat $SHELLPACK_TEMP/scons.patch | patch -p1 || exit $SHELLPACK_FAILURE
scons
if [ $? -ne 0 ]; then
	echo
	echo Trying an alternative compiler: gcc47
	install-depends gcc47-c++
	echo "diff --git a/SConstruct b/SConstruct
index b782d90c38e4..c87e2ec61119 100644
--- a/SConstruct
+++ b/SConstruct
@@ -12,6 +12,7 @@ env.Append(CCFLAGS = '-std=c++0x -D_GNU_SOURCE') # -D__STDC_FORMAT_MACROS')
 if sys.platform == 'darwin':
     env['CC']  = 'clang'
     env['CXX'] = 'clang++'
+env['CXX'] = \"g++-4.7\"
 
 conf = env.Configure(config_h = \"config.h\")
 conf.Define(\"__STDC_FORMAT_MACROS\")" | patch -p1 || exit $SHELLPACK_ERROR

	scons
	if [ $? -ne 0 ]; then
		echo
		echo Trying an alternative compiler: gcc48
		install-depends gcc48-c++
		sed -i -e 's/g++-4.7/g++-4.8/' SConstruct
		scons
		if [ $? -ne 0 ]; then
			die Failed to build mutilate
		fi
	fi
fi

mkdir -p ../mutilate-${VERSION}-installed/bin
cp mutilate ../mutilate-${VERSION}-installed/bin

echo mutilate installed successfully
exit $SHELLPACK_SUCCESS

==== BEGIN gtod.patch ====
diff --git a/mutilate.cc b/mutilate.cc
index cacf4db..af7f734 100644
--- a/mutilate.cc
+++ b/mutilate.cc
@@ -942,7 +942,7 @@ void do_mutilate(const vector<string>& servers, options_t& options,
     //    now = get_time();
     //#else
     struct timeval now_tv;
-    event_base_gettimeofday_cached(base, &now_tv);
+    gettimeofday(&now_tv, NULL);
     now = tv_to_double(&now_tv);
     //#endif
==== END gtod.patch ====

==== BEGIN scons.patch ====
diff --git a/SConstruct b/SConstruct
index 57d00548cbed..05d3aa09d590 100644
--- a/SConstruct
+++ b/SConstruct
@@ -16,19 +16,19 @@ env.Append(CCFLAGS = '-std=c++11 -D_GNU_SOURCE')
 conf = env.Configure(config_h = "config.h")
 conf.Define("__STDC_FORMAT_MACROS")
 if not conf.CheckCXX():
-    print "A compiler with C++11 support is required."
+    print("A compiler with C++11 support is required.")
     Exit(1)
-print "Checking for gengetopt...",
+print("Checking for gengetopt..."),
 if env.Execute("@which gengetopt &> /dev/null"):
-    print "not found (required)"
+    print("not found (required)")
     Exit(1)
-else: print "found"
+else: print("found")
 if not conf.CheckLibWithHeader("event", "event2/event.h", "C++"):
-    print "libevent required"
+    print("libevent required")
     Exit(1)
 conf.CheckDeclaration("EVENT_BASE_FLAG_PRECISE_TIMER", '#include <event2/event.h>', "C++")
 if not conf.CheckLibWithHeader("pthread", "pthread.h", "C++"):
-    print "pthread required"
+    print("pthread required")
     Exit(1)
 conf.CheckLib("rt", "clock_gettime", language="C++")
 conf.CheckLibWithHeader("zmq", "zmq.hpp", "C++")
==== END scons.patch ====
#### Description mutilate
#### Details mutilate 33
