#!/bin/sh
# $Header: update_javaclients.sh,v 1.8 2000/01/27 19:24:07 raa Stab $
#bcpyrght
#***************************************************************************
#* Copyright 1993 - 1999 VERITAS Software Corporation, All Rights Reserved *
#***************************************************************************
#ecpyrght
#
# update software on NetBackup clients
#
#	update_javaclients [hardware] [operating_system]

USAGE="usage: $0 [hardware_type] [operating_system]\n
		-ClientList filename"
TMPDIR=${TMPDIR:-/tmp}
BP_DIR=/usr/openv/netbackup
BP_BIN=${BP_DIR}/bin
JAVA_DIR=/usr/openv/java
CLIENT_BIN_DIR=/usr/openv/netbackup/client
ClientList=""
NumMatch=0

LC_MESSAGES="C"
export LC_MESSAGES

# Announce possible clients at the wrong level
#
# Input $1 = the "found" file
#	$2 = the  hardware type for the message

announce_possibles () {

	Entries=`wc -l $1 | sed s/' '/''/g | cut -d '/' -f1`
	if [ $Entries -gt 4 ] ; then
		head -4 $1
		cat $1 >> $LOGFILE
		echo "  ...		There are $Entries $2 clients to check."
	else
		cat $1 | tee -a $LOGFILE
	fi

}

# Confirm an action with yes as the default
#
#	confirm prompt_string
#	Returns 0 for yes and 1 for no.

confirm () {
	Valid=1
	while [ $Valid = 1 ]
	do
		echo "$1 (y/n) [y]\c "
		read ans
		: ${ans:=y}
		case $ans in
			N*|n*)	return 1 ;;
			Y*|y*)	return 0 ;;
			*)	echo "You have entered an invalid option."
		esac
	done
}

# Get a numeric value from the user.
#
#	get_number prompt_string low_value high_value default
#
#	Entered value is returned in the VALUE variable

get_number() {

	echo "
$1
Must be in the range $2 - $3 (default: $4): \c"
	valid=0
	until [ $valid != 0 ]
	do
		read VALUE
		VALUE=${VALUE:=$4}
		expr $VALUE + 0 > /dev/null 2>/dev/null
		x=$?
		if [ $x -ne 2 -a $VALUE -ge $2 -a $VALUE -le $3 ] ; then
			valid=1
		else
			echo "Must be in the range $2 - $3, try again: \c"
		fi
	done
}

# error return
#
error_return() {
	/bin/rm -f ${TMPDIR}/${CLIENT}

	cat ${Output} >> ${LOGFILE}
	rm -f ${Output}
}
# Update a client in a given class
#

