#!/bin/bash
# This script installs reaim and runs the regression tests
P=reaim-bench
DEFAULT_VERSION=7.0.1.13
. $SHELLPACK_INCLUDE/common.sh

REAIM_FILESIZE=10k
REAIM_POOLSIZE=1m
REAIM_STARTUSERS=1
REAIM_ENDUSERS=$((NUM_CPU*4))
REAIM_INCREMENT=$((NUM_CPU/4))
REAIM_ITERATIONS=5
REAIM_WORKFILE=workfile.fserver

# Basic argument parser
while [ "$1" != "" ]; do
	case "$1" in
	-v)
		VERSION=$2
		shift 2
		;;
	--install-only)
		INSTALL_ONLY=yes
		shift
		;;
	--install-force)
		INSTALL_FORCE=yes
		shift
		;;
	--filesize)
		REAIM_FILESIZE=$2
		shift 2
		;;
	--poolsize)
		REAIM_POOLSIZE=$2
		shift 2
		;;
	--startusers)
		REAIM_STARTUSERS=$2
		shift 2
		;;
	--endusers)
		REAIM_ENDUSERS=$2
		shift 2
		;;
	--increment)
		REAIM_INCREMENT=$2
		shift 2
		;;
	--iterations)
		REAIM_ITERATIONS=$2
		shift 2
		;;
	--workfile)
		REAIM_WORKFILE=$2
		shift 2
		;;
	--exclude)
		REAIM_EXCLUDE=$2
		shift 2
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

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

echo FILESIZE $REAIM_FILESIZE > reaim.config
echo POOLSIZE $REAIM_POOLSIZE >> reaim.config
echo DISKDIR $TESTDISK_DIR  >> reaim.config

# Remove any tests that were requested to be excluded
cp data/$REAIM_WORKFILE data/$REAIM_WORKFILE.mmtests
for EXCLUDE in $REAIM_EXCLUDE; do
	grep -v " $EXCLUDE$" data/$REAIM_WORKFILE.mmtests > data/$REAIM_WORKFILE.mmtests.tmp
	mv data/$REAIM_WORKFILE.mmtests.tmp data/$REAIM_WORKFILE.mmtests
done

monitor_pre_hook $LOGDIR_RESULTS
for ITERATION in `seq 1 $REAIM_ITERATIONS`; do
	save_rc ./src/reaim \
		-s$REAIM_STARTUSERS \
		-e$REAIM_ENDUSERS \
		-t \
		-i$REAIM_INCREMENT \
		-fdata/$REAIM_WORKFILE.mmtests \
		-r1 \
		-c./reaim.config \
		-l$LOGDIR_RESULTS/reaim.$ITERATION \
			2>&1 | grep -v NSIG | tee $LOGDIR_RESULTS/reaim.$ITERATION.raw

	recover_rc
	if [ $? -ne 0 ]; then
		die Failed to run reaim
	fi
done
monitor_post_hook $LOGDIR_RESULTS

exit $SHELLPACK_SUCCESS
#### Description reaim
#### Details reaim-bench 32
