#!/bin/sh
#
# SCRIPTNAME - Start/Stop the GMS daemons
#
# Copyright (c) 1999 by Gordano Ltd.
# All Rights Reserved.
#

#
# The network and streams should already be built by the time
# this script runs.
#
# If the GMS daemons are already active, don't re-execute them.
#

PATH=/usr/bin:/bin
LOCN=LOCATION/bin
SCRIPT=SCRIPTNAME
TARGET=TARGETNAME

case $1 in 
'start')
    for i in SERVICES
    do
    if [ -f $LOCN/$i ]
        then
            echo "starting $i"
            $LOCN/$i 
            sleep 2
    fi  
    done
    ;;

'stop')
    echo "stopping $TARGET"
    echo "{ if ( ( index(\$3,temp)!=0 ) && \$1=="1" ) print \$2 }" > progfile
    for i in SERVICES
    do
        pid=`/bin/ps -eo ppid,pid,comm | /usr/bin/awk -f progfile temp=$i `
        if test "$pid"
        then
            /bin/kill $pid
        fi
    done
    sleep 6
    for i in SERVICES
    do
        j="0"
        while [ $j != "5" ]
        do
            if [ $j -lt 1 ]
            then
                echo "stopping $i"
            fi
            pid=`/bin/ps -eo ppid,pid,comm | /usr/bin/awk -f progfile temp=$i `
            if test "$pid"
            then
                if [ "$j" = "1" ] || [ "$j" = "3" ]
                then
                    /bin/kill -s SIGUSR1 $pid
                else
                    /bin/kill $pid
                fi
                sleep 2
                j=`expr $j + 1`
            else
                echo "stopped $i"
                j="5"
            fi
        done
    done
    rm progfile
    ;;
*)
    echo "usage: $SCRIPT {start|stop}"
    ;;
esac
