#!/bin/bash
# This script whatevers vmscale
###SHELLPACK preamble vmscale-bench 1

###SHELLPACK parseargBegin
###SHELLPACK parseargInstall
###SHELLPACK parseargParam   --cases	VMSCALE_CASES
###SHELLPACK parseargYes     --use-perf VMSCALE_USE_PERF
###SHELLPACK parseargEnd

install-depends coreutils
###SHELLPACK check_install_required vmscale-${VERSION}
if [ "$VMSCALE_USE_PERF" = "yes" ]; then
	###SHELLPACK check_external_install_required_continue perfbuild perfbuild-4.1 4.1
	export PATH=$SHELLPACK_SOURCES/perfbuild-4.1-installed/bin:$PATH
fi

###SHELLPACK monitor_hooks

cd $SHELLPACK_SOURCES/vmscale-${VERSION}-installed || die Failed to cd to vmscale directory
VMSCALE_CASES=`echo $VMSCALE_CASES | sed -e 's/,/ /g'`

echo Unlimiting virtual memory usage
ulimit -v unlimited

echo -n > $LOGDIR_RESULTS/cases
for CASE in $VMSCALE_CASES; do
	mmtests_activity $CASE

	# Prepare for compatability with how vmscale works without using the
	# run script as it does more setup than mmtests needs to deal with
	rm -rf /tmp/vm-scalability
	rm -rf $SHELLPACK_TEMP/*
	ln -s $SHELLPACK_TEMP /tmp/vm-scalability

	echo $CASE | tee -a $LOGDIR_RESULTS/cases
	monitor_pre_hook  $LOGDIR_RESULTS $CASE
	if [ "$VMSCALE_USE_PERF" = "yes" ]; then
		$TIME_CMD "./run case-$CASE" 2>$LOGDIR_RESULTS/$CASE.time | tee $LOGDIR_RESULTS/${CASE}.log
		mv `hostname`-* $LOGDIR_RESULTS/$CASE-raw
	else
		$TIME_CMD "./case-$CASE" 2>$LOGDIR_RESULTS/$CASE.time | tee $LOGDIR_RESULTS/${CASE}.log
	fi
	monitor_post_hook $LOGDIR_RESULTS $CASE
done

exit $SHELLPACK_SUCCESS
