#!/bin/sh 
# $Revision: 1.11.8.1 $
#
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2004 VERITAS Software Corporation, All Rights Reserved $
#***************************************************************************
#
# netbackup.sh
#
# Start/Stop the NetBackup request daemon at system initiation/shutdown.  
# Starts/Stops the Media Manager device daemon (ltid).
# Starts/Stops the Global Data Manager daemon (visd).
# Starts/Stops the Advanced Reporter daemons.
# Stops nbdbd.
#
# Note - when the Media Manager device daemon (ltid) is started, 
# it automatically starts the Media Manager volume daemon 
# (/usr/openv/volmgr/bin/vmd) and robotic daemons as required.
#
# The following two lines are used by RedHat Linux "chkconfig" command.
# chkconfig: 235 77 77
# description: Veritas NetBackup 
#
# If installing this file on an AIX server, edit /etc/inittab to add
# the following entry so that this script is called during level 2 boot:
# veritas:2:wait:/etc/rc.veritas.aix 

NBPATH=/usr/openv/netbackup/bin
VMPS=/usr/openv/volmgr/bin/vmps
BPPS=/usr/openv/netbackup/bin/bpps 
LTID=/usr/openv/volmgr/bin/ltid 
BPRD=/usr/openv/netbackup/bin/initbprd
VISD=/usr/openv/netbackup/bin/visd
INITVISD=/usr/openv/netbackup/bin/initvisd
STOP_MYSQL=/usr/openv/netbackup/bin/admincmd/nbdbstop
MAKE_SCSI_DEV=/usr/openv/volmgr/bin/make_scsi_dev
BPCONF=/usr/openv/netbackup/bp.conf 
BPRDREQ=/usr/openv/netbackup/bin/admincmd/bprdreq 
STOPLTID=/usr/openv/volmgr/bin/stopltid 
VMCTRLDBM=/usr/openv/volmgr/bin/vmctrldbm 
MKDEV_OVPASS=/usr/openv/volmgr/bin/driver/mkdev_ovpass

FORCE_INIT_INQUIRY=/usr/openv/netbackup/db/config/FORCE_INIT_INQUIRY

SPACE=' '
BLANK=''
RETURN=0

TMPDIR=${TMPDIR:-/tmp}

PATH=$PATH:/usr/bin
export PATH

PRG=`basename $0`
TMPF=${TMPDIR}/${PRG}.$$
/bin/rm -f $TMPF

PS="/bin/ps -ea"
nbar_location="/usr/openv/nbar"
case `uname -s` in
	Darwin*) PS="/bin/ps -ax" ;;
	FreeBSD*) PS="/bin/ps -ax" ;;
	SunOS)	nbar_location="/opt/VRTSnbaro" ;;
esac

ARG="${1}"

if [ -z "${ARG}" ]; then
	ARG=start
fi

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
}

stop_acs_daemon ()
{
	process=$1
        kill $process > /dev/null 2>/dev/null
}

case "${ARG}" in
	start)

	if [ "`uname -s`" = "AIX" ] ; then
		if [ -x ${MKDEV_OVPASS} ]; then
			$MKDEV_OVPASS

			if [ -f "$FORCE_INIT_INQUIRY" ] ; then
				INQUIRY=/usr/openv/volmgr/bin/scsi_command
				TPCONFIG=/usr/openv/volmgr/bin/tpconfig
				for i in `$TPCONFIG -d | grep rmt | awk '{ print $3 }' `
				do
					echo "Forcing initial SCSI inquiry to $i" > /dev/console
					$INQUIRY -d $i > /dev/null 2>&1
				done
			fi
		else	
			echo "ERROR executing mkdev_ovpass !"
		fi
	fi

	#  Need to keep the now defunct -noinstall parameter in case
	#  NDMP has not been upgraded yet.

	if [ -f /usr/openv/volmgr/bin/ndmpmoveragent.start ] ; then
		/usr/openv/volmgr/bin/ndmpmoveragent.start -noinstall > /dev/null 2>&1
		echo "NetBackup NDMP Mover Agent started." 
	fi

	# Don't start ltid if already started by HSM startup script.
		$VMPS > $TMPF
		if [ -f "$LTID" ]
		then
			ltid=`grep ltid $TMPF`
			if test Y"$ltid" = Y
			then
				if [ -x ${MAKE_SCSI_DEV} -a "`uname -s`" = "Linux" ] ; then
				echo "Rebuilding device nodes." 
				$MAKE_SCSI_DEV
				fi
				$LTID
				echo "Media Manager daemons started." 
			fi
		else
			RETURN=1
			echo "Media Manager daemons not started." 
		fi

		/bin/rm -f $TMPF

		if [ -x "$BPRD" ]
		then
			$BPRD
			echo "NetBackup request daemon started." 
		else
			RETURN=2
			echo "NetBackup request daemon not started." 
		fi

		# Wait a little for nbdbd to come up, before starting visd

		wait=0
		$BPPS > $TMPF
		nbdbd=`grep nbdbd $TMPF`

		while [ ${wait} -lt 3 -a "${nbdbd}" = "" ]
		do
			sleep 5
			wait=`expr ${wait} + 1`
			$BPPS > $TMPF
			nbdbd=`grep nbdbd $TMPF`
		done
		/bin/rm -f $TMPF
	
		if [ -f ${nbar_location}/bin/nbar ] ; then
			${nbar_location}/bin/nbar start >/dev/null 2>&1
			echo "NetBackup Advanced Reporter daemons started."
		fi

		if [ -x "${INITVISD}" ]; then
			if [ "${nbdbd}" != "" ]; then
				${INITVISD} > /dev/null 2>/dev/null
				if [ $? != 0 ]; then
					echo "Global Data Manager daemon not started." 
					RETURN=2
				else
					echo "Global Data Manager daemon started." 
				fi
			else
				echo "Global Data Manager daemon not started." 
				RETURN=2
			fi
		fi
