#! /bin/sh

# This script assumes that all hosts that run the node managers and system
# managers share a common filesystem. If this is not the case, run PRM_setup
# for each separately.

# Edit next line. Path name of the top of the PRM directory hierarchy. 
PRM_TOP=/nfs/pfs/divirs/jmsm/new

# Next line contains the name of the file in which node names are specified.
# Node names are internet-style host names. All host names (including sysmngr
# host) must be specified on a single line, separated by spaces.
# Assigning a value to NODESFILE here is optional.
NODESFILE=


BINDIR=$PRM_TOP/bin

SMHOST=
NMHOSTLIST=
NMOPTS=
SMOPTS=

# ping used to find out whether a host is on the network.
PING=/usr/etc/ping

#rsh to start managers on remote node.
RSH=/usr/ucb/rsh

# Directories in which nodemngr and sysmngr binaries reside. At execution time
# the $ARCH gets appended to this.
NMGRDIR=$BINDIR    
SMGRDIR=$BINDIR    

COUNT=`expr 0`

usage="Usage: $0 [-f hostsfile ] [ -n \"nodemngr-opts\" ] [ -s \"sysmngr-opts\" ]"

#set -x

while [ $# -gt 0 ]; do

	case "$1" in
	-s)
		SMOPTS=$2
		shift ; shift
		;;
	-S)
		SMHOST=$2
		shift ; shift
		;;
	-n)     
		NMOPTS=$2
		shift ; shift 
		;;
	-f)     
		NODESFILE=$2
		shift ; shift 
		;;
	-*)
		echo "$usage" 1>&2 
		exit 1
		;;
	*)
		break
		;;
	esac
done

if [ $# -gt 0 ]; then
	echo "$usage" 1>&2 
	exit 1
fi

rm -f /tmp/nm1
touch /tmp/nm1

if [ $ARCH = hp700 ] ; then
   RSH=/usr/bin/remsh
fi

[ $CWD ] || CWD=`pwd`


if [ $NODESFILE ] ; then

	[ $SMHOST ] || SMHOST=`awk  '/-s [ ]*[.A-Za-z]*/ \
	{for (i = 1; i <= NF; i++) if ($i == "-s") print $(i+1) }' $NODESFILE`
	if [ $SMHOST ] ; then
		NMHOSTLIST=`sed "s/-s [ ]*[.A-Za-z]*//g" $NODESFILE`
	else
		read NMHOSTLIST < $NODESFILE
		$SMHOST=zephyr.isi.edu
	fi
else
	echo -n "Enter name of sysmngr host: "
	read SMHOST
	echo "Enter nodemngr hostnames, one per line. Ctl-D ends list."
	x=dummy
	while [ $x ] ; do
		read x
		NMHOSTLIST="$NMHOSTLIST $x"
	done
	echo ""
	echo -n "Starting managers..."
fi


# start nodemngrs on every NMHOST

for  host in $NMHOSTLIST ; do
	{ { [ $ARCH = hp700 ] || $PING $host 5 > /dev/null 2>&1 ; } && \
	  $RSH $host "exec /bin/sh -cx \"echo $host nodemngr \$\$ \\\\ >> $CWD/PRM_config.$$ ;  cd $NMGRDIR/\`arch\` ; exec  \
	  nodemngr $NMOPTS \" < /dev/null >& /tmp/nodemngr_err.$$ " ; }
	if [ $? ] ; then
		echo "$host `$RSH $host arch`" >> /tmp/nm1
                COUNT=`expr $COUNT + 1`
	else
		echo "Could not start nodemngr on $host"
	fi

#	read host NMOPTS  < $NODESFILE
done

mv -f /tmp/nm1 $SMGRDIR/`arch`/nmfile-${COUNT}.setup
rm -f /tmp/nm1


# start system manager

for host in $SMHOST ; do
	{ { [ $ARCH = hp700 ] ||  $PING $host 5 > /dev/null 2>&1 ; } && \
	  $RSH $host "exec /bin/sh -cx \"echo $host sysmngr \$\$ \\\\ >> $CWD/PRM_config.$$ ;  cd $SMGRDIR/\`arch\` ; exec sysmngr \
	  -f nmfile-${COUNT}.setup $SMOPTS \" < /dev/null >& \
	  /tmp/sysmngr_err.$$ " ; }
	if [ $? != 0 ] ; then
		echo "Could not reach $host to start sysmngr"
	fi
done

echo "done"
