#!/bin/sh
# $Revision: 1.31.4.2 $
#***************************************************************************
# $VRTScprght: Copyright 1993 - 2004 VERITAS Software Corporation, All Rights Reserved $
#***************************************************************************
#
# You need to be root to run this script.
#
# This script fixes up the files on the client so that they are in the correct
# place and have the needed permissions. It moves the files from /tmp/bp/bin
# to /usr/openv/netbackup/bin. It then fixes up the permissions and updates the
# /etc/services file to add bprd, bpcd, bpjava-msvc, vopied, and vnetd. It
# also fixes up /etc/inetd.conf with bpcd, bpjava-msvc, vopied, and vnetd.

# The following variable is changed by the Makefile to a valid value.
MACHINE=Solaris

# INSERT fn.set_echo_var
#---------- set_echo_var -- $Revision: 1.2 $ ------------
#
#		This function is a case statement sets
#		the ECHO variable
#		with the appropriate path & flags.

#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

# INSERT fn.set_tar_options
#---------- set_tar_options -- $Revision: 1.9 $ ------------
#
#		This function takes one parameter, a
#		string which is the MACHINE or platform type.
#		It sets two variables: TAR_C_OPTIONS and
#			TAR_X_OPTIONS
#
#		NOTE: The v flag is not handled in this function. If you want 
#		to add the v flag, you need to set it in your scripts.
#		For example, place the v before the variable like so, v${TAR_X_OPTIONS}.
#		The v must go before the variable because in the ALPHA|SGI
#		case statement you can't put the v after the 20 blocking factor.
#
#		calling signature:
#			set_tar_options "MachineType"

set_tar_options ()
{
	case "${1}" in
		ALPHA* | alpha* | OSF1* | SGI* | sgi6* | IRIX* )

			TAR_C_OPTIONS="cf" #same as default

			TAR_X_OPTIONS="xbpf 20"
			#x - extract
			#b - use blocking factor
			#p - use original tarred file protection properties
			#f - use given filename
			#20 - portable blocking factor
			;;
		HP* | hp* )

			TAR_C_OPTIONS="cOfA"
			#c - create new archive
			#O - write a pre-POSIX format archive
			#f - use given filename
			#A - suppress warning messages for ACL entries

			TAR_X_OPTIONS="xpfA"
			#x - extract
			#p - use original tarred file protection properties
			#f - use given filename
			#A - suppress warning messages for ACL entries	
			;;

		* )
			TAR_C_OPTIONS="cf"
			#c - create new archive
			#f - use given filename

			TAR_X_OPTIONS="xpf"
			#x - extract
			#p - use original tarred file protection propeties
			#f - use given filename
			;;
	esac
}

# INSERT fn.inetd_entries
#---------- inetd.entries -- $Revision: 1.1 $ ------------
#	Set up the entries needed for inetd or xinetd by
#	defining a bunch of mini-functions the calling
#	scripts can reference.
#
#	There are 2 major types (inetd or xinetd).
#	New entries need to be added to both sections.
#
#-------------------------------------------------------------------------------
# inetd entries - NOWAIT will be filled in based on machine type later
# when the appropriate function is called.  For most machines,
# NOWAIT will be "nowait" but for Linux, it will be "nowait.300".

bpcd_i ()
{
	bpcd_i_entry="bpcd	stream	tcp	${NOWAIT}	root	/usr/openv/netbackup/bin/bpcd bpcd"
}

bpjava_msvc_i ()
{
	bpjava_msvc_i_entry="bpjava-msvc	stream	tcp	${NOWAIT}	root	/usr/openv/netbackup/bin/bpjava-msvc bpjava-msvc -transient"
}

vnetd_i ()
{
	vnetd_i_entry="vnetd	stream	tcp	${NOWAIT}	root	/usr/openv/bin/vnetd vnetd"
}

vopied_i ()
{
	vopied_i_entry="vopied	stream	tcp	${NOWAIT}	root	/usr/openv/bin/vopied vopied"
}

#-------------------------------------------------------------------------------
# xinetd entries - GROUP_LINE will be filled in based on machine type
# later when the appropriate function is called.  For most machines,
# GROUP_LINE will be nothing, but for MACINTOSH, it will be replaced
# by "groups = yes"

bpcd_x ()
{
	bpcd_x_entry="# Service bpcd for NetBackup
service bpcd
{
        socket_type             = stream
        protocol                = tcp
        wait                    = no
        user                    = root
        server                  = /usr/openv/netbackup/bin/bpcd
        disable                 = no
        ${GROUP_LINE}
}"
}

bpjava_msvc_x ()
{
	bpjava_msvc_x_entry="# Service bpjava-msvc for NetBackup
service bpjava-msvc
{
        socket_type             = stream
        protocol                = tcp
        wait                    = no
        user                    = root
        server                  = /usr/openv/netbackup/bin/bpjava-msvc
        server_args             = -transient
        disable                 = no
        ${GROUP_LINE}
}"
}

vnetd_x ()
{
	vnetd_x_entry="# Service vnetd for NetBackup
service vnetd
{
        socket_type             = stream
        protocol                = tcp
        wait                    = no
        user                    = root
        server                  = /usr/openv/bin/vnetd
        disable                 = no
        ${GROUP_LINE}
}"
}

