#!/bin/bash
P=sparsetruncate-bench
DEFAULT_VERSION=0
. $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
		;;
	--nr-directories)
		SPARSETRUNCATE_DIRECTORIES=$2
		shift 2
		;;
	--nr-files)
		SPARSETRUNCATE_FILES=$2
		shift 2
		;;
	--filesize)
		SPARSETRUNCATE_FILESIZE=$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/sparsetruncate-${VERSION}
fi
if [ ! -d $SHELLPACK_SOURCES/sparsetruncate-${VERSION}-installed ]; then
	mmtests_activity source-install
	$SHELLPACK_INCLUDE/shellpack-install-sparsetruncate -v ${VERSION}  || die sparsetruncate install script returned error
	mmtests_activity source-installed
fi
cd $SHELLPACK_SOURCES/sparsetruncate-${VERSION}-installed || die Failed to cd to sparsetruncate install directory
cd $SHELLPACK_SOURCES/sparsetruncate-${VERSION}-installed ||
	die Failed to change to benchmark directory

# Include monitor hooks
. $SHELLPACK_INCLUDE/include-monitor.sh

if [ "$INSTALL_ONLY" = "yes" ]; then
echo Initialising files
# <dir> <dirs> <files> <fsize> <initonly>
rm -rf $SHELLPACK_DATA/sparsetruncate
mkdir -p $SHELLPACK_DATA/sparsetruncate
./sparsetruncate $SHELLPACK_DATA/sparsetruncate $SPARSETRUNCATE_DIRECTORIES $SPARSETRUNCATE_FILES $SPARSETRUNCATE_FILESIZE 1
if [ $? -ne 0 ]; then
	die Failed to initialise $SHELLPACK_DATA
fi
	echo sparsetruncate installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

echo Truncating
monitor_pre_hook $LOGDIR_RESULTS 0
$TIME_CMD -o $LOGDIR_RESULTS/time											\
	./sparsetruncate $SHELLPACK_DATA/sparsetruncate $SPARSETRUNCATE_DIRECTORIES $SPARSETRUNCATE_FILES $SPARSETRUNCATE_FILESIZE 0	\
		> $LOGDIR_RESULTS/truncate.log 2>&1
monitor_post_hook $LOGDIR_RESULTS 0

exit $SHELLPACK_SUCCESS
#### Description sparsetruncate
#### Details sparsetruncate-bench 11
