#!/bin/sh
# $Header: install_client_files.sh,v 1.7.16.2 2004/01/15 16:44:30 $
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2004 VERITAS Software Corporation, All Rights Reserved $ *
#***************************************************************************
#
# install software on NetBackup client(s)
#
#	install_client_files ftp client_name|ALL user_name
#	install_client_files rsh client_name|ALL

# 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


# Install a client in a given a class
client_install() {
	CLIENT=${3}

	if [ ! -d ${CLIENT_BIN_DIR}/${1}/${2} ] ; then
		${ECHO} "Cannot install client $CLIENT
invalid hardware type '$1' and/or operating system '$2'
"
		return 1
	fi

	if [ ! -f ${CLIENT_BIN_DIR}/${1}/${2}/version ] ; then
		${ECHO} "Cannot install client $CLIENT - binaries are not on the server
for hardware type '$1' and operating system '$2'
"
		return 2
	fi

	${ECHO} "       $CLIENT ..."
	${ECHO} "Client $CLIENT -- $1 hardware running $2"

	if [ "${method}" = "ftp" ] ; then
		${ECHO} "Installing NetBackup software on ${CLIENT} as user ${user_name}"
		cd ${CLIENT_BIN_DIR}/${1}/${2}

		./ftp_to_client ${CLIENT} ${user_name}
		ret=$?
	else
		${ECHO} "Installing NetBackup software on ${CLIENT}"

		${CLIENT_BIN_DIR}/${1}/${2}/install_client ${CLIENT}
		ret=$?
	fi
	if [ "$ret" -ne 0 ] ; then
		${ECHO} "       $CLIENT install failed
"
	else
		${ECHO} "       $CLIENT install complete
"
	fi
	return $ret
}


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

BP_DIR=/usr/openv/netbackup
BP_BIN=${BP_DIR}/bin
CLIENT_BIN_DIR=${BP_DIR}/client

if [ -f /usr/openv/netbackup/version ] ; then
	HARDWARE=`head -1 /usr/openv/netbackup/version | cut -f2 -d" "`
else
	${ECHO} "/usr/openv/netbackup/version not found"
	exit 1
fi

# Based on the HARDWARE type, determine the location of the
# tr command. Prepend /usr/5bin to PATH on Suns to get
# System V behaviour from echo.

TR=/bin/tr

case "$HARDWARE" in
	RS6000 | SOLARIS | ALPHA | SGI)
		;;
	HP* | LINUX*)
		TR=/usr/bin/tr
		;;
	*)	${ECHO} "
Unknown hardware type: $HARDWARE
"
		exit 1
		;;
esac

# Make sure we're on an active node or a standalone machine
# before we allow the user to push to the clients.

${BP_BIN}/cluster/cluster_active > /dev/null 2>&1
if [ $? -ne 0 ] ; then
	${ECHO} "
This machine appears to be an inactive member of a cluster.  Pushing
software to clients must occur on the active member of the cluster.
"
	exit 0
fi

${BP_BIN}/admincmd/bpauthorize -test_admin >/dev/null
status=$?
if [ ${status} -ne 0 ] ; then
	${ECHO} ""
	${ECHO} "You do not have the proper authorization to perform"
	${ECHO} "this task.  Please resolve and try again."
	exit ${status}
fi

# Determine the system's hostname.  Use hostname if available and strip
# away any domain information.

if type hostname | grep 'not found' >/dev/null
then
	MASTER=`uname -n | sed 's/\..*$//'`
else
	MASTER=`hostname | sed 's/\..*$//'`
fi

# Get the domainname into domain without a leading '.' (if there is one).

DOMAIN=`domainname | sed 's/^\.//'`
if [ X"${DOMAIN}" = X ] ; then
	# No domain. Set it to something that will not be matched.
	DOMAIN=ZZZZZZZZ
fi

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

doexit=0
# Look for 0 or more spaces and tabs followed by SERVER.
SRVR=`egrep "^[${SPACE}${TAB}]*SERVER[${SPACE}${TAB}]*=" ${BP_DIR}/bp.conf`
if [ "${SRVR}" = "" ] ; then
	${ECHO} "Missing SERVER in /usr/openv/netbackup/bp.conf"
	doexit=1
fi

# Read the bprd entry from /etc/services.
BPRDSERVICE=`grep "bprd" /etc/services`
if [ "${BPRDSERVICE}" = "" ] ; then
	${ECHO} "You need an entry for bprd in /etc/services"
	doexit=1
