#!/bin/sh
# $Id: cluster_config.sh,v 1.39.2.2 2004/01/12 17:17:55 $
#******************************************************************************
#* $VRTScprght: Copyright 1993 - 2004 VERITAS Software Corporation, All Rights Reserved $ *
#******************************************************************************
###############################################################################
#cluster_config.sh
#This script will configure all of the net backup clustering agents. 
#Cluster systems are auto detected.
###############################################################################


#initialize program variables 
CLEAR=clear
SED=sed
CUT=cut
CP=cp
GREP=grep
LS=ls
RM=rm

${CLEAR}

#
#Define Echo to allow escape characters
#
case "`uname -s`" in
        Linux*)
                unset POSIXLY_CORRECT
                ECHO="/bin/echo -e"
                ;;
        SunOS*)
                ECHO="/usr/bin/echo"
                ;;
        *)
                ECHO="echo"
                ;;
esac

SPACE=' '
TAB=`${ECHO} ' ' | tr ' ' '\011'`

#trap trapped HUP INT TERM 
trap trapped 1 2 15 

trapped () {

        ${ECHO} "A ^C was detected!"
	${ECHO} "Cleaning up" 
	if [ -n ${RSP} ] ; then 
		${ECHO} "Removing ${RSP}"	
		rm -f ${RSP}
	fi 
	${ECHO} "We did not remove anything from the shared disk. If NetBackup databases" 
	${ECHO} "were moved they are still there. You will have to replace them by hand"
	${ECHO} "If nothing was moved, you can remove any NetBackup resources or groups" 
	${ECHO} "and run /usr/openv/netbackup/bin/cluster/cluster_config again."

	exit 1
}

###
#  Make sure this is being run from root.
ISROOT=`id | egrep "^uid=0\("`
if [ "${ISROOT}" = "" ] ; then
	${ECHO} ""
	${ECHO} "$0 must be run while logged in as root."
	${ECHO} ""
	exit 2 
fi

#Check that we are logged into NBAC
#We dont redirect stderr because we want the error message.
/usr/openv/netbackup/bin/admincmd/bpauthorize -test_admin > /dev/null 
rc=$?
if [ ${rc} -ne 0 ] ; then 
	exit $rc
fi 


#First we check the type of OS we are running on and then determine the 
#clustering solutions available for that platform.
OS_TYPE=`uname -s`
OS_VERSION=`uname -r`
HOST=`uname -n | sed 's/\..*$//'`

#Get the NetBackup root.
NBU_ROOT=/usr/openv
NB_PATH=${NBU_ROOT}/netbackup

###
#initialize program vars
VCS_CLUSTER=false
TRU_CLUSTER=false
SUN_CLUSTER=false
VIRTUAL_NAME=""
NODES=""
BPCLNTCMD="/usr/openv/netbackup/bin/bpclntcmd -is_local_host"

###
#Setup the canned error message
CONFIG_ERROR_MSG="
Configuration failed! 
When the cause of the failure is fixed please perform the following steps. 
1. Add a SERVER = nodename line to bp.conf for each node in the cluster. 
Perform this task on all nodes in the cluster. 
2. Add a CLUSTER_NAME = virtualname to vm.conf and bp.conf on all nodes of 
the cluster. 
3. Add a DEVICE_HOST = virtualname line to vm.conf on all nodes of the cluster. 

" 

