#!/bin/bash
# wikibench installer

P=wikibench
DEFAULT_VERSION=0.3.1
. $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://www.wikibench.eu/wp-content/uploads/2011/01
MIRROR_LOCATION=$WEBROOT/wikibench
MEDIAWIKI_VERSION=1.18.6

# 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

install-depends ant

. $SHELLPACK_INCLUDE/common.sh

if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/mediawikibuild-${MEDIAWIKI_VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/mediawikibuild-${MEDIAWIKI_VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-mediawikibuild -v ${MEDIAWIKI_VERSION}  || die mediawikibuild install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/mediawikibuild-${MEDIAWIKI_VERSION}-installed || die Failed to cd to mediawikibuild install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo mediawikibuild installed only as requested.
	exit $SHELLPACK_SUCCESS
fi
# Unconditionally fetch the tar to find out the real version number
TARFILE=wikibench-${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 wikibench-${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 wikibench-${VERSION}-installed
pushd wikibench-${VERSION}-installed > /dev/null || die Failed to rename tar

echo wikibench installed successfully
#### Description wikibench
#### Details wikibench 23
