#!/bin/sh
# $Header: K77netbackup.sh,v 1.13 2002/08/28 13:42:44 djs Stab $
#bcpyrght
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2002 VERITAS Software Corporation, All Rights Reserved $ *
#***************************************************************************
#ecpyrght
#
# This is a sample script that can be copied to the correct system shutdown
# directory (e.g./etc/rc0.d) to automatically stop the NetBackup request
# daemon at system shutdown.
# It also stops the Media Manager device daemon (ltid).

STOP_MYSQL=/usr/openv/netbackup/bin/admincmd/nbdbstop
BPPS=/usr/openv/netbackup/bin/bpps
VISD=/usr/openv/netbackup/bin/visd

term_with_kill ()
{
    killstr=$1
    signal=$2
    processes=`${BPPS} | awk '! /(\/|\[)nbdbd(mon)?($|[ 	]|\])/' | \
    grep "$killstr" | awk '{print $2}'`
    for process in $processes
    do
        if [ Y"$signal" != Y ] ; then
            kill -${signal} $process > /dev/null 2>/dev/null
        else
            kill $process > /dev/null 2>/dev/null
        fi
    done
}

stop_gdm_daemon ()
{
    ${VISD} -terminate > /dev/null 2>/dev/null
    if [ $? != 0 ]; then
        term_with_kill visd KILL
    fi
}

# This is kludgey but we need a tab to use in regular expressions
# and we want it to be obvious and not get broken accidentally.
TAB=`echo ' ' | tr ' ' '\011'`
SPACE=' '
# Determine the system's hostname.  Use hostname if available and strip
# away any domain information.
if type hostname | grep 'not found' >/dev/null
then
	host=`uname -n | sed 's/\..*$//'`
else
	host=`hostname | sed 's/\..*$//'`
fi
if [ -f /usr/openv/netbackup/bp.conf ] ; then
	master_found=`cat /usr/openv/netbackup/bp.conf | egrep -v "^#" | head -1 | egrep "^[${SPACE}${TAB}]*SERVER[${SPACE}${TAB}]*=[${SPACE}${TAB}]*${host}"`
	if [ X"${master_found}" != X ] ; then
		# There is a bp.conf, and "SERVER = host" is the first active line.
		echo "stopping the NetBackup request daemon" >/dev/console
		/usr/openv/netbackup/bin/admincmd/bprdreq -terminate 2> /dev/null

		echo "stopping the NetBackup database daemon" >/dev/console
		/usr/openv/netbackup/bin/bpdbm -terminate 2> /dev/null
	fi
fi

echo "stopping the Media Manager device daemon" >/dev/console
/usr/openv/volmgr/bin/stopltid 2> /dev/null

echo "stopping the Media Manager volume daemon" >/dev/console
/usr/openv/volmgr/bin/vmctrldbm -t 2> /dev/null

visd=`${BPPS} | grep visd | grep -v grep | awk '{print $2}'`
if [ "${visd}" != "" ]; then
    echo "stopping Global Data Manager daemon"
    stop_gdm_daemon
fi

nbdbd=`${BPPS} | grep nbdbd | grep -v grep | awk '{print $2}'`
if  [ "${nbdbd}" != "" ]; then
     echo "stopping nbdbd"
     ${STOP_MYSQL} 2> /dev/null
     if [ $? != 0 ]; then
         echo "unable to stop nbdbd"
     fi
fi