###############################################################################
#add_links
#adds DIR and LINK lines to rsp file
###############################################################################
add_links() { 
	${ECHO} "
DIR=db mkdir 
DIR=db/var mv
DIR=netbackup mkdir 
DIR=netbackup/db mv
DIR=volmgr mkdir
DIR=volmgr/database mkdir 
DIR=volmgr/misc mkdir
DIR=volmgr/misc/robotic_db mv 

LINK=db/var
LINK=volmgr/database/ruleDB
LINK=volmgr/database/volDB
LINK=volmgr/database/globDB
LINK=volmgr/database/poolDB
LINK=volmgr/misc/robotic_db 
LINK=volmgr/misc/SSOhosts 
LINK=netbackup/db " >> ${RSP}

}
###############################################################################
#active_node
#We make changes to the active nodes. This may end up moving some db's around
#for nbar and / or gdm in the future.
###############################################################################
active_node() {
	if [ ${TRU_CLUSTER} != "true" ] ; then 
		${ECHO} "Configuring node ${HOST} for clustering"
	fi 

	#
	#Remove start and stop scripts for NetBackup
	#

	if [ ${OS_TYPE} = "HP-UX" ] ; then 
		rm -f /sbin/rc2.d/S777netbackup 
		rm -f /sbin/rc0.d/K777netbackup 
		rm -f /sbin/rc1.d/K001netbackup
	elif [ ${OS_TYPE} = "Linux" ] ; then
		rm -f /etc/rc.d/rc2.d/S77netbackup 
		rm -f /etc/rc.d/rc3.d/S77netbackup 
		rm -f /etc/rc.d/rc5.d/S77netbackup 
		rm -f /etc/rc.d/rc6.d/K77netbackup 
		rm -f /etc/rc.d/rc6.d/K01netbackup 
		rm -f /etc/rc.d/rc0.d/K01netbackup 
		rm -f /etc/rc.d/rc0.d/K77netbackup
	elif [ ${OS_TYPE} = "AIX" ] ; then
		rm -f /etc/rc.veritas.aix
	elif [ ${OS_TYPE} = "SunOS" ] ; then
		rm -f /etc/rc2.d/S77netbackup 
		rm -f /etc/rc0.d/K77netbackup 
		rm -f /etc/rc0.d/K01netbackup 
	elif [ ${OS_TYPE} = "OSF1" ] ;then 
		rm -f /sbin/rc0.d/K77netbackup	
		rm -f /sbin/rc0.d/K01netbackup	
		rm -f /sbin/rc2.d/S77netbackup
		rm -f /sbin/rc3.d/S77netbackup
	fi

	#
	#Add node names as SERVER lines in bp.conf 
	#Also add CLUSTER_NAME to bp.conf and vm.conf
	#Set DEVICE_HOST in vm.conf
	#

	for nodename in ${NODES} ; do 
		egrep "^[${SPACE}${TAB}]*SERVER[${SPACE}${TAB}]*=[${SPACE}${TAB}]*${nodename}" ${NB_PATH}/bp.conf > /dev/null 2>&1
		if [ $? -ne 0 ] ; then 
			${ECHO} SERVER=${nodename} >> ${NB_PATH}/bp.conf
		fi
	done 

	egrep "^[${SPACE}${TAB}]*CLUSTER_NAME[${SPACE}${TAB}]*=[${SPACE}${TAB}]*${VIRTUAL_NAME}" ${NB_PATH}/bp.conf > /dev/null 2>&1
	if [ $? -ne 0 ];  then 
		${ECHO} "CLUSTER_NAME=${VIRTUAL_NAME}" >> /usr/openv/netbackup/bp.conf
	fi

	cat /usr/openv/volmgr/vm.conf > /dev/null 2>&1
	if [ $? -ne 0 ] ; then 
		touch /usr/openv/volmgr/vm.conf
	fi
	egrep "^[${SPACE}${TAB}]*CLUSTER_NAME[${SPACE}${TAB}]*=[${SPACE}${TAB}]*${VIRTUAL_NAME}" ${NBU_ROOT}/volmgr/vm.conf > /dev/null 2>&1
	if [ $? -ne 0 ];  then 
		${ECHO} "CLUSTER_NAME=${VIRTUAL_NAME}" >> ${NBU_ROOT}/volmgr/vm.conf
	fi
	egrep "^[${SPACE}${TAB}]*DEVICE_HOST[${SPACE}${TAB}]*=[${SPACE}${TAB}]*${VIRTUAL_NAME}" ${NBU_ROOT}/volmgr/vm.conf > /dev/null 2>&1
	if [ $? -ne 0 ] ; then 
		${ECHO} "DEVICE_HOST=${VIRTUAL_NAME}" >> /usr/openv/volmgr/vm.conf
	fi  
}

