#!/bin/sh

#
# COPYRIGHT    2001
# THE REGENTS OF THE UNIVERSITY OF MICHIGAN
# ALL RIGHTS RESERVED
#
# Permission is granted to use, copy, create derivative works
# and redistribute this software and such derivative works
# for any purpose, so long as the name of The University of
# Michigan is not used in any advertising or publicity
# pertaining to the use of distribution of this software
# without specific, written prior authorization.  If the
# above copyright notice or any other identification of the
# University of Michigan is included in any copy of any
# portion of this software, then the disclaimer below must
# also be included.
#
# THIS SOFTWARE IS PROVIDED AS IS, WITHOUT REPRESENTATION
# FROM THE UNIVERSITY OF MICHIGAN AS TO ITS FITNESS FOR ANY
# PURPOSE, AND WITHOUT WARRANTY BY THE UNIVERSITY O
# MICHIGAN OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING
# WITHOUT LIMITATION THE IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
# REGENTS OF THE UNIVERSITY OF MICHIGAN SHALL NOT BE LIABLE
# FOR ANY DAMAGES, INCLUDING SPECIAL, INDIRECT, INCIDENTAL, OR
# CONSEQUENTIAL DAMAGES, WITH RESPECT TO ANY CLAIM ARISING
# OUT OF OR IN CONNECTION WITH THE USE OF THE SOFTWARE, EVEN
# IF IT HAS BEEN OR IS HEREAFTER ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGES.
#

# start an apv subsystem (listener or archiver)

KBS=./kbs
WHICH=none
ERR=0
RC=0


usage() {
	echo "usage:	$0 [-l | -a]
	-l to start listener subsystem
	-a to start archiver subsystem
	(exactly one of -l or -a must be specified)"
	exit 1;
}

date >>apv.log
echo + $0 $* >>apv.log

if [ `id -u` != '0' ]; then
	echo "You must be root to run this script."
	exit 1
fi

while [ -n "$1" ]; do
	case $1 in
		-a)	if [ $WHICH = listener ]; then usage; fi;
			WHICH=archiver;;
		-l)	if [ $WHICH = archiver ]; then usage; fi;
			WHICH=listener;;
		-*|*)	usage;;
	esac
	shift
done
if [ $WHICH != listener -a $WHICH != archiver ]; then
	usage
fi

if [ $WHICH = listener ]; then

	$KBS -0 '/csh ./pilot.csh' >/dev/null
	if [ $? -eq 0 ]; then ERR=1; fi;

	$KBS -0 '/perl ./pilot.pl' >/dev/null
	if [ $? -eq 0 ]; then ERR=1; fi;

	$KBS -0 '/util/logmonitor -o /var/log/pilot.log' >/dev/null
	if [ $? -eq 0 ]; then ERR=1; fi;

	$KBS -0 '/sh ./../listen/listen.sh' >/dev/null
	if [ $? -eq 0 ]; then ERR=1; fi;

	$KBS -0 './../listen/listen -f /' >/dev/null
	if [ $? -eq 0 ]; then ERR=1; fi;

	$KBS -0 '/util/logmonitor -o /var/log/listen.log' >/dev/null
	if [ $? -eq 0 ]; then ERR=1; fi;

	if [ $ERR -gt 0 ]; then
		echo "Some listener components seem to be running already."
		echo "Stop them via apvstop first."
		RC=1
	else
		echo "Starting listener..."
		xterm -e ./pilot.csh &
		RC=0
	fi

else

	ERR=0

	$KBS -0 '/csh ./archiver.csh' >/dev/null
	if [ $? -eq 0 ]; then ERR=1; fi;

	$KBS -0 '/perl ./archiver.pl' >/dev/null
	if [ $? -eq 0 ]; then ERR=1; fi;

	$KBS -0 '/util/logmonitor -o /var/log/archiver.log' >/dev/null
	if [ $? -eq 0 ]; then ERR=1; fi;

	if [ $ERR -gt 0 ]; then
		echo "Somce archiver components seem to be running already."
		echo "Stop them via apvstop first."
		RC=1
	else
		echo "Starting archiver..."
		xterm -e ./archiver.csh --drivenum=0 &
		sleep 20
		xterm -e ./archiver.csh --drivenum=1 &
		RC=0
	fi

fi

exit $RC