fi

# Read the bpjava-msvc entry from /etc/services.
BPJAVAMSERVICE=`grep "bpjava-msvc" /etc/services`
if [ "${BPJAVAMSERVICE}" = "" ] ; then
	${ECHO} "You need an entry for bpjava-msvc in /etc/services"
	doexit=1
fi

# Read the bpcd entry from /etc/services.
BPCDSERVICE=`grep "bpcd" /etc/services`
if [ "${BPCDSERVICE}" = "" ] ; then
	${ECHO} "You need an entry for bpcd in /etc/services"
	doexit=1
fi

# Read the vnetd entry from /etc/services.
VNETDSERVICE=`grep "vnetd" /etc/services`
if [ "${VNETDSERVICE}" = "" ] ; then
	${ECHO} "You need an entry for vnetd in /etc/services"
	doexit=1
fi

# Read the vopied entry from /etc/services.
VOPIEDSERVICE=`grep "vopied" /etc/services`
if [ "${VOPIEDSERVICE}" = "" ] ; then
	${ECHO} "You need an entry for vopied in /etc/services"
	doexit=1
fi

if [ $doexit = 1 ] ; then
	exit 1
fi

# Verify that the right number of arguments were given
if [ "$#" -gt 0 ] ; then
	usage_error=0
	if [ "$#" -eq 3 ] ; then
		method=$1
		if [ "${method}" != "ftp" ] ; then
			usage_error=1
		fi
		client_name=$2
		user_name=$3
	elif [ "$#" -eq 2 ] ; then
		method=$1
		if [ "${method}" != "rsh" ] ; then
			usage_error=1
		fi
		client_name=$2
	else
		usage_error=1
	fi
	if [ $usage_error = 1 ] ; then
		${ECHO} "
usage:  $0 ftp client_name|ALL user_name
or:     $0 rsh client_name|ALL
"
		exit 22
	fi
else
	method=rsh
	client_name=ALL
fi

if [ "$client_name" = "ALL" ] ; then
	if [ -f /tmp/netbackup.install_client_files.ALL ] ; then
		${ECHO} "
Someone else is already installing client software on all clients or
install_client_files was interrupted during a previous install of all clients.
Delete /tmp/netbackup.install_client_files.ALL if a previous
install_client_files of all clients was interrupted.
"
		exit 1
	else
		/bin/cp /dev/null /tmp/netbackup.install_client_files.ALL
	fi
fi

/bin/rm -f /tmp/client_found_in_config_db.${client_name}.$$

/usr/openv/netbackup/bin/admincmd/bpplclients -allunique -noheader |
while read class_hw class_os client
do
	if [ "$client_name" = "ALL" -o "$client_name" = "$client" ] ; then
		${ECHO} 0 > /tmp/client_found_in_config_db.${client_name}.$$

		# Check to see if the client is the master (the local machine).
		# Use bpclntcmd so we aren't fooled by virtual names.

		${BP_BIN}/bpclntcmd -is_local_host ${client} > /dev/null 2>&1
		if [ $? -eq 0 ] ; then
			# Don't install the server with files from itself.
			# The rcps in install_client will fail.

			${ECHO} "
The client files are already on server ${client}.
"
			if [ -f /usr/openv/netbackup/client/${class_hw}/${class_os}/tar ] ; then
				/bin/cp ${CLIENT_BIN_DIR}/${class_hw}/${class_os}/tar ${BP_BIN}/tar
				/bin/cp ${CLIENT_BIN_DIR}/${class_hw}/${class_os}/bpbkar ${BP_BIN}/bpbkar
			fi

		else
			client_install ${class_hw} ${class_os} ${client}
			if [ $? != 0 ] ; then
				${ECHO} 1 > /tmp/client_found_in_config_db.${client_name}.$$
			fi
		fi
	fi
done

if [ ! -f /tmp/client_found_in_config_db.${client_name}.$$ ] ; then
	if [ "$client_name" = "ALL" ] ; then
		${ECHO} "
There are no clients specified in the configuration database.
No client files were installed.
"
	else
		${ECHO} "
Client $client_name was not found in the configuration database.
The client files were not installed on $client_name.
"
	fi
	exit 1
fi

worked=`cat /tmp/client_found_in_config_db.${client_name}.$$`

if [ "$client_name" = "ALL" ] ; then
	rm -f /tmp/netbackup.install_client_files.ALL
fi
/bin/rm -f /tmp/client_found_in_config_db.${client_name}.$$

exit $worked