###############################################################################
#all_nodes
#This will do configuration on all nodes + any other details that need to be 
#done
###############################################################################
all_nodes() { 
	

	if [ ${SUN_CLUSTER} = "true" ] ; then 
		NODES=`grep NODES ${RSP} | sed -e s/NODES=//`
	elif [ ${TRU_CLUSTER} = "true" ] ; then
		active_node 
		return
	elif [ ${VCS_CLUSTER} = "true" ] ; then 
		SHARED_DISK=`grep SHARED_DISK ${RSP} | sed -e s/SHARED_DISK=//`
	else 
		exit 1 #Not a cluster 
	fi

	#
	#Find out if / where /usr/openv is a link to
	#

	start="/usr/openv"
	while [ "$start" != '' ]; do
		next=`ls -ld "$start" 2> /dev/null | grep '^l' | sed 's/^.* -> //'`
		if [ "$next" = '' ]; then
			break
		fi
		start="$next"
	done

	if [ "/usr/openv" = "${SHARED_DISK}" ] ; then 
		ALL_SHARED=true
	elif [ "/usr/openv/" = "${SHARED_DISK}" ] ; then 
		ALL_SHARED=true
	elif [ "${start}" = "${SHARED_DISK}" ] ; then 
		ALL_SHARED=true
	elif [ "${start}/" =  "${SHARED_DISK}" ] ; then 
		ALL_SHARED=true
	else 
		ALL_SHARED=false
	fi

	if [ "${ALL_SHARED}" != "true" ] ; then 
		add_links
	fi	

	for node in ${NODES} ; do

		RSH_CMD="rsh ${node}"
		if [ "${OS_TYPE}" = "HP-UX" ] ; then 
			RSH_CMD="remsh ${node}"
		fi
		
		#
		#If we are on the active node (the node cluster_config is being ran from
		#do the active node tasks instead of the inactive node tasks. 
		#

		${BPCLNTCMD} ${node} > /dev/null 2>&1
		if [ $? -eq 0 ] ; then 
			active_node
			continue	
		fi

		${ECHO} "Configuring node ${node} for clustering."
		
		#
		#Add node names as SERVER lines in bp.conf 
		#Also add CLUSTER_NAME to bp.conf and vm.conf
		#Set DEVICE_HOST in vm.conf
		#

		for nodename in ${NODES} ; do 
			rc=`${RSH_CMD} "egrep \"^[${SPACE}${TAB}]*SERVER[${SPACE}${TAB}]*=[${SPACE}${TAB}]*${nodename}\" ${NB_PATH}/bp.conf > /dev/null 2>&1 ; ${ECHO} \\$?"`
			if [ ${rc} -ne 0 ] ;then 
				${RSH_CMD} "${ECHO} \"SERVER=${nodename}\" >> /usr/openv/netbackup/bp.conf"
			fi
		done

		rc=`${RSH_CMD} "egrep \"^[${SPACE}${TAB}]*CLUSTER_NAME[${SPACE}${TAB}]*=[${SPACE}${TAB}]*${VIRTUAL_NAME}\" ${NB_PATH}/bp.conf > /dev/null 2>&1 ; ${ECHO} \\$?"`
		if [ ${rc} -ne 0 ] ;then 
			${RSH_CMD} "${ECHO} \"CLUSTER_NAME=${VIRTUAL_NAME}\" >> /usr/openv/netbackup/bp.conf"
		fi

		rc=`${RSH_CMD} "cat /usr/openv/volmgr/vm.conf > /dev/null 2>&1 ; ${ECHO} \\$?"`
		if [ ${rc} -ne 0 ] 
		then 
			${RSH_CMD} "touch /usr/openv/volmgr/vm.conf"
		fi
		rc=`${RSH_CMD} "egrep \"^[${SPACE}${TAB}]*CLUSTER_NAME[${SPACE}${TAB}]*=[${SPACE}${TAB}]*${VIRTUAL_NAME}\" ${NBU_ROOT}/volmgr/vm.conf > /dev/null 2>&1 ; ${ECHO} \\$?"`
		if [ ${rc} -ne 0 ]
		then 
			${RSH_CMD} "${ECHO} \"CLUSTER_NAME=${VIRTUAL_NAME}\" >> ${NBU_ROOT}/volmgr/vm.conf"
		fi
		rc=`${RSH_CMD} "egrep \"^[${SPACE}${TAB}]*DEVICE_HOST[${SPACE}${TAB}]*=[${SPACE}${TAB}]*${VIRTUAL_NAME}\" ${NBU_ROOT}/volmgr/vm.conf > /dev/null 2>&1 ; ${ECHO} \\$?"`
		if [ ${rc} -ne 0 ] 
		then 
			${RSH_CMD} "${ECHO} \"DEVICE_HOST=${VIRTUAL_NAME}\" >> /usr/openv/volmgr/vm.conf"
		fi  


		rcp -p ${RSP} ${node}:${RSP}
		if [ $? -ne 0 ] 
		then
			${ECHO} "There was a problem copying ${RSP} to the inactive nodes. Do this manually"
		fi
		
		#
		#remove .do_templates
		#

		${RSH_CMD} "rm -f /usr/openv/netbackup/bin/cluster/.do_templates > /dev/null 2>&1"

		# 
		#Remove start and stop scripts for netbackup 
		#from the other nodes of the cluster
		#

		if [ ${OS_TYPE} = "HP-UX" ] ; then 
			${RSH_CMD} "rm -f /sbin/rc2.d/S777netbackup /sbin/rc0.d/K777netbackup /sbin/rc1.d/K001netbackup" 
		elif [ ${OS_TYPE} = "Linux" ] ; then
			${RSH_CMD} "rm -f /etc/rc.d/rc2.d/S77netbackup /etc/rc.d/rc3.d/S77netbackup /etc/rc.d/rc5.d/S77netbackup /etc/rc.d/rc6.d/K77netbackup /etc/rc.d/rc6.d/K01netbackup /etc/rc.d/rc0.d/K01netbackup /etc/rc.d/rc0.d/K77netbackup" 
		elif [ ${OS_TYPE} = "AIX" ] ; then
			${RSH_CMD} "rm -f /etc/rc.veritas.aix"
		elif [ ${OS_TYPE} = "SunOS" ] ; then
			${RSH_CMD} "rm -f /etc/rc2.d/S77netbackup /etc/rc0.d/K77netbackup /etc/rc0.d/K01netbackup "
		fi 
	done
}

###############################################################################
#do_templates creates the NetBackup templates on the active node
###############################################################################
do_templates() { 
	${ECHO} "Setting up the NetBackup templates"
	${NB_PATH}/bin/cluster/.do_templates
	if [ $? -ne 0 ];then
		${ECHO} "The NetBackup templates were not created successfully"
	fi
}

###############################################################################
#Gets license keys from machine
#Function has one parameter, the Add on to lookup
###############################################################################
get_product_info () {
	case "${1}" in
		NBADV_CLNT)
		key_bits="24 41 52 53 55 59 62"
		;;
		NBARO)
		key_bits="31"
		;;
		NBDSTORE)
		key_bits="46"
		;;
		NBEncryption_40)
		key_bits="42"
		;;
		NBEncryption_56)
		key_bits="43"
		;;
		NBGDM)
		key_bits="28 50"
		;;
		NBNDMP)
		key_bits="27"
		;;
		NBVAULT)
		key_bits="54"
		;;
		*)
		${ECHO} ""
		${ECHO} "Unknown Add-On Product ${1}."
		${ECHO} "Add-On product failover configuration aborted."
		;;
	esac
}