#
# This locking file is used by LINUX init
#
		if [ -d /var/lock/subsys ]; then
			touch /var/lock/subsys/netbackup
		fi
	;;
stop)

	IS_NETBACKUP_DAEMON=YES
	export IS_NETBACKUP_DAEMON

	BPRD_PID=`$PS | grep bprd | grep -v grep | awk '{print $1}'` 
	if [  "$BPRD_PID" != "" ]; then
		echo "stopping the NetBackup request daemon" 
		${BPRDREQ} -terminate 2> /dev/null
	fi

	BPDBM_PID=`$PS | grep bpdbm | grep -v grep | awk '{print $1}'`
	if [  "$BPDBM_PID" != "" ]; then
		echo "stopping the NetBackup database daemon" 
		/usr/openv/netbackup/bin/bpdbm -terminate 2> /dev/null
	fi

	LTID_PID=`$PS | grep ltid | grep -v grep | awk '{print $1}'`
	if [  "$LTID_PID" != "" ]; then
		echo "stopping the Media Manager device daemon" 
		${STOPLTID} 2> /dev/null
	fi

	VMD_PID=`$PS | egrep '[ /]vmd' | grep -v grep | awk '{print $1}'`
	if [  "$VMD_PID" != "" ]; then
		echo "stopping the Media Manager volume daemon" 
		${VMCTRLDBM} -t 2> /dev/null 
	fi

	#  Need to keep the now defunct -remove parameter in case
	#  NDMP has not been upgraded yet.

	NDMP_PID=`$PS | grep ndmpmove | grep -v grep | awk '{print $1}'`
	if [ "${NDMP_PID}" != "" -a -f /usr/openv/volmgr/bin/ndmpmoveragent.stop ] ; then
		/usr/openv/volmgr/bin/ndmpmoveragent.stop -remove > /dev/null 2>&1
		echo "stopping NetBackup NDMP Mover Agent" 
	fi

	nbar="`${BPPS} -r`"
	if [ "${nbar}" != "" -a -f ${nbar_location}/bin/nbar ] ; then
		echo "stopping NetBackup Advanced Reporter daemons" 
		${nbar_location}/bin/nbar stop >/dev/null 2>&1
	fi

	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

	ACSSSI_PID=`$PS | grep acsssi | grep -v grep | awk '{print $1}'`
	if [  "$ACSSSI_PID" != "" ]; then
		echo "stopping the Automated Cartridge System Storage Server Interface daemon" 
		# sleep matches what was already in bp.kill_all
		sleep 20
		stop_acs_daemon $ACSSSI_PID
	fi

	ACSSEL_PID=`$PS | grep acssel | grep -v grep | awk '{print $1}'`
	if [  "$ACSSEL_PID" != "" ]; then
		echo "stopping the Automated Cartridge System SSI Event Logger daemon" 
		stop_acs_daemon $ACSSEL_PID
	fi
#
# Lock file used by LINUX init
#
	rm -f /var/lock/subsys/netbackup
	;;
start_msg)
	echo "Starting NetBackup"  
	;;
stop_msg)
	echo "Stopping NetBackup"  
	;;
*)
	echo "usage: $0 [start | stop | start_msg | stop_msg ]"
	RETURN=1
	;;
esac
exit $RETURN