vopied_x ()
{
	vopied_x_entry="# Service vopied for NetBackup
service vopied
{
        socket_type             = stream
        protocol                = tcp
        wait                    = no
        user                    = root
        server                  = /usr/openv/bin/vopied
        disable                 = no
        ${GROUP_LINE}
}"
}

# do_netinfo_service - add a service entry for MacOSX*
#
# These OSes use the NetInfo utilities to administrate services rather than
# the /etc/services file.
#
# do_netinfo_service <file_name>
#
#	<file_name>	file containing a single services line.

do_netinfo_service () {
	service_file="${1}"
	service_name=`awk '{print $1}' < "${service_file}"`
	service_port=`awk '{print $2}' < "${service_file}" | \
	    awk -F/ '{print $1}'`
	current_protocol=`niutil -readprop . /services/"${service_name}" \
	    protocol 2> /dev/null`
	current_port=`niutil -readprop . /services/"${service_name}" port 2> \
	    /dev/null`
	if [ \( "${current_protocol}" != 'tcp' \) -o \
	     \( "${current_port}" = '' \) ] ; then
		niutil -create . /services/"${service_name}" > /dev/null 2>&1
		niutil -createprop . /services/"${service_name}" protocol \
		    tcp > /dev/null 2>&1
		niutil -createprop . /services/"${service_name}" port \
		    "${service_port}" > /dev/null 2>&1
		current_protocol=`niutil -readprop . \
		    /services/"${service_name}" protocol 2> /dev/null`
		current_port=`niutil -readprop . /services/"${service_name}" \
		    port 2> /dev/null`
	fi
	if [ \( "${current_protocol}" != 'tcp' \) -o \
	     \( "${current_port}" = '' \) ] ; then
		return 1
	fi
	return 0
}
# -----------------------------------------------------------------------------
# Add files to xinetd.d directory for NetBackup services if xinetd is used.
#
ck_xinetd () {

	entry=$1

	#  If using xinetd and the machine is a MACINTOSH, it must be the
	#  MacOSX10.2 client.  If there isn't a groups entry in the file,
	#  remove it and we'll create a new one from scratch.

	if [ -f /etc/xinetd.d/${entry} -a "${MACHINE}" = "MACINTOSH" ] ; then
		tr -d ' \011' < /etc/xinetd.d/${entry} | grep "^groups=yes" >/dev/null 2>&1
		if [ $? -eq 1 ] ; then
			rm -f /etc/xinetd.d/${entry}
		fi
	fi
	if [ ! -f /etc/xinetd.d/${entry} ] ; then
		if [ "${MACHINE}" = "MACINTOSH" ] ; then
			GROUP_LINE="groups                  = yes"
		else
			GROUP_LINE=""
		fi
		case "${entry}" in
			bpcd)
				bpcd_x
				${ECHO} "${bpcd_x_entry}" >> /etc/xinetd.d/${entry}
				;;
			bpjava-msvc)
				bpjava_msvc_x
				${ECHO} "${bpjava_msvc_x_entry}" >> /etc/xinetd.d/${entry}
				;;
			vnetd)
				vnetd_x
				${ECHO} "${vnetd_x_entry}" >> /etc/xinetd.d/${entry}
				;;
			vopied)
				vopied_x
				${ECHO} "${vopied_x_entry}" >> /etc/xinetd.d/${entry}
				;;
		esac
	fi
}
# -----------------------------------------------------------------------------

#################### MAIN ####################

set_tar_options ${MACHINE} # note no v

case "${MACHINE}" in
	HP*)
		DSO_SUFFIX='sl'
		;;
	*)
		DSO_SUFFIX='so'
		;;
esac