###############################################################################
#get_monitors
#This function will populate the list of functions to monitor.  
#The function has one parameter, operating system type.
###############################################################################
get_monitors () {

	ANSWER="" 
	rc=0;
	get_monitor_choices

	if [ ${rc} -ne 1 -a -n "${aval_prods}" ] ; then 
		${ECHO} "you have selected to monitor ${aval_prods}." 
	elif [ -z "${aval_prods}" ] ; then 
		${ECHO} "You have no add on's avaliable to monitor."
		exit 1
	else 
		${ECHO} "There was problem finding license keys for ${aval_prod}!"
		${ECHO} "Do you have the keys installed?"
		exit 1
	fi
	ANSWER="" 
}

###############################################################################
#get_monitor_choices
#This displays the choices of add on products available to the cluster 
#The function has one parameter, the operating system type.
###############################################################################
get_monitor_choices () { 
	lic_file="/usr/openv/var/license.txt"

	if [ ! -f ${lic_file} -o ! -s ${lic_file} ] ; then
		${ECHO} ""
		${ECHO} "The license key file doesn't exist or is empty."
		${ECHO} ""
		rc=1
		return
	else
		grep "VMD5_DIGEST" ${lic_file} >/dev/null 2>&1
		if [ $? != 0 ] ; then
			${ECHO} ""
			${ECHO} "The license key file is missing a checksum."
			${ECHO} ""
			rc=1
			return
		else
			${NB_PATH}/bin/admincmd/bpminlicense -nb_features > /dev/null 2>&1
			if [ $? = 11 ] ; then
				${ECHO} ""
				${ECHO} "No license key exists for NetBackup Server or Enterprise Server."
				${ECHO} ""
				rc=1
				return
			fi
		fi
	fi

	#These are the products we can monitor for failover
	aval_prods=""
	while [ "${CORRECT}" != "y" ] ; do
		CORRECT=Y
		get_prod_input
		validate_prod_input
		if [ "${ADD}" = "a" -o "${ADD}" = "A" ] ; then 
			${ECHO} "You have chosen to monitor ${aval_prods}. Is this list correct? [y,n] (${CORRECT}) "
		else
			${ECHO} "You have chosen to remove ${aval_prods}. Is this list correct? [y,n] (${CORRECT}) "
		fi
		read CORRECT
	done
}

###############################################################################
#This function will validate the users input for the processes to monitor
###############################################################################
validate_prod_input () { 

	${ECHO} "VALIDATING PRODUCT INPUT"
	NBAR=0
	GDM=0  
	NDMP=0
	${ECHO} "${aval_prods}"
	for prod in ${aval_prods} ; do
		case "${prod}" in
			NBAR|nbar)
			NBAR=1
			;;
			GDM|gdm)
			GDM=1
			;;
			NDMP|ndmp)
			NDMP=1
			;;
			*)
			${ECHO} ""
			${ECHO} "${prod} can not be monitored at this time."
			${ECHO} "${prod} is being ignored."
			${ECHO} "${aval_prods}" > /tmp/tmpprod
			aval_prods=`sed -e s/${prod}// /tmp/tmpprod`
			rm -f /tmp/tmpprod
			;;
		esac
	done
}

###############################################################################
#This function will get the add on products the user would like to monitor
###############################################################################
get_prod_input () { 
	${ECHO} "The following process are available to monitor:"
	PRODUCT="NBARO NBGDM NBNDMP"
	for prod in ${PRODUCT} ; do
		get_product_info ${prod}

		#
		#  To make sure bpminlicense strings are in English.
		#  Since this script is not internationalized, use the
		#  all encompassing LC_ALL variable.  Check for exit
		#  status of bpminlicense when using nb_ufid since
		#  output in and of itself is not good enough.
		#

		LC_ALL=C
		export LC_ALL
		bit_found=0
		for bit in ${key_bits} ; do
			if [ ${bit_found} -eq 0 ] ; then
				list=`${NB_PATH}/bin/admincmd/bpminlicense -nb_ufid ${bit} 2>/dev/null`
				if [ $? -eq 0 -a "${list}" != "" ] ; then
					bit_found=1
				fi
			fi
		done
		if [ ${bit_found} -eq 0 ] ; then
			#lic not found
			${ECHO} ""
		else
			if [ ${prod} = "NBARO" ] ; then 
				if [ -x /usr/openv/nbar/bin/ardbd ] ; then 
					${ECHO} " NetBackup Advanced Reporter (NBAR) " 
					aval_prods="${aval_prods} NBAR"
					MON_PROCS="${MON_PROCS} ardbd armonitor"
				else
					${ECHO} "NBAR license keys are installed but ardbd was not found. Please install NBAR"
				fi 
			elif [ ${prod} = "NBGDM" ] ; then 
				if [ -x /usr/openv/netbackup/bin/visd ] ; then 
					${ECHO} " NetBackup Global Data Manager (GDM) " 
					aval_prods="${aval_prods} GDM"
					MON_PROCS="${MON_PROCS} visd" 
				else
					${ECHO} "NBAR license keys are installed but visd was not found. Please install GDM"
				fi 
			elif [ ${prod} = "NBNDMP" ] ; then 
				if [ -x /usr/openv/volmgr/bin/ndmpmoveragent ] ; then 
					${ECHO} " NetBackup for NDMP (NDMP)"
					aval_prods="${aval_prods} NDMP"
					MON_PROCS="${MON_PROCS} ndmpmoveragent" 
				else 
					${ECHO} "NBAR license keys are installed but ndmpmoveragent was not found. Please install NDMP"
				fi 
			fi 
		fi
		unset LC_ALL
	done

	if [ -n "${aval_prods}" ] ; then 
		INPUT="${aval_prods}" #set the defaults 
		${ECHO} "\nEnter the name of the add on options you would like to monitor [${INPUT}]:" 
		read TMP_INPUT 
		if [ -n "${TMP_INPUT}" ] ; then 
			INPUT=${TMP_INPUT}
		fi
		aval_prods=${INPUT}
	else 
		${ECHO} "\n You have no products available to monitor." 
		exit 1
	fi 
}

