#!/bin/bash
# memcached installer
P=memcached-install
DEFAULT_VERSION=1.4.15
. $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://memcached.googlecode.com/files
MIRROR_LOCATION="$WEBROOT/memcached"

# 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
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--shutdown)
		SHUTDOWN=yes
		shift
		;;
	--mempool)
		MEMCACHED_MEMPOOL=$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 if necessary
if [ ! -e $SHELLPACK_SOURCES/memcached-${VERSION}-installed ]; then
	# Unconditionally fetch the tar to find out the real version number
	TARFILE=memcached-${VERSION}.tar.gz
	sources_fetch $WEB_LOCATION/$TARFILE $MIRROR_LOCATION/$TARFILE $SHELLPACK_SOURCES/$TARFILE

	# Building from scratch, uncompress the tar
	cd $SHELLPACK_SOURCES
	tar xf $TARFILE
	if [ $? -ne 0 ]; then
		error "$P: tar xf $TARFILE 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 memcached-${VERSION}
	pushd memcached-${VERSION} > /dev/null || die Failed to rename tar

	# Configure
	./configure --prefix=$SHELLPACK_SOURCES/memcached-${VERSION}-installed
	if [ $? -ne 0 ]; then
		error "$P: configure failed"
		popd > /dev/null
		exit $SHELLPACK_ERROR
	fi

	# Build
	make
	if [ $? -ne 0 ]; then
		error "$P: make failed"
		echo Attempting workaround specific to openSUSE gcc bug
		echo "diff --git a/memcached.c b/memcached.c
--- a/memcached.c
+++ b/memcached.c
@@ -2498,15 +2498,20 @@ void append_stat(const char *name, ADD_STAT add_stats, conn *c,
 inline static void process_stats_detail(conn *c, const char *command) {
     assert(c != NULL);
 
-    if (strcmp(command, \"on\") == 0) {
+    /* Workaround for stupid openSUSE gcc bug */
+    char on[] = \"on\";
+    char off[] = \"off\";
+    char dump[] = \"dump\";
+
+    if (strcmp(command, on) == 0) {
         settings.detail_enabled = 1;
         out_string(c, \"OK\");
     }
-    else if (strcmp(command, \"off\") == 0) {
+    else if (strcmp(command, off) == 0) {
         settings.detail_enabled = 0;
         out_string(c, \"OK\");
     }
-    else if (strcmp(command, \"dump\") == 0) {
+    else if (strcmp(command, dump) == 0) {
         int len;
         char *stats = stats_prefix_dump(&len);
         write_and_free(c, stats, len);
" | patch -p1 || die Failed to patch workaround
		make
		if [ $? -ne 0 ]; then
			die make failed
		fi
	fi

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

	if [ "$INSTALL_ONLY" = "yes" ]; then
		exit 0
	fi
fi

# Shutdown existing instances
if [ -e $SHELLPACK_SOURCES/memcached-${VERSION}-installed/memcached.pid ]; then
	MEMCACHED_PID=`cat $SHELLPACK_SOURCES/memcached-${VERSION}-installed/memcached.pid`
	shutdown_pid memcached $MEMCACHED_PID
	rm $SHELLPACK_SOURCES/memcached-${VERSION}-installed/memcached.pid
fi
if [ "$SHUTDOWN" = "yes" ]; then
	exit 0
fi

if [ "$MEMCACHED_MEMPOOL" = "" ]; then
	die "MEMCACHED_MEMPOOL environment variable or --mempool switch not set"
fi

# memcached refuses to run as root by default but security is not a priority
# and the expectation is that mmtests will often be running as root
USER_SWITCH=
if [ `whoami` = "root" ]; then
	USER_SWITCH="-u root"
fi

# Start new instance
MEMCACHED_MEMPOOL_MB=$((MEMCACHED_MEMPOOL/1048576))
echo Starting memcached server with pool $MEMCACHED_MEMPOOL_MB MB
$SHELLPACK_SOURCES/memcached-${VERSION}-installed/bin/memcached $USER_SWITCH \
	-d -P $SHELLPACK_SOURCES/memcached-${VERSION}-installed/memcached.pid \
	-m $MEMCACHED_MEMPOOL_MB
	
if [ $? -ne 0 ]; then
	die Failed to start memcached server
fi

# Give the server a few seconds to get started. Ideally, we would connect
# and make sure it's alive but it's overkill at this point
sleep 10
	
echo memcached installed successfully
#### Description memcached installer
#### Details memcached 25
