#!/bin/bash
# This script installs netperf and runs the regression tests

P=ltp-pounder-bench
VERSION=full-20120104
RUNTIME=$((3600*7))
. $SHELLPACK_INCLUDE/common.sh

# Basic args parser
while [ "$1" != "" ]; do
	case "$1" in
		-v)
			VERSION=$2
			shift 2;;
		--ltp-runtime)
			RUNTIME=$2
			shift 2
			;;
		*)	echo Unrecognised option: $1; shift
	esac
done

# Install LTP if necessary
if [ ! -d $SHELLPACK_SOURCES/ltp-${VERSION}-installed ]; then
	$SHELLPACK_INCLUDE/shellpack-install-ltp -v ${VERSION} || die ltp install script returned error
fi
cd $SHELLPACK_SOURCES/ltp-${VERSION}/tools/pounder21 || die Failed to cd to ltp pounder install directory
sed -i -e 's/AUTO_SKIP=0/AUTO_SKIP=1/' Install

# Fixup LTP Pounder if necessary
if [ ! -e LTP_POUNDER_INSTALLED ]; then
	yes '' | ./Install 2>&1 | tee /tmp/pounder.$$
	if [ $? -ne 0 ]; then
		grep "ide_cdrom_copy build failed with Error" /tmp/pounder.$$
		if [ $? -ne 0 ]; then
			die Failed to install with unknown error
		fi
	fi
	touch LTP_POUNDER_INSTALLED
fi

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

monitor_pre_hook $LOGDIR/results ltp-pounder
if [ ! -e /mnt/nfs ]; then
	mkdir -p /mnt/nfs
fi
export NFS_LOGDIR=$LTP_POUNDER_NFS_LOGDIR
export NFS_LOGSERVER=$LTP_POUNDER_NFS_LOGSERVER
export NFS_LOGGING=$LTP_POUNDER_NFS_LOGGING
export DO_X_TESTS=0
mount -v -o nolock $NFS_LOGSERVER:$NFS_LOGDIR /mnt/nfs/
if [ $? -ne 0 ]; then
	echo WARNING: Failed to mount NFS directory, disabling logging
	export NFS_LOGGING=0
else
	# LTP will remount it where it likes
	umount /mnt/nfs
fi

STARTTIME=`date +%s`
ENDTIME=$(($STARTTIME+$RUNTIME))

rm -f pounder.pid
./pounder &
PID=$!
while [ `date +%s` -lt $ENDTIME ]; do
	TEST=`ps aux | grep $PID | grep -v grep`
	if [ "$TEST" = "" ]; then
		echo WARNING: Cannot find pounder process
	else
		echo Heartbeat: pounder $PID alive `date`
	fi
	sleep 300
done
echo Run time complete, killing pounder
./pounder -k
sleep 10
mv log/* $LOGDIR_RESULTS

monitor_post_hook $LOGDIR/results ltp-pounder

echo
echo LTP pounder completed successfully | tee -a $LOGDIR/summary
exit $SHELLPACK_SUCCESS
#### Description Run just the pounder test from LTP
#### Details ltp-pounder-bench 5