###############################################################################
#caa_config
#This will run the caa_config
#scripts for TruCluster.
#The function has on parameter
#the path to the caa_config_nbu
#file.
###############################################################################
caa_config() {
	${CAA_CONFIG}
	if [ $? -eq 0 ]
	then
		${ECHO} "The NetBackup TruCluster configuration was successful!!"
	else
		${ECHO} "${CONFIG_ERROR_MSG}"
		
		if [ -f ${NB_PATH}/cluster/.do_templates ] ; then 
			${ECHO} "After NetBackup is brought online run:"
			${ECHO} "${TAB}${NB_PATH}/cluster/.do_templates"
			${ECHO} "After this NetBackup will be ready for use"
		fi

		exit 1
	fi
}

###############################################################################
#scnb_config
#This will run the config utility for Sun 
#Cluster. 
#This function has one parameter, the path to
#the scnb_config utility. 
###############################################################################
scnb_config() { 
	${ECHO} "Configuring NetBackup Sun Cluster Agent"
	${SUN_CLUSTER_PATH}/scnb_config
	if [ $? -eq 0 ] 
	then 
		${ECHO} "The NetBackup Sun Cluster configuration was successful!" 
	else 
		${ECHO} "${CONFIG_ERROR_MSG}"
		
		if [ -f ${NB_PATH}/cluster/.do_templates ] ; then 
			${ECHO} "After NetBackup is brought online run:"
			${ECHO} "${TAB}${NB_PATH}/cluster/.do_templates"
			${ECHO} "After this NetBackup will be ready for use"
		fi
		exit 1
	fi
}

###############################################################################
#vcs_config
#This will run the vcs config utility on all platforms
###############################################################################
vcs_config() {
	#
	#Check if we are doing a VCS agent upgrade from 3.5 agent
	#
	if [ "${1}" = "-vcs_agent_upgrade" ] ; then 
		VCS_AGENT_UPGRADE=true
	fi

	if [ -f ${RSP} ]; then 
		#Do a check for configuring add on's. 
		if [ "${1}" = "-a" ] ; then 
			addon
			exit 0
		elif [ "${1}" = "-r" ] ; then 
			robot
			exit 0
		fi 
		${ECHO} "Your system appears to have already been configured for VCS support."
		${ECHO} "Do you want to reconfigure your system? [y,n] \c" 
		ANSWER=""
		read ANSWER
		if [ ${ANSWER} = "y" -o ${ANSWER} = "Y" ]; then 
			#run the VCS Configuration script.. 
			if [ "${VCS_AGENT_UPGRADE}" = "true" ] ; then 
				${NBU_ROOT}/netbackup/bin/cluster/vcs/vcs_nbu_config -vcs_agent_upgrade
			else
				${NBU_ROOT}/netbackup/bin/cluster/vcs/vcs_nbu_config
			fi

			if [ $? -eq 0 ]; then 
				${ECHO} "The NetBackup VCS configuration was successful!"
			elif [ $? -eq 99 ] ; then 
				exit 99 
			else 
				${ECHO} "${CONFIG_ERROR_MSG}"
				
				if [ -f ${NB_PATH}/cluster/.do_templates ] ; then 
					${ECHO} "After NetBackup is brought online run:"
					${ECHO} "${TAB}${NB_PATH}/cluster/.do_templates"
					${ECHO} "After this NetBackup will be ready for use"
				fi

				exit 1
			fi
		elif [ "$ANSWER" = "n" -o "${ANSWER}" = "N" ] ; then 	
			exit 0
		fi
		ANSWER=""
	else 
		#run the VCS Configuration script.. 
		if [ "${VCS_AGENT_UPGRADE}" = "true" ] ; then 
			${NBU_ROOT}/netbackup/bin/cluster/vcs/vcs_nbu_config -vcs_agent_upgrade
		else
			${NBU_ROOT}/netbackup/bin/cluster/vcs/vcs_nbu_config
		fi
		if [ $? -eq 0 ]; then 
			${ECHO} "The NetBackup VCS configuration was successful!"
		elif [ $? -eq 99 ] ; then 
			exit 99 
		else 
			${ECHO} "${CONFIG_ERROR_MSG}"
			
			if [ -f ${NB_PATH}/cluster/.do_templates ] ; then 
				${ECHO} "After NetBackup is brought online run:"
				${ECHO} "${TAB}${NB_PATH}/cluster/.do_templates"
				${ECHO} "After this NetBackup will be ready for use"
			fi
			exit 1
		fi
	fi
	NODES=`grep NODES ${RSP} | sed -e s/NODES=//`
	VIRTUAL_NAME=`grep VNAME ${RSP} | sed -e s/VNAME=//`
}


