#!/bin/sh
#pragma ident	"$Id: autotest,v 1.1 2005/10/25 22:38:34 jmoyer Exp $"
#
# autotest [-a|-b|-e|-p|-n|-v] [-s|-c] [-f servername] [-m mntpoint] servername
#
# arguments:
#	-a|-b|-e|-p|-n|-v	test selectors, passed to runtests.
#	-c	run tests with current environment, do not perform any setup.
#	-s	setup the automount test environment, do not run any tests.	
#	-f servername	server to failover to.
#	-m mntpoint	path to automount on locally
#	servername	server to automount from
#
# Default is to perform a complete setup of the environment and to run tests.
Program=`basename $0`

InitFile="../src/tests.init"
USAGE="usage:  $Program [-a|-b|-e|-p|-n|-v] [-s|-c] [-f server_name] [-m mntpoint] server_name"

# defaults
. $InitFile
export PATH CFLAGS LIBS MOUNT UMOUNT

set - `getopt abcef:m:npsv $*`

if [ $? != 0 ]; then
	echo $USAGE
	exit 1
fi
for c in $*
do
	case $c in
		-a|-b|-e|-p|-n|-v)	TESTS=$c; shift	;;
		-s)		JUSTMAPS=y; shift ;;
		-c)		SETUP=n; shift ;;
		-m)		CLI_AUTO_CLIENT_MNTPNT=$2; shift; shift	;;
		-f)		SERVER2=$2; shift; shift	;;
		--)		shift; break		;;
	esac
done

if [ $# -gt 0 ]; then
	SERVER1=$1
	AUTO_CLIENT_MNTPNT=/auto_test/${SERVER1}
	shift
	if [ $# -gt 0 ]; then
		echo $USAGE
		exit 1
	fi
fi

if [ "x$SERVER1" = "x" ]; then
	echo $USAGE
	exit 1
else
	if [ "x$SERVER2" = "x" ]; then
		SERVER2=$SERVER1
	fi
fi
	
if [ "x$CLI_AUTO_CLIENT_MNTPNT" != "x" ]; then
	AUTO_CLIENT_MNTPNT=${CLI_AUTO_CLIENT_MNTPNT}
fi

cd ../bin
if [ "$JUSTMAPS"es = "yes" ]; then
	echo "Setting up environment for automount testing."
	sh ./setup -c -s $SERVER1 -f $SERVER2 -m $AUTO_CLIENT_MNTPNT
	if [ $? -ne 0 ]; then
		echo "Exiting"
		exit 1
	else
		echo "Update auto_master and run automounter before testing."
		exit 0
	fi
fi

if [ "$SETUP"o = "no" ]; then
	echo "Proceeding with automount testing using current environment."
else
	echo "Setting up environment for automount testing."
	sh ./setup -a -s $SERVER1 -f $SERVER2 -m $AUTO_CLIENT_MNTPNT
	if [ $? -ne 0 ]
	then
		echo "Exiting"
		exit 1
 	fi
fi

echo "Running automount tests."
echo "sh ./runtests $TESTS"
export VERBOSE SERVER1 AUTO_CLIENT_MNTPNT AUTOMAP_DIR
sh ./runtests $TESTS

exit 0
