#! /bin/bash
#
#   Wait for the specified process to show up on the current node.
#   Only processes run by the current user are examined.
#   The syntax is:
#
#      waitProc <prog> <time> [<service>]
#
#   where:
#      <prog>    is the program name
#      <time>    is the amount of time to wait for the process
#      <service> is the name register in the Name server.
#
# set verify
narg=$#
if [ $narg -lt 2 ]; then
    echo "Wait for a process or named service. Syntax:"
    echo "  waitProc <prog> <time> [<service>]"
    exit 3
fi
#
wtime=$2
WHOAMI=`which whoami`
uselect=""
if [ -n "$USER" ]; then
    uselect="-u$USER"
elif [ -n "$WHOAMI" -a -x $WHOAMI ]; then
    uselect="-u`$WHOAMI`"
fi
#
[ `ps $uselect | grep -c "$1"` = 0 ] && sleep $wtime
[ `ps $uselect | grep -c "$1"` = 0 ] && exit 1
if [ $narg -ge 3 ]; then
    [ `NameCtrl index | grep -c "$3"` = 0 ] && exit 2
fi
exit 0