##############################################################################
#addon
#configure addon options for sun cluster
##############################################################################
addon() { 

	${ECHO} "Would you like to add or delete add-ons? [a,d] (a): \c"
	ADD=a
	read TMP_ADD
	if [ -n "${TMP_ADD}" ] ; then 
		ADD=${TMP_ADD}
	fi 

	if [ "${ADD}" != "a" -a ${ADD} != "A" -a "${ADD}" != "d" -a "${ADD}" != "D" ] ; then 
		${ECHO} "${ADD} is not a valid value!" 
		exit 1
	fi

	get_monitors ${OS_TYPE} #Get the add ons we want. 
	if [ "${ADD}" = "a" -o "${ADD}" = "A" ] ; then 
		#Find old processes
		OLD_PROCS=`grep PROBE_PROCS ${RSP} | sed -e s/PROBE_PROCS=//g`
		#Keep the original scnb_probe_usr. 
		cp ${RSP}  ${RSP}.orig
		#Add the new processes in and create a new file
		sed -e s/PROBE_PROCS=.*/"PROBE_PROCS=${OLD_PROCS}${MON_PROCS}"/ ${RSP} > ${RSP}_tmp
		#Copy the old file over the new file. 
		if [ $? -eq 0 ]; then 
			cp ${RSP}_tmp ${RSP}
		else 
			${ECHO} "Error while adding ${MON_PROCS} to ${RSP}!!!!"
			${ECHO} "Add ${MON_PROCS} to ${RSP} by hand."
			rm ${RSP}_tmp
			exit 1
		fi 
		#Remove the tmp file. 
		rm ${RSP}_tmp
	else
		${ECHO} "We will remove the followwing add on's:"
		${ECHO} "${MON_PROCS}"
		${ECHO} "REMOVING add on daemons from ${RSP}"
		for proc in ${MON_PROCS} ; do 
			sed -e s/${proc}// ${RSP} > ${RSP}_tmp
			if [ $? -eq 0 ] ; then 
				mv ${RSP}_tmp ${RSP}
			else 
				${ECHO} "Failed to remove ${proc} from ${RSP}!"
				${ECHO} "exiting"
				exit 1
			fi 
		done
	fi 
	NODES=`grep NODES $RSP | sed -e s/NODES=//`

	#Copy the RSP out to the other nodes. 
	for node in ${NODES} ; do 
		${BPCLNTCMD} ${node} > /dev/null 2>&1
		if [ $? -eq 0  ] ; then 
			continue
		fi 
		rcp -p ${RSP} $node:${RSP}
		if [ $? -ne 0 ] ; then 
			${ECHO} "Could not copy ${RSP} to ${node}. Please copy by hand."
		fi 
	done
	${ECHO} "Done."
}

