#!/bin/sh

command=""
args=""
daemonLocation="/var/adm/windu"
boottime=""

#
# check the command line arguments
#
while [ "$1" != "" ]
do
   if [ "$1" = "-dir" ]
   then
      shift
      daemonLocation="$1"
   elif [ "$1" = "-boottime" ]
   then
      boottime="y"
   elif [ "$1" = "start" -o "$1" = "stop" ]
   then
      command="$1"
   else
      args="$args $1"
   fi
   shift
done

usedelays=""
# Only delay during startup
if [ "$boottime" = "y" -a "$command" = "start" ]
then
   usedelays="y"
fi

#
# Figure out the name of the daemon.  It's always the latest version of the
# daemon in the location specified by the user or /var/adm/windu by default.
#
currDir=`pwd`
cd $daemonLocation

latestVers=0
for x in `ls windu_serviced*`
do
   currentVers=`echo $x | /usr/bin/cut -c 15-`
   if [ `expr $latestVers` -lt `expr $currentVers` ]
   then
      latestVers="$currentVers"
   fi
done
WINDU_SERVICED="$daemonLocation/windu_serviced$latestVers"
cd $currDir

# Give registry time to start up
if [ "$usedelays" = "y" ]
then
   sleep 2
fi

# start the service daemon
/usr/bin/nohup $WINDU_SERVICED $command & >> /dev/null

# Give service daemon time to start up (and autostart services)
if [ "$usedelays" = "y" ]
then
   sleep 10
fi
