#! /bin/sh 
#$Header: /cvs/CVS/utopia/lsf/lstools/p4job,v 5.1 1995/05/12 20:23:57 jwang Exp $Exp

#  This sample script is a wrapper for running p4 jobs under lsbatch.
#
#  Usage: p4job [-l] [-s "slave"] master
#	-l :: run a slave on the local host (the same host as the master)
#		The default is to run the master on the first host, and
#		k - 1 slaves on the other hosts
#	-s "slave" :: process to run on slave hosts.
#		If -s slave is not specified, the master process will be run
#		on all hosts
#	master :: process to run on master host
#
#  Submit job by saying "bsub [otheroptions] -n k p4job <arguments>"
#  where k is the number of hosts to use. "p4job"
#  is the name of this script.
#
#  Note the variable LSB_HOSTS is assigned by lsbatch system when
# running the job.
#
# This script starts the p4 master process on the controlling host (the first
# host in the LSB_HOSTS list), and starts components on all the hosts in
# the list.  This means the master and a slave will be running on the same
# host.
#
#  Change this script if you want your job to behave differently.
#  The purpose of this script is to generate a host file and start the job.
#
#  This script should also work with other similar packages, such as TCGMSG.

HOSTFILE=$HOME/.lsbatch/host$$.`hostname`

# clean out any garbage hostfile
rm -f $HOSTFILE

# Process the argument list
usage='usage: p4job [-l] [-s "slave"] master'
local=
slave=

while [ x$1 != x ]
do
    case $1 in
    -l)	local=-l; shift ;;
    -s) slave=$2; shift; shift ;;
    *) break ;;
    esac
done

# Check that we have a master name
if [ "$1" = "" ]
then
    echo $usage
    exit 1
fi

master="$@"

if [ "$slave" = "" ]
then
    slave=$master
fi

#produce host file

# Put the host names into the parameter list, and discard the first host
# name (it is replaced with "local")
set $LSB_HOSTS
shift

if [ "$local" = "-l" ]
then
    echo local 1 >> $HOSTFILE
else
    echo local 0 >> $HOSTFILE
fi

for host in $@
do
    echo $host 1 $slave >> $HOSTFILE
done

# start the master
$master -p4pg $HOSTFILE

# save the exit status when it completes
exstat=$?

#clean up the hostfile
rm -f $HOSTFILE

#and exit with the status from the p4 job
exit $exstat