##############################################################################
#robot 
#Configure robotic daemons for motoring. 
##############################################################################
robot() { 
	${ECHO} "Would you like to add or delete devices? [a,d] (a): \c"
	ADD=a
	read TMP_ADD
	if [ -n "${TMP_ADD}" ] ; then 
		ADD=${TMP_ADD}
	fi 

	#
	#Make sure we have a good value for the addon
	#

	if [ "${ADD}" != "a" -o ${ADD} != "A" -o "${ADD}" != "d" -o "${ADD}" != "D" ] ; then 
		${ECHO} "${ADD} is not a valid value!" 
		exit 1
	fi 

	${ECHO} " 
	Enter the number of the robotic daemon you would like to monitor. 

	1. ACS
	2. LMF
	3. ODL 
	4. TL4
	5. TL8 
	6. TLD
	7. TLH 
	8. TLM
	9. TS8 
	10. TSD 
	11. TSH 
	" 

	ROBOTIC_DEAMON=""
	while [ "${ANSWER}" != "q*" -o "${ANSWER}" != "Q*" ] ; do 
		ANSWER=""
		${ECHO} "Enter the drive number (q to quit) \c"
		read ANSWER
		case ${ANSWER} in 
			ACS*|acs*|1) 
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} acsd"
			;;
			LMF*|lmf*|2)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} lmfcd"
			;;
			ODL*|odl*|3)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} odld"
			;;
			TL4*|tl4*|4)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} tl4d"
			;;
			TL8*|tl8*|5)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} tl8cd"
			;; 
			TS8*|ts8*|6)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} ts8d"
			;;
			TLD*|tld*|7)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} tldcd"
			;;
			TLH*|tlh*|8)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} tlhcd"
			;;
			TLM*|tlm*|9)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} tlmd"
			;;
			TSD*|tsd*|10)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} tsdd"
			;;
			TSH*|tsh*|11)
			ROBOTIC_DEAMON="${ROBOTIC_DEAMON} tshd"
			;;
			Q*|q*)
			break
			;;
			*) 
			${ECHO} "${ANSWER} is not a valid option!"
		esac

	done 
	if [ -z ${ROBOTIC_DEAMON} ] ; then 
		${ECHO} "No daemons selected!"
		exit 1
	fi 
	if [ "${ADD}" = "a" -o "${ADD}" = "A" ] ; then 
		${ECHO} "We will monitor the following daemons:"
		${ECHO} "${ROBOTIC_DEAMON}"
		${ECHO} "Adding robotic daemons from ${RSP}"
		OLD_PROCS=`grep PROBE_PROCS ${RSP} | sed -e s/PROBE_PROCS=//g`
		cp ${RSP}  ${RSP}.orig
		#Add the new processes in and create a new file
		sed -e s/PROBE_PROCS=.*/"PROBE_PROCS=${OLD_PROCS}${ROBOTIC_DEAMON}"/ ${RSP} > ${RSP}_tmp
		if [ $? -eq 0 ]; then 
			cp ${RSP}_tmp ${RSP}
		else 
			${ECHO} "Error while adding ${ROBOTIC_DEAMON} to ${RSP}!!!!"
			${ECHO} "Add ${ROBOTIC_DEAMON} to ${RSP} by hand."
			rm ${RSP}_tmp
			exit 1
		fi 
		#Remove the tmp file. 
		rm ${RSP}_tmp
	else
		${ECHO} "We will remove the followwing daemons:"
		${ECHO} "${ROBOTIC_DEAMON}"
		${ECHO} "REMOVING robotic daemons from ${RSP}"
		for proc in ${ROBOTIC_DEAMON} ; do 
			sed -e s/${proc}// ${RSP} > ${RSP}_tmp
			if [ $? -eq 0 ] ; then 
				mv ${RSP}_tmp ${RSP}
			else 
				${ECHO} "Failed to remove ${proc} from ${RSP}!"
				${ECHO} "exiting"
				exit 1
			fi 
		done 	
	fi

	NODES=`grep NODES $RSP | sed -e s/NODES=//`

	#Copy the RSP out to the other nodes. 
	for node in ${NODES} ; do 
		${BPCLNTCMD} ${node} > /dev/null 2>&1
		if [ $? -eq 0  ] ; then 
			continue
		fi 
		rcp -p ${RSP} $node:${RSP}
		if [ $? -ne 0 ] ; then 
			${ECHO} "Could not copy ${RSP} to ${node}. Please copy by hand."
		fi 
	done
	${ECHO} "Done."
} 

##############################################################################
#Onlines the netbackup in the cluster. 
#START_ERROR_MSG should be set before calling this. 
##############################################################################
online () { 
	${ECHO} "Waiting for NetBackup to come online..." 
	if [ "${TRU_CLUSTER}" = "true" ] ; then
		${NB_PATH}/bin/cluster/cluster_active
		if [ $? -ne 0 ] ; then 
			caa_start ${CAA_PROFILE_NAME} -c ${HOST}
			if [ $? -ne 0 ] ; then 
				${ECHO} "${START_ERR_MSG}" 
				exit 1
			fi
		fi 
	elif [ "$VCS_CLUSTER" = "true" ] ; then
		#get VCS profile name from VCS_NBU_RSP
		VCS_GRP_NAME=`grep NBU_GROUP ${RSP} | sed -e s/NBU_GROUP// -e s/=//`
		/opt/VRTSvcs/bin/hagrp -online ${VCS_GRP_NAME} -sys ${HOST} > /dev/null 2>&1 
		rc=$?
		sleep 20
		if [ ${rc} -ne 0 ] ; then
			${ECHO} "${START_ERR_MSG}"
		fi 
	elif [ "${SUN_CLUSTER}" = "true" ]  ; then 
		SCRG_NAME=scnb-harg

		#
		#vodo to make sure suncluster starts and initilizes correctly. 
		#
		
		/usr/cluster/bin/scswitch -Z -g ${SCRG_NAME}
		if [ $? -ne 0 ] ; then
			${ECHO} "${START_ERR_MSG}"
			exit 1
		fi 
		/usr/cluster/bin/scswitch -F -g ${SCRG_NAME}
		if [ $? -ne 0 ] ; then
			${ECHO} "${START_ERR_MSG}"
			exit 1
		fi 
		/usr/cluster/bin/scswitch -z -g ${SCRG_NAME} -h ${HOST}
		if [ $? -ne 0 ] ; then
			${ECHO} "${START_ERR_MSG}"
			exit 1
		fi 
	fi 
	${ECHO} "NetBackup is online..." 
} 

##############################################################################
##############################################################################
##Main Program starts here
##############################################################################
##############################################################################