if [ -d /tmp/bp/bin ] ; then
	/bin/mkdir /usr/openv 2> /dev/null
	/bin/chmod 755 /usr/openv
	mkdir /usr/openv/netbackup 2> /dev/null
	chmod 755 /usr/openv/netbackup
	mkdir /usr/openv/netbackup/logs 2> /dev/null
	chmod 755 /usr/openv/netbackup/logs
	mkdir /usr/openv/netbackup/logs/user_ops 2> /dev/null
	chmod 777 /usr/openv/netbackup/logs/user_ops
	mkdir /usr/openv/netbackup/bin 2> /dev/null
	chmod 755 /usr/openv/netbackup/bin
	mkdir /usr/openv/bin 2> /dev/null
	chmod 755 /usr/openv/bin
	mkdir /usr/openv/lib 2> /dev/null
	chmod 755 /usr/openv/lib
	mkdir /usr/openv/share 2> /dev/null
	chmod 755 /usr/openv/share
	mkdir /usr/openv/var 2> /dev/null
	chmod 755 /usr/openv/var
	mkdir /usr/openv/var/auth 2> /dev/null
	chmod 755 /usr/openv/var/auth
	mkdir /usr/openv/var/vxss 2> /dev/null
	chmod 755 /usr/openv/var/vxss
	mkdir /usr/openv/var/vxss/credentials 2> /dev/null
	chmod 755 /usr/openv/var/vxss/credentials
	DoJava=No
	if [ -d /tmp/bp/java ] ; then
		DoJava=Yes
		if [ ${MACHINE} = "RS6000" ] ; then
			Version=`uname -v`
			if [ $Version -lt 5 ] ; then
				DoJava=No
			fi
		fi
		if [ $DoJava = Yes ] ; then
			mkdir /usr/openv/java 2> /dev/null
			chmod 755 /usr/openv/java
		else
			rm -rf /tmp/bp/java
		fi
	fi
	if [ -d /tmp/bp/bin/driver ] ; then
		mkdir /usr/openv/netbackup/bin/driver 2> /dev/null
		chmod 755 /usr/openv/netbackup/bin/driver
	fi

	# On an rs6000 machine, free unused shared objects from kernel and library
	# memory, so we don't bomb replacing libraries in /usr/openv/lib.

	if [ "${MACHINE}" = "RS6000" ] ; then
		/usr/sbin/slibclean
	fi

	/bin/cp -R /tmp/bp/bin /usr/openv/netbackup

	# move firewall files

	/bin/mv /usr/openv/netbackup/bin/vnetd /usr/openv/bin/

	# move Third Party Copyright files

	/bin/mv /usr/openv/netbackup/bin/tpc_*.txt /usr/openv/share

	# move authentication files

	/bin/mv /usr/openv/netbackup/bin/template.methods.txt /usr/openv/var/auth/
	/bin/mv /usr/openv/netbackup/bin/template.methods_allow.txt /usr/openv/var/auth/
	/bin/mv /usr/openv/netbackup/bin/template.methods_deny.txt /usr/openv/var/auth/
	/bin/mv /usr/openv/netbackup/bin/template.names_allow.txt /usr/openv/var/auth/
	/bin/mv /usr/openv/netbackup/bin/template.names_deny.txt /usr/openv/var/auth/

	/bin/mv /usr/openv/netbackup/bin/vopied /usr/openv/bin/
	/bin/mv /usr/openv/netbackup/bin/vauth_util /usr/openv/bin/
	/bin/mv /usr/openv/netbackup/bin/vauth_test /usr/openv/bin/
	/bin/mv /usr/openv/netbackup/bin/vauth_testd /usr/openv/bin/
	/bin/mv /usr/openv/netbackup/bin/vopie_util /usr/openv/bin/
	/bin/mv /usr/openv/netbackup/bin/libvnoauth.${DSO_SUFFIX}_new /usr/openv/lib/
	if [ -f /usr/openv/netbackup/bin/libvnoauth64.${DSO_SUFFIX}_new ] ; then
		/bin/mv /usr/openv/netbackup/bin/libvnoauth64.${DSO_SUFFIX}_new \
			/usr/openv/lib/
	fi

	/bin/mv /usr/openv/netbackup/bin/libvopie.${DSO_SUFFIX}_new /usr/openv/lib/
	if [ -f /usr/openv/netbackup/bin/libvopie64.${DSO_SUFFIX}_new ] ; then
		/bin/mv /usr/openv/netbackup/bin/libvopie64.${DSO_SUFFIX}_new \
			/usr/openv/lib/
	fi

	/bin/mv /usr/openv/netbackup/bin/libVcvcomb.${DSO_SUFFIX} /usr/openv/lib/
	if [ -f /usr/openv/netbackup/bin/libVcvcomb64.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libVcvcomb64.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi

	/bin/mv /usr/openv/netbackup/bin/libVnbconf.${DSO_SUFFIX}_new /usr/openv/lib/
	if [ -f /usr/openv/netbackup/bin/libVnbconf64.${DSO_SUFFIX}_new ] ; then
		/bin/mv /usr/openv/netbackup/bin/libVnbconf64.${DSO_SUFFIX}_new \
			/usr/openv/lib/
	fi

	/bin/mv /usr/openv/netbackup/bin/libVnbatST.${DSO_SUFFIX}_new /usr/openv/lib/
	if [ -f /usr/openv/netbackup/bin/libVnbatST64.${DSO_SUFFIX}_new ] ; then
		/bin/mv /usr/openv/netbackup/bin/libVnbatST64.${DSO_SUFFIX}_new \
			/usr/openv/lib/
	fi

	/bin/mv /usr/openv/netbackup/bin/libVnbat.${DSO_SUFFIX} /usr/openv/lib/
	if [ -f /usr/openv/netbackup/bin/libVnbat64.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libVnbat64.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi

	/bin/mv /usr/openv/netbackup/bin/libVxSS_helper.${DSO_SUFFIX}_new /usr/openv/lib/
	if [ -f /usr/openv/netbackup/bin/libVxSS_helper64.${DSO_SUFFIX}_new ] ; then
		/bin/mv /usr/openv/netbackup/bin/libVxSS_helper64.${DSO_SUFFIX}_new \
			/usr/openv/lib/
	fi
	if [ -f /usr/openv/netbackup/bin/libxbsa.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libxbsa.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi
	if [ -f /usr/openv/netbackup/bin/libxbsa64.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libxbsa64.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi
	if [ -f /usr/openv/netbackup/bin/libubs.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libubs.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi
	if [ -f /usr/openv/netbackup/bin/libnbbeclass.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libnbbeclass.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi
	if [ -f /usr/openv/netbackup/bin/libnbbestdutl.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libnbbestdutl.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi
	if [ -f /usr/openv/netbackup/bin/libnbbedscomn.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libnbbedscomn.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi
	if [ -f /usr/openv/netbackup/bin/libdbsb.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libdbsb.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi
	if [ -f /usr/openv/netbackup/bin/libVmangle.${DSO_SUFFIX} ] ; then
		/bin/mv /usr/openv/netbackup/bin/libVmangle.${DSO_SUFFIX} \
			/usr/openv/lib/
	fi

	# Move the *_new files to their real locations.

	chmod 500 /usr/openv/netbackup/bin/move_libs
	/usr/openv/netbackup/bin/move_libs
	rm -f /usr/openv/netbackup/bin/move_libs

	mkdir /usr/openv/netbackup/help 2> /dev/null
	chmod 755 /usr/openv/netbackup/help
	mkdir /usr/openv/netbackup/help/bp 2> /dev/null
	chmod 755 /usr/openv/netbackup/help/bp
	/bin/cp /tmp/bp/help/bp/* /usr/openv/netbackup/help/bp

	if [ "$MACHINE" = 'MACINTOSH' ] ; then
		# ignore X help and config files since not real xbp
		rm -f /tmp/bp/bin/xbp
	fi
	if [ -f /tmp/bp/bin/xbp ] ; then
		if [ ! -d /usr/lib ] ; then
			mkdir /usr/lib
			chmod 755 /usr/lib
		fi
		if [ ! -d /usr/lib/X11 ] ; then
			mkdir /usr/lib/X11
			chmod 755 /usr/lib/X11
		fi
		for file in /usr/lib/X11/XKeysymDB /usr/openv/netbackup/bin/XNB \
			/usr/openv/netbackup/bin/XNB.dt
		do
			if [ ! -f ${file} -a -f /tmp/bp/`basename ${file}` ] ; then
				/bin/cp /tmp/bp/`basename ${file}` ${file}
				chmod 444 ${file}
			fi
		done
		if [ -d /tmp/bp/help/xbp ] ; then
			mkdir /usr/openv/netbackup/help/xbp 2> /dev/null
			chmod 755 /usr/openv/netbackup/help/xbp
			/bin/cp /tmp/bp/help/xbp/* /usr/openv/netbackup/help/xbp
		fi
	fi
	# Merge in the new servers into bp.conf if told to.
	if [ -f /usr/openv/netbackup/bp.conf ] ; then
		if [ "$1"eave_bp.conf != "-Leave_bp.conf" ] ; then
			/bin/cat /usr/openv/netbackup/bp.conf |
			while read FILE1
			do
				# If $FILE1 is not blank.
				if [ X"${FILE1}" != X ] ; then
					# Check if first word in the line is SERVER
					ID1=`${ECHO} $FILE1 | awk -F= '{print $1}'`
					SERVER1=`${ECHO} $FILE1 | awk -F= '{print $2}'`
					# Might need to trim white space from awk output
					ID1=`${ECHO} ${ID1}`
					SERVER1=`${ECHO} ${SERVER1}`
					if [ "${ID1}" = "SERVER" ] ; then
						# Go through the current servers and
						# check to see if it already exists.
						/bin/rm -f /tmp/bp/server_found
						/bin/cat /tmp/bp/bp_servers |
						while read FILE2
						do
							SERVER2=`${ECHO} $FILE2 | awk -F= '{print $2}'`
							SERVER2=`${ECHO} ${SERVER2}`
							if [ "${SERVER1}" = "${SERVER2}" ] ; then
								/bin/cp /dev/null /tmp/bp/server_found
							fi
						done

						# Add the server to a tmp file.
						if [ ! -f /tmp/bp/server_found ] ; then
							${ECHO} "${FILE1}" >>/tmp/bp/bp.conf.add
						fi
					elif [ "${ID1}" = "CLIENT_NAME" ] ; then
						/bin/cp /dev/null /tmp/bp/cli_name_found
						${ECHO} "${FILE1}" >>/tmp/bp/bp.conf.add
					else
						# Not a SERVER entry, copy to bp.conf.add.
						${ECHO} "${FILE1}" >>/tmp/bp/bp.conf.add
					fi
				else
					${ECHO} "${FILE1}" >>/tmp/bp/bp.conf.add
				fi
			done

			# Copy the new /tmp/bp/bp_servers to bp.conf
			/bin/cp /tmp/bp/bp_servers /usr/openv/netbackup/bp.conf

			# Add any of the old entries from bp.conf that
			# aren't in the new /tmp/bp/bp_servers file.
			if [ -f /tmp/bp/bp.conf.add ] ; then
				/bin/cat /tmp/bp/bp.conf.add >>/usr/openv/netbackup/bp.conf
			fi

			if [ ! -f /tmp/bp/cli_name_found ] ; then
				/bin/cat /tmp/bp/bp_client_name >>/usr/openv/netbackup/bp.conf
			fi
		fi
	else
		/bin/cp /tmp/bp/bp_servers /usr/openv/netbackup/bp.conf
		/bin/cat /tmp/bp/bp_client_name >>/usr/openv/netbackup/bp.conf
	fi
fi

# Create dir for dbext software
/bin/mkdir /usr/openv/netbackup/dbext 2> /dev/null
/bin/chmod 755 /usr/openv/netbackup/dbext
# Create dir for NB addon libraries
/bin/mkdir /usr/openv/lib 2> /dev/null
/bin/chmod 755 /usr/openv/lib

# Set the permissions on the files.
chmod 644 /usr/openv/netbackup/bp.conf
chmod 555 /usr/openv/netbackup/bin/bp
chmod 500 /usr/openv/netbackup/bin/bpcd
chmod 500 /usr/openv/netbackup/bin/bpdynamicclient
chmod 555 /usr/openv/netbackup/bin/bpjava-msvc
chmod 555 /usr/openv/netbackup/bin/bpjava-usvc
chmod 555 /usr/openv/netbackup/bin/bpbkar
chmod 555 /usr/openv/netbackup/bin/bpbackup
chmod 555 /usr/openv/netbackup/bin/bprestore
chmod 555 /usr/openv/netbackup/bin/bplist
chmod 555 /usr/openv/netbackup/bin/bpclimagelist
chmod 555 /usr/openv/netbackup/bin/bpclntcmd
chmod 444 /usr/openv/netbackup/bin/bprd_service 2> /dev/null
chmod 444 /usr/openv/netbackup/bin/bpcd_service 2> /dev/null
chmod 444 /usr/openv/netbackup/bin/vnetd_service 2> /dev/null
chmod 444 /usr/openv/netbackup/bin/vopied_service 2> /dev/null
chmod 444 /usr/openv/netbackup/bin/bpjava_service 2> /dev/null
chmod 444 /usr/openv/netbackup/bin/bpcd_inetd.cnf 2> /dev/null
chmod 444 /usr/openv/netbackup/bin/vnetd_inetd.cnf 2> /dev/null
chmod 444 /usr/openv/netbackup/bin/vopied_inetd.cnf 2> /dev/null
chmod 444 /usr/openv/netbackup/bin/bpjava_inetd.cnf 2> /dev/null
chmod 444 /usr/openv/netbackup/bin/version
chmod 500 /usr/openv/netbackup/bin/vxss_db_paths
chmod 444 /usr/openv/netbackup/help/bp/*
chmod 555 /usr/openv/lib/libV* 2> /dev/null
if [ -f /usr/openv/bin/vnetd ] ; then
	chmod 500 /usr/openv/bin/vnetd
fi
if [ -f /usr/openv/bin/vopied ] ; then
	chmod 500 /usr/openv/bin/vopied
fi
if [ -f /usr/openv/bin/vauth_util ] ; then
	chmod 500 /usr/openv/bin/vauth_util
fi
if [ -f /usr/openv/bin/vauth_test ] ; then
	chmod 555 /usr/openv/bin/vauth_test
fi
if [ -f /usr/openv/bin/vauth_testd ] ; then
	chmod 500 /usr/openv/bin/vauth_testd
fi
if [ -f /usr/openv/bin/vopie_util ] ; then
	chmod 500 /usr/openv/bin/vopie_util
fi
if [ -f /usr/openv/netbackup/bin/bpnbat ] ; then
	chmod 555 /usr/openv/netbackup/bin/bpnbat
fi
if [ -f /usr/openv/netbackup/bin/bpnbat_helper ] ; then
	chmod 555 /usr/openv/netbackup/bin/bpnbat_helper
fi
if [ -f /usr/openv/lib/libvnoauth.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libvnoauth.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libvnoauth64.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libvnoauth64.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libvopie.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libvopie.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libvopie64.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libvopie64.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libxbsa.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libxbsa.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libxbsa64.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libxbsa64.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libubs.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libubs.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libnbbeclass.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libnbbeclass.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libnbbestdutl.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libnbbestdutl.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libnbbedscomn.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libnbbedscomn.${DSO_SUFFIX}
fi
if [ -f /usr/openv/lib/libdbsb.${DSO_SUFFIX} ] ; then
	chmod 555 /usr/openv/lib/libdbsb.${DSO_SUFFIX}
fi
if [ -f /usr/openv/netbackup/bin/merge_auth_templates ] ; then
	chmod 500 /usr/openv/netbackup/bin/merge_auth_templates
fi
if [ -f /usr/openv/var/auth/template.methods.txt ] ; then
	chmod 444 /usr/openv/var/auth/template.methods.txt
fi
if [ -f /usr/openv/var/auth/template.methods_allow.txt ] ; then
	chmod 444 /usr/openv/var/auth/template.methods_allow.txt
fi
if [ -f /usr/openv/var/auth/template.methods_deny.txt ] ; then
	chmod 444 /usr/openv/var/auth/template.methods_deny.txt
fi
if [ -f /usr/openv/var/auth/template.names_allow.txt ] ; then
	chmod 444 /usr/openv/var/auth/template.names_allow.txt
fi
if [ -f /usr/openv/var/auth/template.names_deny.txt ] ; then
	chmod 444 /usr/openv/var/auth/template.names_deny.txt
fi
if [ -x /usr/openv/netbackup/bin/merge_auth_templates ] ; then
	/usr/openv/netbackup/bin/merge_auth_templates
fi

if [ -f /usr/openv/netbackup/bin/rbak ] ; then
	chmod 555 /usr/openv/netbackup/bin/rbak
else
	chmod 555 /usr/openv/netbackup/bin/tar
fi

if [ -f /usr/openv/netbackup/bin/mtfrd ] ; then
	chmod 555 /usr/openv/netbackup/bin/mtfrd
fi

if [ -f /usr/openv/netbackup/bin/bpfis ] ; then
	chmod 555 /usr/openv/netbackup/bin/bpfis
fi

if [ -f /usr/openv/netbackup/bin/bphdb ] ; then
	chmod 555 /usr/openv/netbackup/bin/bphdb
fi

if [ -f /usr/openv/netbackup/bin/bpmount ] ; then
	chmod 555 /usr/openv/netbackup/bin/bpmount
fi

if [ -f /usr/openv/netbackup/bin/bptpcinfo ] ; then
	chmod 555 /usr/openv/netbackup/bin/bptpcinfo
fi

if [ -f /usr/openv/netbackup/bin/xbp ] ; then
	chmod 555 /usr/openv/netbackup/bin/xbp
fi

if [ -d /usr/openv/netbackup/help/xbp ] ; then
	chmod 444 /usr/openv/netbackup/help/xbp/* >/dev/null 2>&1
fi

for file in snapcachelist snapctl snapctl_x snaplist snapoff snapon snapstat
do
	if [ -f /usr/openv/netbackup/bin/driver/${file} ] ; then
		chmod 544 /usr/openv/netbackup/bin/driver/${file}
	fi
done
if [ -f /usr/openv/netbackup/bin/driver/snapctl.conf ] ; then
	chmod 444 /usr/openv/netbackup/bin/driver/snapctl.conf
fi

cd /usr/openv/netbackup/bin
/bin/rm -rf bparchive
ln -s bpbackup bparchive

/bin/rm -rf /tmp/bp_services_updated

if [ "$MACHINE" != 'MACINTOSH' ] ; then

bprd=`grep '^bprd' /etc/services`
bpcd=`grep '^bpcd' /etc/services`
vnetd=`grep '^vnetd' /etc/services`
vopied=`grep '^vopied' /etc/services`
bpjava=`grep '^bpjava-msvc' /etc/services`

if [ X"${bprd}" = X -a X"${bpcd}" = X -a X"${vopied}" = X -a X"${bpjava}" = X -a X"${vnetd}" = X ] ; then
	# We need to add all of them.
	${ECHO} " #" >>/etc/services
	${ECHO} " # OpenV*NetBackup services" >>/etc/services
	${ECHO} " #" >>/etc/services
fi
# Add bprd to the /etc/services file if necessary.
if [ "${bprd}" = "" ] ; then
	cat /usr/openv/netbackup/bin/bprd_service >>/etc/services
	/bin/cp /dev/null /tmp/bp_services_updated
fi

# Add bpcd to the /etc/services file if necessary.
if [ "${bpcd}" = "" ] ; then
	cat /usr/openv/netbackup/bin/bpcd_service >>/etc/services
	/bin/cp /dev/null /tmp/bp_services_updated
fi

# Add vnetd to the /etc/services file if necessary.
if [ "${vnetd}" = "" ] ; then
	cat /usr/openv/netbackup/bin/vnetd_service >>/etc/services
	/bin/cp /dev/null /tmp/bp_services_updated
fi

# Add vopied to the /etc/services file if necessary.
if [ "${vopied}" = "" ] ; then
	cat /usr/openv/netbackup/bin/vopied_service >>/etc/services
	/bin/cp /dev/null /tmp/bp_services_updated
fi

# Add bpjava-msvc to the /etc/services file if necessary.
if [ "${bpjava}" = "" ] ; then
	cat /usr/openv/netbackup/bin/bpjava_service >>/etc/services
	/bin/cp /dev/null /tmp/bp_services_updated
fi

else # "$MACHINE" = 'MACINTOSH'

	# MacOSX* uses the NetInfo utilities
	# rather than /etc/services to configure network services.

	do_netinfo_service /usr/openv/netbackup/bin/bpcd_service
	do_netinfo_service /usr/openv/netbackup/bin/bprd_service
	do_netinfo_service /usr/openv/netbackup/bin/vnetd_service
	do_netinfo_service /usr/openv/netbackup/bin/vopied_service
	do_netinfo_service /usr/openv/netbackup/bin/bpjava_service

fi

# Determine location of inetd.conf file.
INETD_CONF=/etc/inetd.conf

if [ "${MACHINE}" = "SGI" ] ; then
	if [ -f /usr/etc/inetd.conf ] ; then
		INETD_CONF=/usr/etc/inetd.conf
	fi
fi

# Add our daemons to the /etc/xinetd.d directory if necessary
# and call reload on xinetd to have the system reread xinetd.conf.
if [ -d /etc/xinetd.d -a -f /etc/xinetd.conf ] ; then
	ck_xinetd bpcd
	ck_xinetd vnetd
	ck_xinetd vopied
	ck_xinetd bpjava-msvc

	#  MacOSX10.2 has xinetd but doesn't have the reload option.
	#  We'll SIGHUP xinetd for MacOSX10.2 later.
	if [ "${MACHINE}" != "MACINTOSH" ] ; then
		${ECHO} "Reloading xinetd configuration"
		/etc/rc.d/init.d/xinetd reload
		#equivalent to kill -USR2 xinetd_pid or kill -s 12 xinetd_pid
	fi

# Since daemons were just configured in xinetd, remove any from INETD_CONF
# that may be left from a previous installation.

	if [ -f ${INETD_CONF} ] ; then
		ED_SCR=/tmp/ed_cmd$$
		bpcd_inetd=`grep '^bpcd' $INETD_CONF`
		if [ "${bpcd_inetd}" != "" ] ; then
			${ECHO} "Removing bpcd from $INETD_CONF, it is in xinetd."
			${ECHO} "g/^bpcd/d" > $ED_SCR
			${ECHO} "w" >> $ED_SCR
			${ECHO} "q" >> $ED_SCR
			ed -s $INETD_CONF < $ED_SCR
			/bin/cp /dev/null /tmp/bp_services_updated
		fi
		vnetd_inetd=`grep '^vnetd' $INETD_CONF`
		if [ "${vnetd_inetd}" != "" ] ; then
			${ECHO} "Removing vnetd from $INETD_CONF, it is in xinetd."
			${ECHO} "g/^vnetd/d" > $ED_SCR
			${ECHO} "w" >> $ED_SCR
			${ECHO} "q" >> $ED_SCR
			ed -s $INETD_CONF < $ED_SCR
			/bin/cp /dev/null /tmp/bp_services_updated
		fi
		vopied_inetd=`grep '^vopied' $INETD_CONF`
		if [ "${vopied_inetd}" != "" ] ; then
			${ECHO} "Removing vopied from $INETD_CONF, it is in xinetd."
			${ECHO} "g/^vopied/d" > $ED_SCR
			${ECHO} "w" >> $ED_SCR
			${ECHO} "q" >> $ED_SCR
			ed -s $INETD_CONF < $ED_SCR
			/bin/cp /dev/null /tmp/bp_services_updated
		fi
		bpjava_inetd=`grep '^bpjava-msvc' $INETD_CONF`
		if [ "${bpjava_inetd}" != "" ] ; then
			${ECHO} "Removing bpjava-msvc from $INETD_CONF, it is in xinetd."
			${ECHO} "g/^bpjava-msvc/d" > $ED_SCR
			${ECHO} "w" >> $ED_SCR
			${ECHO} "q" >> $ED_SCR
			ed -s $INETD_CONF < $ED_SCR
			/bin/cp /dev/null /tmp/bp_services_updated
		fi
		rm -f ${ED_SCR}
	fi
else
	# Use nowait.300 ONLY for Linux clients using inetd.
	# xinetd doesn't need it.
	if [ "${MACHINE}" = "Linux" ] ; then
		cat /usr/openv/netbackup/bin/bpcd_inetd.cnf | sed -e 's/nowait/nowait.300/' > /usr/openv/netbackup/bin/bpcd_inetd.cnf
		cat /usr/openv/netbackup/bin/vnetd_inetd.cnf | sed -e 's/nowait/nowait.300/' > /usr/openv/netbackup/bin/vnetd_inetd.cnf
		cat /usr/openv/netbackup/bin/vopied_inetd.cnf | sed -e 's/nowait/nowait.300/' > /usr/openv/netbackup/bin/vopied_inetd.cnf
		cat /usr/openv/netbackup/bin/bpjava_inetd.cnf | sed -e 's/nowait/nowait.300/' > /usr/openv/netbackup/bin/bpjava_inetd.cnf
	fi

	#  Only update client's inetd.conf file if entries are not already there.
	bpcd_inetd=`grep '^bpcd' $INETD_CONF`
	if [ "${bpcd_inetd}" = "" ] ; then
		cat /usr/openv/netbackup/bin/bpcd_inetd.cnf >>$INETD_CONF
		/bin/cp /dev/null /tmp/bp_services_updated
	fi

	vnetd_inetd=`grep '^vnetd' $INETD_CONF`
	if [ "${vnetd_inetd}" = "" ] ; then
		cat /usr/openv/netbackup/bin/vnetd_inetd.cnf >>$INETD_CONF
		/bin/cp /dev/null /tmp/bp_services_updated
	fi

	vopied_inetd=`grep '^vopied' $INETD_CONF`
	if [ "${vopied_inetd}" = "" ] ; then
		cat /usr/openv/netbackup/bin/vopied_inetd.cnf >>$INETD_CONF
		/bin/cp /dev/null /tmp/bp_services_updated
	fi

	bpjava_inetd=`grep '^bpjava-msvc' $INETD_CONF`
	if [ "${bpjava_inetd}" = "" ] ; then
		cat /usr/openv/netbackup/bin/bpjava_inetd.cnf >>$INETD_CONF
		/bin/cp /dev/null /tmp/bp_services_updated
	fi
fi

if [ "${MACHINE}" = "RS6000" ] ; then
	# We need to call inetimp if we update /etc/services or inetd.conf.
	if [ -f /tmp/bp_services_updated ] ; then
		oslevel=`oslevel | cut -c1-3 | sed -e 's/\.//'`
		if [ ${oslevel} -lt 51 -a -f /usr/bin/inetimp ] ; then
			/usr/bin/inetimp
		fi
		/bin/rm -rf /tmp/bp_services_updated
	fi
fi

# Remove the services files.
HEREIAM=`pwd`
cd /usr/openv/netbackup/bin
/bin/rm -f bprd_service bpcd_service vopied_service bpjava_service bpcd_inetd.cnf vopied_inetd.cnf bpjava_inetd.cnf vnetd_service vnetd_inetd.cnf

# We need to find the process number for inetd or xinetd so that
# we can send a SIGHUP to it and it will reread the (x)inetd.conf file.
if [ "${MACHINE}" = "Linux" -o "${MACHINE}" = "INTEL" ] ; then
	inetd=`/bin/ps aux | grep inetd | grep -v grep | grep -v osinetd|awk '{print $2}'`
elif [ "${MACHINE}" = "MACINTOSH" ] ; then
	inetd=`/bin/ps -aux | grep inetd | grep -v grep | grep -v osinetd | awk '{print $2}'`
else
	inetd=`/bin/ps -ef|grep inetd|grep -v grep|grep -v osinetd|awk '{print $2}'`
fi
if [ "${MACHINE}" = "MACINTOSH" ] ; then
	/bin/kill -s HUP $inetd
else
	/bin/kill -HUP $inetd
fi


if [ -d /tmp/bp/java -a "$DoJava" = "Yes" ] ; then
	cd /usr/openv

	#  Clean up the java directory.  There are a couple of things to keep
	#  so create a temp directory to save those in.
	java_tmp_dir=/usr/openv/NB_java_tmp.$$
	if [ ! -d ${java_tmp_dir} ] ; then
		mkdir ${java_tmp_dir}
	else
		rm -rf ${java_tmp_dir}/*
	fi
	for conf in auth.conf nbj.conf
	do
		if [ -f /usr/openv/java/${conf} ] ; then
			mv /usr/openv/java/${conf} ${java_tmp_dir}/${conf}.bak
		fi
	done

	#  The next section of code tries to figure out if VSM, GDM
	#  or a locale package has been installed.  If so, save off
	#  the pieces installed by those packages.
	for file in HSMApplet.properties allHSM.jar migfb migsa \
		.gdmConf archiver.jar crimson-1.1.jar gdm.conf \
		gdmDashboard.jar gdmSA idl.jar jacorb.jar jaxp-1.1.jar \
		SchedUI_ja_JP.jar VxHelp_ja_JP.jar \
		SchedUI_zh_CN.jar VxHelp_zh_CN.jar
	do
		if [ -f /usr/openv/java/${file} ] ; then
			mv /usr/openv/java/${file} ${java_tmp_dir}
		fi
	done
	for loc in ja zh
	do
		if [ -f /usr/openv/java/vrts/i18n_${loc}.properties ] ; then
			if [ ! -d ${java_tmp_dir}/vrts ] ; then
				mkdir ${java_tmp_dir}/vrts
			fi
			mv /usr/openv/java/vrts/i18n_${loc}.properties ${java_tmp_dir}/vrts
		fi
		for dir in help helpDC ; do
			if [ -d /usr/openv/java/vrts/nbu/${dir}/${loc} ] ; then
				if [ ! -d ${java_tmp_dir}/vrts/nbu/${dir} ] ; then
					mkdir -p ${java_tmp_dir}/vrts/nbu/${dir}
				fi
				mv /usr/openv/java/vrts/nbu/${dir}/${loc} ${java_tmp_dir}/vrts/nbu/${dir}
			fi
		done
	done
	for dir in hsm gdm ; do
		if [ -d /usr/openv/java/vrts/${dir} ] ; then
			if [ ! -d ${java_tmp_dir}/vrts ] ; then
				mkdir ${java_tmp_dir}/vrts
			fi
			mv /usr/openv/java/vrts/${dir} ${java_tmp_dir}/vrts
		fi
	done

	#  Now remove the existing java directory and if anything actually
	#  got saved, move the temp directory back to /usr/openv/java.
	if [ -d /usr/openv/java ] ; then
		rm -rf /usr/openv/java 2>/dev/null
	fi
	rmdir ${java_tmp_dir} 2>/dev/null
	if [ $? != 0 ] ; then
		mv ${java_tmp_dir} /usr/openv/java
		chgrp -R bin /usr/openv/java
	fi

	zcat /tmp/bp/NB-Java.tar.Z | /bin/tar ${TAR_X_OPTIONS} - >/dev/null
	rm -f NB-Java.tar.Z
	if [ ! -d /usr/openv/java/logs ] ; then
		mkdir /usr/openv/java/logs
		chmod 777 /usr/openv/java/logs
	fi

	#  note only the backup copy of auth.conf gets copied back
	#  not nbj.conf
	if [ -f /usr/openv/java/auth.conf.bak ] ; then
		rm -f /usr/openv/java/auth.conf
		mv /usr/openv/java/auth.conf.bak /usr/openv/java/auth.conf
	fi

	cd /usr/openv/java
	zcat /tmp/bp/java/*.tar.Z | /bin/tar ${TAR_X_OPTIONS} - >/dev/null
	rm -f *.tar.Z
	rm -f /usr/openv/netbackup/bin/jnbSA /usr/openv/netbackup/bin/jbpSA
	ln -s /usr/openv/java/jbpSA /usr/openv/netbackup/bin/jbpSA
	ln -s /usr/openv/java/jnbSA /usr/openv/netbackup/bin/jnbSA
	rm -f /usr/openv/lib/libSigScheduleJNI.${DSO_SUFFIX}
	mv /tmp/bp/java/libSigScheduleJNI.${DSO_SUFFIX} /usr/openv/lib
	chmod 555 /usr/openv/lib/libSigScheduleJNI.${DSO_SUFFIX}
	# get the modified version of nbj.conf shipped over from the server
	cp /tmp/bp/java/nbj.conf /usr/openv/java/
fi

cd ${HEREIAM}

# Remove the /tmp/bp files.
if [ -d /tmp/bp/bin ] ; then
	/bin/rm -rf /tmp/bp
fi

# On some HP platforms the umask is not set correctly and bpinst
# will create directories/files with 777 permissions in /tmp/bp/
# "cp -R" will perserve those permissions when they are copied out
# to the live directories.  This is to ensure that netbackup/bin
# directory has the correct permissions.
chmod 755 /usr/openv/netbackup/bin
