#!/bin/sh

# $Id: isisd,v 3.4.2.2 2002/08/22 10:58:09 rajeshp Exp $  - %Q%
#ident "%Z%isis:$RCSfile: isisd,v $ $Revision: 3.4.2.2 $"

# Copyright(C)2002 VERITAS Software Corporation.  ALL RIGHTS RESERVED.
# UNPUBLISHED -- RIGHTS RESERVED UNDER THE COPYRIGHT
# LAWS OF THE UNITED STATES.  USE OF A COPYRIGHT NOTICE
# IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
# OR DISCLOSURE.
#
# THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND
# TRADE SECRETS OF VERITAS SOFTWARE.  USE, DISCLOSURE,
# OR REPRODUCTION IS PROHIBITED WITHOUT THE PRIOR
# EXPRESS WRITTEN PERMISSION OF VERITAS SOFTWARE.
#
#               RESTRICTED RIGHTS LEGEND
# USE, DUPLICATION, OR DISCLOSURE BY THE GOVERNMENT IS
# SUBJECT TO RESTRICTIONS AS SET FORTH IN SUBPARAGRAPH
# (C) (1) (ii) OF THE RIGHTS IN TECHNICAL DATA AND
# COMPUTER SOFTWARE CLAUSE AT DFARS 252.227-7013.
#               VERITAS SOFTWARE
# 1600 PLYMOUTH STREET, MOUNTAIN VIEW, CA 94043

BASEDIR=/opt/VRTSob

if pkginfo -q VRTSob
then
  BASEDIR=`pkgparam VRTSob BASEDIR`
elif pkginfo -q VRTSobdev
then
   BASEDIR=`pkgparam VRTSobdev BASEDIR`
fi


LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$BASEDIR/lib
export LD_LIBRARY_PATH
VXSVC_LOCK_FILE=/var/vx/isis/vxisis.lock


RUNSTATE=`echo " RUNNING"`
STARTPENDING=`echo " START PENDING"`
STOPPENDING=`echo " STOP PENDING"`
# Stop Isis server
VXSVC="$BASEDIR/bin/vxsvc"
KILLCMD="$VXSVC -k"
STATUSCMD="$VXSVC -m"

FORCESHUTDOWN="VEA Server could not be stopped after 1 minute. VEA Server will be stopped forcefully"

INSTANCERUNNING="An instance of vxsvc is running. Please stop it before starting another instance."

SHUTDOWNMESSAGE=" VEA server is shutting down ."

loopwait()
{
    CURRENT_RUNSTATE=`$VXSVC -m|awk -F: '{ print $2 }'`
    while [ "$STARTPENDING" = "$CURRENT_RUNSTATE" ]
    do
        sleep 1
        CURRENT_RUNSTATE=`$VXSVC -m|awk -F: '{ print $2 }'`
    done
    while [ "$STOPPENDING" = "$CURRENT_RUNSTATE" ]
    do
        sleep 1
        CURRENT_RUNSTATE=`$VXSVC -m|awk -F: '{ print $2 }'`
    done
}

stopService()
{
   
   loopwait
   if [ "$RUNSTATE" = "$CURRENT_RUNSTATE" ]
   then

      # stop the server in subshell
        echo "$SHUTDOWNMESSAGE "

        ($KILLCMD >/dev/null 2>&1 </dev/null &)
        PID=$!
        echo $STATUSCMD | nawk '{
            for (i=0;i<30;i++)
            {
                $0 | getline output
                close($0)
                if (output ~ /NOT RUNNING/)
                {
                    break
                }
               else
                {
                    system("sleep 2");
                    printf ".";
                }
            }
            if (i == 30)
            {
                printf "\n";
            }
        }'

        # now kill vxsvc with a kill -9
        CURRENT_RUNSTATE=`$STATUSCMD|awk -F: '{ print $2 }'`
        if [ "$STOPPENDING" = "$CURRENT_RUNSTATE" ]
        then
            echo "$FORCESHUTDOWN"
            kill -9 $PID 2>/dev/null
            PID=`eval ps -aef | awk '/'vxsvc'/ && !/awk/ {print $2}'`
            kill -9 $PID 2>/dev/null
            rm -f /var/vx/isis/vxisis_shutdown.lock 2>/dev/null
        fi
    fi
}

startService()
{
	loopwait
        if [ -f $VXSVC_LOCK_FILE ]
        then
            PID=`cat $VXSVC_LOCK_FILE`
            if [ "X$PID" = X ] 
            then
                rm -f $VXSVC_LOCK_FILE  2>/dev/null
                $VXSVC -r $BASEDIR/config/Registry
            else
                PROC=`ps -aef | grep vxsvc | grep -v grep`
                if [ "X$PROC" = X ]
                then
                    $VXSVC -r $BASEDIR/config/Registry
                else
                    echo "$INSTANCERUNNING"
                fi
            fi
        else
            $VXSVC -r $BASEDIR/config/Registry
        fi
}

case "$1" in
    start)
        startService
        ;;
    stop)
        stopService
        ;;
    status)
        $VXSVC -m
        ;;
    version)
        $VXSVC -v
        ;;
    restart)
        stopService
        startService
        ;;
    *)
        echo "Usage: /etc/init.d/isisd { start | stop | status | version | restart }"
        ;;
esac
exit 0