###
#Are we are configuring a TruCluster?
if [ ${OS_TYPE} = "OSF1" ] ; then
	if [ ${OS_VERSION} = "V5.1" ] ; then
		if [ ! -x /usr/bin/caa_stat ]; then 
			${ECHO} "Can not find caa_stat. Is this really a cluster?"
			exit 1
		fi
		#Set the path to the NetBackup CAA scripts.
	CAA_PATH=${NB_PATH}/bin/cluster/trucluster
		CAA_CONFIG=${CAA_PATH}/caa_config_nbu
		CAA_PROFILE=/usr/sbin/caa_profile #command 
		CAA_PROFILE_NAME="" #nbu profile name
		TRU_CLUSTER=true	
		RSP=${CAA_PATH}/CAA_NBU_RSP
		#Does a profile exist? 
		if [ -f ${RSP} ] ; then 
			#Do a check for configuring add on's. 
			if [ "${1}" = "-a" ] ; then 
				addon
				exit 0
			elif [ "${1}" = "-r" ] ; then 
				robot
				exit 0
			fi 
			#Now we get the NetBackup profile name. 
			CAA_PROFILE_NAME=`grep PROFILE ${RSP} | sed -e s/PROFILE=//`
			VIRTUAL_NAME=`grep ALIAS ${RSP} | sed -e s/ALIAS=//`

			${ECHO} "Your system appears to have already been configured for TruCluster support."
			${ECHO} "We have detected your profile name to be $CAA_PROFILE_NAME"
			${ECHO} "Do you want to reconfigure your system? [y,n] \c" 
			
			#Run the TruCluster config util. 
			read ANSWER	
			if [ ${ANSWER} = "y" ] || [ ${ANSWER} = "Y" ];  then 
				caa_config
			else
				exit 0 
			fi 

		else 
			#check to make sure the caa_config is present if it is we configure
			if [ -f ${CAA_CONFIG} ] ; then
				caa_config
				if [ $? -eq 0 ] ; then 
					${ECHO} "Configuration was successful!" 
				fi 
			else
				#we need to say where to get these scripts 
				${ECHO} "Error finding ${CAA_CONFIG}" 
				exit 1
			fi

		fi 
		NODES=`grep NODES ${RSP} | sed -e s/NODES=//`
		CAA_PROFILE_NAME=`grep PROFILE ${RSP} | sed -e s/PROFILE=//`
	else 
		${ECHO} "NetBackup for TruCluster is not supported on this platform"
		exit 1
	fi 	

###############################################################################
#We are on an AIX, HP-UX or Linux Box
###############################################################################
elif [ ${OS_TYPE} = "AIX" ] || [ ${OS_TYPE} = "Linux" ] \
	|| [ ${OS_TYPE} = "HP-UX" ] ; then
	#Are we running in a VCS Cluster
	if [ -d /var/VRTSvcs ]; then 
		VCS_CLUSTER=true
		RSP=${NBU_ROOT}/netbackup/bin/cluster/vcs/VCS_NBU_RSP
		vcs_config ${1}

	else #VCS is not found  
		${ECHO} "VCS was not detected."
		exit 1
	fi

###############################################################################
#We are on a Solaris Box
###############################################################################
elif [ ${OS_TYPE} = "SunOS" ]; then 

	#Check for the Sun Cluster package. 
	pkginfo SUNWscr  > /dev/null 2>&1
	if [ $? -eq 0 ]  #Did we find the sun cluster packages
	then 
		SUN_CLUSTER=true
		SUN_CLUSTER_PATH=${NB_PATH}/bin/cluster/suncluster
		RSP=${SUN_CLUSTER_PATH}/SC_NBU_RSP

		##########
		##########
		#If SUN ends up writing their own agent we will possibly need to 
		#do some upgrade checking here 
		##########
		##########

		#This check will change possibly with the new agent
		if [ -f ${RSP} ] ;then
			#Do a check for configuring add on's and robotic daemon flags. 
			if [ "${1}" = -a ] ; then 
				addon
				exit 0
			elif [ "${1}" = "-r" ] ; then 
				robot
				exit 0
			fi 

			${ECHO} "Your system appears to have already been configured for Sun Cluster support."
			${ECHO} "Do you want to reconfigure your system? [y,n] \c" 
			read ANSWER

			if [ ${ANSWER} = "y" -o ${ANSWER} = "Y" ] 
			then 
				scnb_config #Configure the cluster.
			else 
				exit 1
			fi
		else 
			scnb_config #Configure the cluster. 
		fi
		VIRTUAL_NAME=`grep VNAME ${RSP} | sed -e s/VNAME=//`

	#VCS Configuration on Solaris
	elif [ -d /var/VRTSvcs/ ]; then
		VCS_CLUSTER=true
		RSP=${NBU_ROOT}/netbackup/bin/cluster/vcs/VCS_NBU_RSP
		vcs_config ${1}
	else 
		${ECHO} "No cluster system was detected! Please install VCS or Sun Cluster."
	fi
fi

all_nodes #Do configuration tasks on all nodes

#Set the error message for the online. 
START_ERR_MSG="Failed to start the NetBackup resource. Check your configuration.\n When the NBU Resources come online run ${NB_PATH}/bin/cluster/.do_templates\n When .do_templates completes run rm ${NB_PATH}/bin/cluster/.do_templates\n"
online #Online the cluster
#
#Do the templates
if [ -f ${NB_PATH}/bin/cluster/.do_templates ] ; then

	#start services per platform
	#
	#run the do templates script. 
	#
	do_templates

	rm -f /usr/openv/netbackup/bin/cluster/.do_templates
fi

exit 0
