#!/bin/bash
# This script installs ltp and runs the regression tests
P=ltp-bench
DEFAULT_VERSION=full-20120104
. $SHELLPACK_INCLUDE/common.sh
LTP_RUN_TESTS="controllers"

# 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
		;;
	--ltp-tests)
		LTP_RUN_TESTS=$2
		shift 2
		;;
	--ltp-args)
		LTP_RUN_ARGS=$2
		shift 2
		;;
	--ltp-iterations)
		LTP_RUN_ITERATIONS=$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/ltp-${VERSION}
fi
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}-installed || die Failed to cd to ltp install directory
if [ "$INSTALL_ONLY" = "yes" ]; then
	echo ltp installed only as requested.
	exit $SHELLPACK_SUCCESS
fi

# Run the testsuite
cd $SHELLPACK_SOURCES/ltp-${VERSION}-installed || die Failed to cd to ltp install directory
export LTPROOT=`pwd`
export PATH=$PATH:$LTPROOT/testcases/bin

if [ "$LTP_RUN_ITERATIONS" = "" ]; then
	LTP_RUN_ITERATIONS=1
fi

for TEST in $LTP_RUN_TESTS; do
	echo Executing $TEST LTP test
	RESULTS="$LOGDIR_RESULTS"
	mkdir -p $RESULTS

	for ITER in `seq 1 $LTP_RUN_ITERATIONS`; do
		if [ "$TEST" = "test-direct-process" ]; then
			# This is part of a CPU hotplug reproduction case. It hammers
			# process creation and is intended as a scheduler stress test
			( ./testcases/bin/process -b 10 -d 5 || die ltp $TEST failed ) | tee $RESULTS/log-$TEST.txt
		elif [ -e runtest/$TEST ]; then
			( ./runltp -f $TEST $LTP_RUN_ARGS 2>&1 || die ltp $TEST failed ) | tee $RESULTS/log-$TEST.txt
		elif [ -e testcases/bin/$TEST ]; then
			( ./testcases/bin/$TEST $LTP_RUN_ARGS 2>&1 || die ltp $TEST failed ) | tee $RESULTS/log-$TEST.txt
		else
			( ./runltp $TEST $LTP_RUN_ARGS 2>&1 || die ltp $TEST failed ) | tee $RESULTS/log-$TEST.txt
		fi
	done

	# Check for failures
	#grep -v PASS $RESULTS/log-$TEST.txt > $RESULTS/failed_tests.txt
	#TEST=`cat $RESULTS/failed_tests.txt`
	#if [ "$TEST" != "" ]; then
	#	echo
	#	echo Some ltp regression tests reported failure | tee -a $LOGDIR/summary
	#	cat $RESULTS/failed_tests.txt | tee -a $LOGDIR/summary
	#	echo
	#
	#	echo ltp successfully installed but some regression tests failed | tee -a $LOGDIR/summary
	#	exit $SHELLPACK_ERROR
	#fi
done

echo
exit $SHELLPACK_SUCCESS
#### Description LTP Regression Test Suite
#### Details ltp-bench 20
