#!/bin/bash
# This script runs the git source unit tests

P=gitsource-bench
DEFAULT_VERSION=v2.15.1
. $SHELLPACK_INCLUDE/common.sh
TIME_CMD=`which time`
if [ "$TIME_CMD" = "" ]; then
        TIMEFORMAT="%2Uuser %2Ssystem %Relapsed %P%%CPU"
        TIME_CMD="time"
fi

# 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
		;;
	--iterations)
		GITSOURCE_ITERATIONS=$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

if [ "$INSTALL_FORCE" = "yes" ]; then
	rm -rf $SHELLPACK_SOURCES/gitsource-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/gitsource-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-gitsource -v ${VERSION}  || die gitsource install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/gitsource-${VERSION}-installed || die Failed to cd to gitsource install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo gitsource installed only as requested.
	exit $SHELLPACK_SUCCESS
fi
# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

cp -ar $SHELLPACK_SOURCES/gitsource-${VERSION}-installed $SHELLPACK_DATA || die Failed to copy gitsource-$VERSION-installed

cd $SHELLPACK_DATA/gitsource-${VERSION}-installed || die Failed to cd to gitsource-$VERSION-installed

echo Warming run
make test > $LOGDIR_RESULTS/warmup.log 2>&1
tail $LOGDIR_RESULTS/warmup.log

monitor_pre_hook $LOGDIR_RESULTS 1
for ITERATION in `seq 1 $GITSOURCE_ITERATIONS`; do
	mmtests_activity iteration $ITERATION
	echo Starting iteration $ITERATION/$GITSOURCE_ITERATIONS
	$TIME_CMD -o $LOGDIR_RESULTS/gitsource-${ITERATION}.time \
		make test \
		> $LOGDIR_RESULTS/gitsource-${ITERATION}.log 2>&1
	tail $LOGDIR_RESULTS/gitsource-${ITERATION}.log
	cat $LOGDIR_RESULTS/gitsource-${ITERATION}.time
sync
done
monitor_post_hook $LOGDIR_RESULTS 1

cat $LOGDIR_RESULTS/gitsource-*.time | grep elapsed | tee $LOGDIR_RESULTS/time
rm -rf $SHELLPACK_DATA/gitsource-${VERSION}-installed

exit $SHELLPACK_SUCCESS
#### Description git source unit tests
#### Details gitsource-bench 12
