#!/bin/sh
# $Header: S777netbackup.sh,v 1.8 2002/08/28 13:42:33 djs Stab $
#
#bcpyrght
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2002 VERITAS Software Corporation, All Rights Reserved $ *
#***************************************************************************
#ecpyrght
#
# S777netbackup.sh
#
# This is a sample script that can be copied to the HP-UX 10 and 11
# /sbin/rc2.d directory to automatically start the NetBackup request daemon
# at system # initiation.  It also starts the Media Manager device daemon
# (ltid).
#
# 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.

# HP-UX 10 and 11 call this script twice at startup.  Be sure to only
# do interesting stuff the second time when "start" is passed as the argument.

PATH=/sbin:/usr/sbin:/usr/bin
export PATH

ARG1="$1"
case "$ARG1" in
start)	;;
start_msg)
	echo "Starting netbackup" ; exit 0 ;;
stop_msg)
	echo "Stopping netbackup" ; exit 0 ;;
stop)
	exit 0 ;;
*)
	echo "Unknown argument: $ARG1" ; exit 1 ;;
esac

RETURN=0

VMPS=/usr/openv/volmgr/bin/vmps
LTID=/usr/openv/volmgr/bin/ltid 
BPRD=/usr/openv/netbackup/bin/initbprd
VISD=/usr/openv/netbackup/bin/visd
BPPS=/usr/openv/netbackup/bin/bpps
NBDBDMON=/usr/openv/netbackup/bin/admincmd/nbdbdmon

TMPF=/tmp/active_ovVM_processes
/bin/rm -f $TMPF
$VMPS > $TMPF

if [ -f "$LTID" ]
then
	ltid=`grep ltid $TMPF`
	# Don't start ltid if already started by HSM startup script.
	if test Y"$ltid" = Y
	then
		$LTID
		echo "Media Manager daemons started."
	fi
else
	RETURN=1
	echo "Media Manager daemons not started."
fi

/bin/rm -f $TMPF

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


# if nbdbd is installed, try to start up nbdbd and visd
if [ -x "${NBDBDMON}" ]; then
    # 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

    # start visd if present
    if [ -x "${VISD}" ]; then
        if [ "${nbdbd}" != "" ]; then
            ${VISD} > /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
fi

exit $RETURN
