#!/bin/bash
P=cmattach-test-bench
DEFAULT_VERSION=20110718
. $SHELLPACK_INCLUDE/common.sh

# 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
		;;
	*)
		echo Unrecognised option: $1
		shift
	esac
done
if [ -z "$VERSION" ]; then
	VERSION=$DEFAULT_VERSION
fi

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

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

for TEST in process_vm_readv_simple process_vm_writev process_vm_readv_iovec; do

	echo -n "$TEST: "

	STATUS="OK"
	./setup_$TEST > $SHELLPACK_TEMP/src.out 2>&1 || STATUS="SETUP_FAILED"
	sleep 1

	if [ "$STATUS" != "OK" ]; then
		echo $STATUS
		cat $SHELLPACK_TEMP/src.out
		exit $SHELLPACK_ERROR
	fi

	COMMAND=`tail -1 $SHELLPACK_TEMP/src.out`
	$COMMAND > $SHELLPACK_TEMP/target.out 2>&1 || STATUS="TARGET_FAILED"
	SRCPID=`echo $COMMAND | awk '{print $2}'`

	if [ "$STATUS" != "OK" ]; then
		kill -9 $SRCPID
		echo $STATUS
		echo SETUP
		cat $SHELLPACK_TEMP/src.out
		echo
		echo TARGET
		cat $SHELLPACK_TEMP/target.out
		echo
		echo COMMAND: $COMMAND
		exit $SHELLPACK_ERROR
	fi

	kill -9 $SRCPID
	echo $STATUS
done
#### Description This is a basic functional test of cross-memory-attach
#### Details cmattach-test-bench 2