client_update() {

	ret=0
	Output=${TMPDIR}/javaclient_update.$$.${JobIndex}
	exec > $Output 2>&1

	CLIENT=${3}

	if [ "${CLIENT}" = "${MASTER}" -o "${CLIENT}" = "${MASTER}.${DOMAIN}" ] ; then
		# Don't try to update the server with files from itself.
		echo " $CLIENT is your master server and will not be updated."
		error_return
		return 1
	fi

	XSOLARIS_JRE_TAR=Solaris_X86_JRE_117B.tar.Z
	SOLARIS_JRE_TAR=Solaris_JRE_117B.tar.Z
	HP1020_JRE_TAR=hp1020_jre116.tar.Z
	HP1100_JRE_TAR=hp110_jre116.tar.Z
	JAVA_FILE=NB-Java.tar.Z

	# pick the correct JRE for Java client $1/$2
	# and set up USE_APP_INITIALIZER

	USE_APP_INITIALIZER=""
	if [ $1 = "Solaris" ] ; then
		if [ $2 = "Solaris_x86_2.6" -o $2 = "Solaris_x86_7" -o $2 = "Solaris_x86_8" ] ; then
			JRE_FILE=${XSOLARIS_JRE_TAR}
		elif [ $2 = "Solaris2.5" -o $2 = "Solaris2.6" -o $2 = "Solaris7" -o $2 = "Solaris8" ] ; then
			JRE_FILE=${SOLARIS_JRE_TAR}
		else
			echo " ${CLIENT} is $1/$2 platform and is not a supported NB-Java client."
			error_return
			return 2
		fi
	elif [ $1 = "HP9000-800" ] ; then
		if [ $2 = "HP11.00" ] ; then
			JRE_FILE=${HP1100_JRE_TAR}
			USE_APP_INITIALIZER="-Dvrts.nbu.admin.USE_APP_INITIALIZER=false"
		elif [ $2 = "HP-UX10.20" ] ; then
			JRE_FILE=${HP1020_JRE_TAR}
		else	
			echo " ${CLIENT} is $1/$2 platform and is not a supported NB-Java client."
			error_return
			return 2
		fi
	else
		echo " ${CLIENT} is $1/$2 platform and is not a supported NB-Java client."
		error_return
		return 2
	fi


	echo "	$CLIENT $1/$2..."

	/bin/rm -f ${TMPDIR}/${CLIENT}
	/bin/echo "CLIENT_NAME = ${CLIENT}" > ${TMPDIR}/${CLIENT}

	if [ ! -d /tmp/JAVA.$$.${JobIndex} ] ; then
		mkdir -p /tmp/JAVA.$$.${JobIndex}
	fi
	if [ -f /tmp/JAVA.$$.${JobIndex}/nbj.conf ] ; then
		rm -f /tmp/JAVA.$$.${JobIndex}/nbj.conf
	fi

	${BP_BIN}/bpinst -MISC -Z ${JAVA_FILE} -src /usr/openv -dest /usr/openv -host ${CLIENT} -d 
	Status=$?
	if [ $Status != 0 ] ; then
		error_return
		return 3
	fi
	# create the extract script
	cat <<eof >/tmp/JAVA.$$.${JobIndex}/extract.sh 
#!/bin/sh
cd /usr/openv
zcat NB-Java.tar.Z | /bin/tar xvf - 
if [ ! -d /usr/openv/java/logs ] ; then
	mkdir /usr/openv/java/logs
fi
rm -f /usr/openv/NB-Java.tar.Z 
rm -f /usr/openv/java/auth.conf
eof
	#
	${BP_BIN}/bpinst -MISC -X -Z extract.sh -src /tmp/JAVA.$$.${JobIndex} -dest /usr/openv/netbackup/bin -host ${CLIENT} -d 
	Status=$?
	if [ $Status != 0 ] ; then
		error_return
		return 3
	fi

	${BP_BIN}/bpinst -MISC -Z ${JRE_FILE} -src /usr/openv/java -dest /usr/openv/java -host ${CLIENT} -d 
	Status=$?
	if [ $Status != 0 ] ; then
		error_return
		return 3
	fi
	# create the extract script
	cat <<eof >/tmp/JAVA.$$.${JobIndex}/extract.sh 
#!/bin/sh
cd /usr/openv/java
zcat ${JRE_FILE} | /bin/tar xvf - 
rm -f /usr/openv/java/${JRE_FILE} 
rm -f ${BP_BIN}/jnbSA ${BP_BIN}/jbpSA
if [ \`uname -m\` != "i86pc" ] ; then
	ln -s ${JAVA_DIR}/jnbSA ${BP_BIN}/jnbSA
fi
ln -s ${JAVA_DIR}/jbpSA ${BP_BIN}/jbpSA
mv ${JAVA_DIR}/jre/lib/font.properties ${JAVA_DIR}/jre/lib/font.properties.bak
mv ${JAVA_DIR}/font.properties ${JAVA_DIR}/jre/lib/
mkdir -p -m 755 ${JAVA_DIR}/java/awt
mv ${JAVA_DIR}/Finalize2.class ${JAVA_DIR}/java/awt/
mv ${JAVA_DIR}/Graphics*.class ${JAVA_DIR}/java/awt/
mv ${JAVA_DIR}/SecondaryFinalizer.class ${JAVA_DIR}/java/awt/
mv ${JAVA_DIR}/Component*.class ${JAVA_DIR}/java/awt/
eof
	#
	${BP_BIN}/bpinst -MISC -X -Z extract.sh -src /tmp/JAVA.$$.${JobIndex} -dest /usr/openv/netbackup/bin -host ${CLIENT} -d 

	Status=$?
	if [ $Status != 0 ] ; then
		error_return
		return 3
	fi

	WHEREIAM=`pwd`
	# copy and modify nbj.conf for $CLIENT
	if [ -f /usr/openv/java/nbj.conf ] ; then
		touch /tmp/JAVA.$$.${JobIndex}/nbj.conf
		grep -v CLIENT_HOST  /usr/openv/java/nbj.conf | grep -v USE_APP_INITIALIZER > /tmp/JAVA.$$.${JobIndex}/nbj.conf
		echo "USE_APP_INITIALIZER=${USE_APP_INITIALIZER}" >> /tmp/JAVA.$$.${JobIndex}/nbj.con
		echo "CLIENT_HOST=$CLIENT" >> /tmp/JAVA.$$.${JobIndex}/nbj.conf
	
		${BP_BIN}/bpinst -MISC -Z nbj.conf -src /tmp/JAVA.$$.${JobIndex} -dest /usr/openv/java -host ${CLIENT} -d 
		Status=$?
		if [ $Status != 0 ] ; then
			error_return
			return 3
		fi
	else
		echo " Server is missing /usr/openv/java/nbj.conf " 
		echo " You must create $CLIENT /usr/openv/java/nbj.conf manually" 
	fi
	echo

	cd ${WHEREIAM}
	rm -f /tmp/JAVA.$$.${JobIndex}/nbj.conf >/dev/null -f ${TMPDIR}/${CLIENT}

	cat ${Output} >> ${LOGFILE}
	rm ${Output}
	return 0
}

# Leave through here to clean up all files

quit () {

	rm -rf /tmp/JAVA.$$.*
	if [ $1 = 2 ] ; then
		echo "The complete list of NetBackup-Java clients can be found in
${CLIENTS}."
		rm -f ${CLIENTS}.*
	elif [ $1 = 4 ] ; then  # No clients to upgrade
		rm -f ${CLIENTS}* ${LOGFILE}
	else
		/bin/rm -f ${CLIENTS}*
	fi
	exit $1
}

## MAIN Starts Here
##

DATE_TIME=`/bin/date +%m-%d-%H%M`.$$
CLIENTS=${TMPDIR}/JAVA_CLIENT_LIST.${DATE_TIME}
echo "0" > ${CLIENTS}.count

# Verify that the right number of arguments were given
if [ $# = 0 ] ; then
	hardware="ALL"
	os_name="ALL"
elif [ $# = 2 ] ; then
	hardware=$1
	os_name=$2
	if [ $1 = "-ClientList" ] ; then
		ClientList=$2
		hardware="ALL"
		os_name="ALL"
	fi
else
	echo $USAGE
	exit 1
fi

echo "
Installing NetBackup-Java GUI applications to NetBackup-Java supported clients."

# Verify that if hardware/os were specified, they're valid
if [ ! "$hardware" = "ALL" -a ! -d "$CLIENT_BIN_DIR/$hardware/$os_name" ] ; then
	echo "
Invalid hardware type \"$hardware\" and/or operating system \"$os_name\"
$USAGE
"
	exit 1
fi

if [ -f /usr/openv/netbackup/version ] ; then
	HARDWARE=`head -1 /usr/openv/netbackup/version | cut -f2 -d" "`
else
	HARDWARE=NONE
fi

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

INETD_CONF=/etc/inetd.conf; export INETD_CONF
TR=/bin/tr

HASYP=TRUE
case "$HARDWARE" in
	DGUX | RS6000 | SOLARIS | ALPHA | PYRAMID)
		;;
	SGI)
		if [ -f /usr/etc/inetd.conf ] ; then
			INETD_CONF=/usr/etc/inetd.conf
		fi ;;
	SUN4)
		PATH=/usr/5bin:$PATH ;;
	HP*)
		TR=/usr/bin/tr ;;
	NCR)
		if [ -f /usr/etc/inetd.conf ] ; then
			INETD_CONF=/usr/etc/inetd.conf
		elif [ -f /etc/inet/inetd.conf ] ; then
			INETD_CONF=/etc/inet/inetd.conf
		fi
		TR=/usr/bin/tr ;;
	SEQUENT*)
		TR=/usr/bin/tr
		HASYP=FALSE
		;;
	*)
		echo "
/usr/openv/netbackup/version not found or corrupted.
Unknown hardware type: \"$HARDWARE\"
"
		exit 1 ;;
esac

TAB=`/bin/echo ' ' | $TR ' ' '\011'`
SPACE=' '

# 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).
if [ $HASYP = TRUE ] ; then
	DOMAIN=`domainname | sed 's/^\.//'`
fi
if [ X"${DOMAIN}" = X ] ; then
	# No domain. Set it to something that will not be matched.
	DOMAIN=ZZZZZZZZ
fi

# Create a log file for output of update_javaclient
LOGFILE=${TMPDIR}/update_javaclients.${DATE_TIME}
touch ${LOGFILE}

if [ "${ClientList}" = "" ] ; then
	/usr/openv/netbackup/bin/admincmd/bpclclients -allunique -noheader >${CLIENTS} 2>/dev/null
	if [ ! "$hardware" = "ALL" ] ; then
		grep $hardware ${CLIENTS} | grep $os_name > ${CLIENTS}.tmp
		mv ${CLIENTS}.tmp ${CLIENTS}
	fi

	HP_FOUND=${TMPDIR}/HP_FOUND.${DATE_TIME}
	SOLARIS_FOUND=${TMPDIR}/SOLARIS_FOUND.${DATE_TIME}
	NON_JAVA_FOUND=${TMPDIR}/NON_JAVA_FOUND.${DATE_TIME}

	touch ${CLIENTS}.tmp
	/bin/cat ${CLIENTS} |
	while read class_hw class_os client
	do
		if [ "${client}" = "${MASTER}" -o "${client}" = "${MASTER}.${DOMAIN}" ] ; then
			# Don't try to update the server with files from itself.
			echo " $client is your master server and will not be updated.
">$LOGFILE
			continue
		fi
		if [ "$class_hw" = "Solaris" -o "$class_hw" = "Sun4" ] ; then
			case "${class_os}" in
				SunOS4 | Solaris | Solaris2 )
						if [ "$hardware" != "ALL" ] ; then
							echo "  $class_os  \t$client" >> ${NON_JAVA_FOUND}
						fi
						;;

				*)
						echo "  $class_os   \t$client" >> ${SOLARIS_FOUND}
						;;
			esac
		elif [ "$class_hw" = "HP9000-800" -o "$class_hw" = "HP9000-700" ] ; then
			echo "  $class_os   \t$client" >> ${HP_FOUND}
		else 
			# doesn't get added to CLIENTS.tmp
			# only report this client if ALL wasn't selected
			# ALL assumes you wanted only supported platforms updated.
			if [ "$hardware" != "ALL" ] ; then
				echo "  $class_os  \t$client" >> ${NON_JAVA_FOUND}
			fi
			continue
		fi
		echo $class_hw $class_os $client >> ${CLIENTS}.tmp
	done
	mv ${CLIENTS}.tmp ${CLIENTS}

	if [ -f ${NON_JAVA_FOUND} ] ; then
		echo "
The following clients with the OS type listed
do not support NetBackup-Java clients."
		announce_possibles ${NON_JAVA_FOUND} Java
		rm -f $LOGFILE  # filled by announce_possibles ()
		echo "
The full list of suspect clients can be found in ${NON_JAVA_FOUND}."
	
	fi

	if [ -f ${SOLARIS_FOUND} -o -f ${HP_FOUND} ] ; then
		echo "
The following clients with the OS type listed may need to be
changed in the NetBackup class configuration so the appropriate
NetBackup-Java GUI application software for the OS type can be installed.
  OS Type       Client name"
		if [ -f ${SOLARIS_FOUND} ] ; then
			announce_possibles ${SOLARIS_FOUND} Solaris
		fi
		if [ -f ${HP_FOUND} ] ; then
			announce_possibles ${HP_FOUND} HP9000
		fi
		rm -f ${SOLARIS_FOUND} ${HP_FOUND} > /dev/null
		echo "
The full list of suspect NetBackup-Java clients can be found in $LOGFILE."
		if confirm "\nDo you want to continue?"
		then
			echo "Continuing with the NetBackup-Java client update.
	(Deleting $LOGFILE)"
			rm -f $LOGFILE
		else
			echo "
Aborting the NetBackup-Java client update.
"
			quit 2
		fi
	fi
else
	cp ${ClientList} ${CLIENTS}
fi

if [ ! -s ${CLIENTS} ] ; then
	echo "
There are no client NetBackup-Java components to upgrade."
	quit 4
else
	sort ${CLIENTS} > ${CLIENTS}.sorted
	mv ${CLIENTS}.sorted ${CLIENTS}
	NUM_CL=`wc -l ${CLIENTS} | sed s/' '/''/g | cut -d '/' -f1`
	if [ $NUM_CL -gt 15 ] ; then
		Default=15
	else
		Default=$NUM_CL
	fi
	if [ $NUM_CL -gt 30 ] ; then
		Max_Num=30
	else
		Max_Num=$NUM_CL
	fi
	echo "
There are $NUM_CL NetBackup-Java clients to upgrade."
	if [ $NUM_CL != 1 ] ; then
		get_number "Enter the number of simultaneous updates you wish to take place" 1 $Max_Num $Default
	else
		VALUE=1
	fi
	MinTime=`expr \( $NUM_CL + 1 \) \/ \( ${VALUE} \* 2 \)`
	MaxTime=`expr \( \( 2 \* $NUM_CL \) + 1 \) \/ ${VALUE}`
	if [ $MinTime -lt 1 ] ; then
		MinTime=1
	fi
	if [ $MaxTime -le $MinTime ] ; then
		MaxTime=`expr $MinTime + 1`
	fi
	echo "
The upgrade will likely take $MinTime to $MaxTime minutes."
	if confirm "Do you want to upgrade clients now?"
	then
		echo "Please be patient."
	else
		echo "
You will need to upgrade clients later with:
update_javaclients -ClientList filename
"
		quit 2
	fi
fi

echo
MostJobs=$VALUE
index=0
while [ $index -lt $MostJobs ]
do
	echo "Available" > ${CLIENTS}.${index}
	index=`expr $index + 1`
done

/bin/cat ${CLIENTS} |
while read class_hw class_os client
do
	index=0
	while [ $index -lt $MostJobs ]
	do
		Available=`cat ${CLIENTS}.${index}`
		if [ "$Available" = "Available" ] ; then
			echo "In Use" > ${CLIENTS}.${index}
			JobIndex=$index
			break
		fi
		index=`expr $index + 1`
		if [ $index -ge $MostJobs ] ; then
			sleep 1
			index=0
		fi
	done

	if [ "$hardware" = "ALL" -o \
	  \( "$hardware" = "$class_hw" -a "$os_name" = "$class_os" \) ] ; then
		(
		echo "Updating client ${client} -- ${class_hw} hardware running ${class_os}"
		client_update ${class_hw} ${class_os} ${client} ${JobIndex} & \
		PiD=$!
		wait $PiD
		Ustat=$?
		if [ $Ustat = 1 ] ; then
			echo "	${client}: - no update needed, see ${LOGFILE}"
			NumMatch=`cat ${CLIENTS}.count`
			expr $NumMatch + 1 > ${CLIENTS}.count
		elif [ $Ustat = 2 ] ; then
			echo "	${client}: not supported, see ${LOGFILE}"
		elif [ $Ustat = 3 ] ; then
			echo "	${client}: update failed, see ${LOGFILE}"
		elif [ $Ustat = 0 ] ; then
			echo "	${client}: update complete"
		else
			echo "	${client}: update failed, see ${LOGFILE}"
		fi
		echo "Available" > ${CLIENTS}.${JobIndex}
		)&
	fi
done

# Wait for all jobs to be finished

index=0
while [ $index -lt $MostJobs ]
do
	if [ -f ${CLIENTS}.${index} ] ; then
		Available=`cat ${CLIENTS}.${index}`
		if [ "$Available" = "Available" ] ; then
			rm -f ${CLIENTS}.${index}
		else
			sleep 1
			continue  # wait for this one to be gone.
		fi
	fi
	index=`expr $index + 1`
done

echo "
See ${LOGFILE} for NetBackup-Java client distribution details"
# Clean up the /tmp files.
if [ -d /tmp/JAVA ] ; then
	rm -rf /tmp/JAVA
fi
if [ -f ${CLIENTS} ] ; then
	rm -f ${CLIENTS}
fi

quit 0

