#!/bin/sh
# $Header: install_dbext.sh,v 1.34 2000/08/21 19:56:11 fmr Stab $
#bcpyrght
#***************************************************************************
#* $VRTScprght: Copyright 1993 - 2000 VERITAS Software Corporation, All Rights Reserved $ *
#***************************************************************************
#ecpyrght

#
#  Places to possibly update when adding new products and/or platforms:
#
#  function get_pkg_info (includes 32/64 bit code)
#  function untar_image
#  function get_tar_images (includes 32/64 code)
#  function get_platform (includes 32/64 code)
#  function check_version (includes 32/64 code)
#  variable All_Extension_List in MAIN
#

USAGE ()
{
	echo ""
	echo "USAGE:"
	echo "   $0"
	exit 22
}

trap trapped 1 2 3 15

trapped ()
{
	rm -f ${TMPDIR}/db_pkg_list.$$
	rm -f ${TMPDIR}/db_prod_list.$$
	rm -f ${TMPDIR}/install_dbext.$$.status
	rm -f ${TMPDIR}/tmpobk.$$
	exit 1
}

#
#  To have a nice complete trace, most of the program is executed in a
#  subshell.  In order to preserve any exit status from within the subshell,
#  we need to put it into something visible when the subshell goes away.
#

wrap_up ()
{
	rm -f ${TMPDIR}/install_dbext.$$.status
	echo ${1} >> ${TMPDIR}/install_dbext.$$.status
	exit
}

#
#  Define the Prompt function.
#

case "`echo 'x\c'`" in
	'x\c')
		Prompt()
		{
		echo -n "$*"
		}
		;;
	x)
		Prompt()
		{
		echo "$*\c"
		}
		;;
	*)
		Prompt()
		{
		echo -n "$*"
		}
		;;
esac

#
#  This function provides a mapping from the first parameter (the
#  product name that's part of db extensions directory and tar.Z
#  file names) for example OEBU, to something more readable such
#  as Oracle.  Also make available the name of the product's
#  final installation script, if there is one.  Have fake names
#  for 32 bit products so that text displayed to the user comes
#  out right.
#

get_pkg_info ()
{
	real_product=""
	run_script=""
	case "${1}" in
		BACKTRACK)
			real_product="SQL-BackTrack"
			run_script="${NB_PATH}/bin/install_obsi"
			;;
		DB2)
			real_product="${1}"
			run_script="${NB_PATH}/bin/install_db2"
			;;
		INFORMIX)
			real_product="Informix"
			run_script="${NB_PATH}/bin/install_infxbsa"
			;;
		INFORMIX_32)
			real_product="Informix  32-bit"
			run_script="${NB_PATH}/bin/install_infxbsa"
			;;
		LOTUS)
			real_product="Lotus Notes"
			;;
		OEBU)
			real_product="Oracle"
			;;
		OEBU_32)
			real_product="Oracle  32-bit"
			;;
		PROXY)
			real_product="Oracle on UNIX Advanced BLI Agent"
			run_script="${NB_PATH}/bin/install_oracle"
			;;
		SAP)
			real_product="${1}"
			;;
		SYBASE)
			real_product="Sybase"
			run_script="${NB_PATH}/bin/install_sybackup"
			;;
		TERADATA)
			real_product="Teradata"
			run_script="${NB_PATH}/bin/install_tdata"
			;;
		*)
			real_product="Unknown"
			echo ""
			echo "${1} is an unknown database extension"
			echo ""
			wrap_up 1
			;;
	esac
}

#
#  This function takes a string as input and echos it out
#  as a question with a prompt for an answer.  Yes is
#  the default.
#

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

#
#  This function takes a string as input and echos it out
#  as a question with a prompt for an answer.  No is
#  the default.
#

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

#
#  Get one or more numeric values from the user.  The first parameter
#  is a string that is echoed out as a question with a prompt for
#  an answer.  The second parameter is the minimum value that
#  will be accepted and the third parameter is the highest value
#  that will be accepted.  Answers starting with x/X or q/Q are
#  valid.  If the answer is a number, we check to make sure it's
#  in the range established by the input parameters.  If the answer
#  is anything else or it wasn't in the range, the user is prompted
#  again.  The variable VALUE contains the final answer(s).
#

get_number()
{
	Prompt "${1}"
	lower=${2}
	upper=${3}
	valid=0
	list=""
	until [ ${valid} != 0 ]
	do
		read VALUE
		echo "${VALUE}" >> ${run_Trace_File}
		VALUE=${VALUE:="Value"}
		set ${VALUE}
		num_values=$#
		count=0
		for val in ${VALUE} ; do
			count=`expr ${count} + 1`
			case "${val}" in
				x* | X*)
					list="${list} x"
					valid=1
					if [ ${count} -ne ${num_values} ] ; then
						echo ""
						echo "x has been entered.  The rest of the input line has been ignored."
					fi
					break
					;;
				q* | Q*)
					list="${list} q"
					valid=1
					if [ ${count} -ne ${num_values} ] ; then
						echo ""
						echo "q has been entered.  The rest of the input line has been ignored."
					fi
					break
					;;
				      *)
					echo ${val} | egrep -e \(\^[0-9]\+\$\) > /dev/null
					if [ $? -ne 0 ] ; then
						echo ""
						echo "ERROR:  ${val} must be in the range ${lower} - ${upper}, try again."
						if [ ${count} -ne ${num_values} ] ; then
							echo "ERROR:  The rest of the input line has been ignored."
						fi
						echo ""
						valid=0
						Prompt "Choose an option: "
						break
					else
						if [ ${val} -ge ${lower} -a ${val} -le ${upper} ] ; then
							list="${list} ${val}"
							valid=1
						else
							echo ""
							echo "ERROR:  ${val} must be in the range ${lower} - ${upper}, try again."
							if [ ${count} -ne ${num_values} ] ; then
								echo "ERROR:  The rest of the input line has been ignored."
							fi
							echo ""
							valid=0
							Prompt "Choose an option: "
							break
						fi
					fi
					;;
			esac
		done
	done
	VALUE="${list}"
	echo
}

#
#  This function expects a tar.Z file name as input.  It extracts the
#  the platform type from the tar.Z file name and based on that info,
#  it executes the tar command with the proper options.  Use basename
#  in case TARPATH has "." in it somewhere.
#

untar_image()
{
	tar_file=${1}

	echo ""
	echo "Extracting files from ${tar_file} ..."
	base=`basename ${tar_file}`
	PLAT=`echo ${base} | sed -e 's/\.tar.Z$//' | cut -f2- -d'.'`

	case "${PLAT}" in
		alpha)
			tar_opts="xvbpf 20"
			;;
		dgux | ncr | sequent4.2 | solaris*)
			tar_opts="-xvpof"
			;;
		hp* | rs6000*)
			tar_opts="-xvpf"
			;;
		sgi6*)
			tar_opts="-xvpbf 20"
			;;
		*)
			echo "unsupported platform"
			return 1
			;;
	esac

	#
	#  On some platforms tar notices if libobk is busy and aborts.  This
	#  code makes an attempt to see if libobk is busy and bails before
	#  tar is executed.  However on some platforms, tar does not notice
	#  and therefore this check doesn't notice either.  We just go ahead
	#  and install in that case.
	#
	#  In the proxy case, install_oracle will fail trying to move
	#  bli_libobk to libobk so check now for that too.
	#

	if [ "${product}" = "OEBU" -o "${product}" = "OEBU_32" -o \
	     "${product}" = "PROXY" ] ; then
		obkfile=`zcat ${tar_file} | /bin/tar tf - | grep libobk`
		dirobk="/`dirname ${obkfile}`"
		obk=`basename ${obkfile} | sed -e 's/^bli_//'`
		if [ -f "${dirobk}/${obk}" ] ; then
			stopnow=0
			rm -f ${TMPDIR}/tmpobk.$$
			cp ${dirobk}/${obk} ${TMPDIR}/tmpobk.$$
			out=`cp ${TMPDIR}/tmpobk.$$ ${dirobk}/${obk} 2>&1`
			rm -f ${TMPDIR}/tmpobk.$$
			echo ${out} | grep -i "busy" > /dev/null
			if [ $? -eq 0 ] ; then
				stopnow=1
			else
				echo ${out} | grep -i "running" > /dev/null
				if [ $? -eq 0 ] ; then
					stopnow=1
				fi
			fi
			if [ ${stopnow} -eq 1 ] ; then
				get_pkg_info ${product}
				echo ""
				echo "Oracle cannot be running when NetBackup for ${real_product}"
				echo "is installed.  Take Oracle down and redo the NetBackup"
				echo "for ${real_product} installation for this client."
				echo ""
				return 1
			fi
		fi
	fi

	(
	cd /
	zcat ${tar_file} | /bin/tar ${tar_opts} -
	)

	#
	#  Check if zcat/tar had a non-zero exit status and pass it back.
	#

	if [ $? -ne 0 ] ; then
		return 1
	fi
}

#
#  If an extension was passed in on the command line, just use that one.
#  Otherwise, check all possible extensions.  Determine the right tar.Z file
#  name for this product and platform.  This is needed because at least one
#  platform (hp11.00) is available in two configurations (hp11.00 and hp11_64).
#  If we find some tar.Z files and matching version files, this function is
#  finished.  If we don't and we're in batch mode, exit normally (this assumes
#  that bpinst has shipped the files over successfully else it would have aborted
#  prior to this script and gets around the fact that hp11.00 won't be installed
#  on a hp11_64 machine and vice versa and yet is not really a failure).  Otherwise,
#  ask if the user wants to continue.  If not, the program exits.  If so, they
#  are asked to provide the correct directory location to find the tar.Z files
#  and version files.  This continues until they give a directory where we can
#  find tar.Z files and version files or they choose not to continue.  Variable
#  product_list contains the list of extensions for which we found files.
#

get_tar_images ()
{
	TARPATH=`pwd`
	done=0
	notdir=0
	product_list=""

	if [ "${cmd_ext}" = "" ] ; then
		LIST=${All_Extension_List}
	else
		LIST=${cmd_ext}
	fi

	for prod in ${LIST}; do
		platform=${PLATFORM}

		#
		#  If we're on a 64 bit machine and we have 32 bit products and we're
		#  not in batch mode, set a special tar image and version file name.
		#  We will fall through and also set the native 64 bit names so that
		#  a user can choose to install 64 bit and 32 bit on the same machine.
		#  However, in batch mode (via update_dbclients), we can't ask so
		#  only install the native 64 bit.
		#

		if [ ${is_64_bit} -eq 1 ] ; then
			if [ "${prod}" = "OEBU" -o "${prod}" = "INFORMIX" ] ; then
				if [ "${platform}" = "hp11.00" ] ; then
					platform=hp11_64
					if [ "${batch_mode}" = "" ] ; then
						LIST="${LIST} ${prod}_32"
						eval ${prod}_32_tar_image="${prod}.hp11.00.tar.Z"
						eval ${prod}_32_version_file="${prod}.hp11.00.version"
					fi
				elif [ "${platform}" = "solaris2.6" ] ; then
					platform=solaris7_64
					if [ "${batch_mode}" = "" ] ; then
						LIST="${LIST} ${prod}_32"
						eval ${prod}_32_tar_image="${prod}.solaris2.6.tar.Z"
						eval ${prod}_32_version_file="${prod}.solaris2.6.version"
					fi
				fi
			fi
		fi
		if [ "${PLATFORM}" = "sgi65" -a "${prod}" = "OEBU" ] ; then
			platform=sgi6
		fi
		if [ "${PLATFORM}" = "rs6000_43" ] ; then
			if [ "${prod}" != "LOTUS" -a "${prod}" != "PROXY" -a\
			     "${prod}" != "TERADATA" ]; then
				platform=rs6000_42
			fi
		fi
		eval ${prod}_tar_image="${prod}.${platform}.tar.Z"
		eval ${prod}_version_file="${prod}.${platform}.version"
	done

	while [ ${done} -eq 0 ] ; do
		for prod in ${LIST}; do
			if eval [ -s ${TARPATH}/\${${prod}_tar_image} -a \
				  -s ${TARPATH}/\${${prod}_version_file} ] ; then
				product_list="${product_list} ${prod}"
			fi
		done

		if [ "${product_list}" = "" ] ; then
			if [ ${notdir} -eq 0 ] ; then
				echo "
No valid database extension tar.Z files found in ${TARPATH} directory."
			fi
			notdir=0

			if [ "${batch_mode}" = "" ] ; then
				if confirm_no "Do you want to continue?"
				then
					echo "No database extension packages were installed."
					wrap_up 0
				else
					Prompt "Enter a directory where tar.Z files are located: "
					read TARPATH
					echo "${TARPATH}" >> ${run_Trace_File}
					if [ ! -d "${TARPATH}" ] ; then
						echo ""
						echo "Directory ${TARPATH} does not exist"
						notdir=1
					fi
				fi
			else
				wrap_up 0
			fi
		else
			done=1
		fi
	done
}

#
#  Use the list of extensions created in get_tar_images.  If there's only
#  one and install_dbext was called with no parameters, say we're installing
#  that one and ask if they want to continue.  If install_dbext was called
#  with parameters, it would have been via cdrom_install_dbext which already
#  asked this question, or via update_dbclients which runs in batch and we
#  can't answer any questions interactively.  If there's more than one,
#  provide a menu and let them choose which ones.  Once they're done
#  selecting, provide the list and let them confirm it.  If they're not
#  happy, the selecting process starts again.  If the list was ok, we're
#  done here.  In both cases, the variable PRODUCT contains which extensions
#  we're supposed to install.
#

choose_product ()
{
	num_products=0

	#
	#  Make sure the list of products is sorted.  Things get out
	#  of order when tacking on the 32 bit products.
	#

	rm -f ${TMPDIR}/db_prod_list.$$
	for product_name in ${product_list} ; do
		echo "${product_name}" >> ${TMPDIR}/db_prod_list.$$
	done
	product_list=`sort ${TMPDIR}/db_prod_list.$$`
	rm -f ${TMPDIR}/db_prod_list.$$

	#
	#  Assign each extension to a variable based on a number
	#  that matches its location in the menu display.  Example,
	#  product_1=BACKTRACK.
	#

	for product_name in ${product_list}; do
		num_products=`expr ${num_products} + 1`
		eval product_${num_products}="${product_name}"
	done

	PRODUCT=""
	if [ ${num_products} -eq 1 ]; then
		opt=1
		PRODUCT=${product_name}
		get_pkg_info ${PRODUCT}
		echo ""
		echo "${Stars}"
		echo ""
		echo "        Installing NetBackup for ${real_product}"
		echo ""
		if [ ${noparams} -eq 1 ] ; then
			if confirm_yes "Do you wish to continue?"
			then
				:
			else
				PRODUCT=""
			fi
		fi
	else
		good_list=0
		all_picked=0
		while [ ${good_list} -ne 1 ]; do

			#
			#  Print out the menu choices, using the product_<number>
			#  assignments and the readable extension names.  Make sure
			#  that when the list reaches 10, to delete a space so it
			#  still lines up nicely.
			#

			tput clear 2>/dev/null
			echo ""
			echo "${Stars}"
			echo ""
			echo "  NetBackup Database Extension Installation"
			echo ""
			echo "Choose the Database Extensions you wish to install"
			echo "one at a time or select Install All of the Above."
			echo ""

			index=0
			spacer=" "
			while [ ${index} -lt ${num_products} ]; do
				index=`expr ${index} + 1`
				eval get_pkg_info \${product_${index}}
				if [ ${index} -gt 9 ] ; then
					spacer=""
				fi
				echo "       ${spacer}${index})  NetBackup for ${real_product}"
			done

			#
			#  Give them the ability to pick the whole bunch at once.  q is
			#  used when they're done picking, x will bail out completely.
			#

			index=`expr ${index} + 1`
			if [ ${index} -gt 9 ] ; then
				spacer=""
			fi
			echo ""
			echo "       ${spacer}${index})  Install All of the Above"
			echo ""
			echo "	q)  Done Selecting Extensions"
			echo "	x)  Exit from this Script"
			echo ""

			done_selecting=0
			while [ ${done_selecting} -ne 1 ]; do
				get_number "Choose an option: " 1 ${index}
				answ=${VALUE}

				for opt in ${answ} ; do
					if [ "${opt}" = "x" ] ; then
						done_selecting=1
						good_list=1
					elif [ "${opt}" = "q" ] ; then
						echo ""
						echo "You have chosen to install these Database Extensions:"
						echo ""
						rm -f ${TMPDIR}/db_pkg_list.$$
						for prod in ${PRODUCT}; do
							echo "	${prod}" >> ${TMPDIR}/db_pkg_list.$$
						done

						#
						#  Sort the chosen list (throwing out duplicates)
						#  for an easier to read display otherwise,
						#  display NONE.
						#

						if [ -f ${TMPDIR}/db_pkg_list.$$ ] ; then
							PRODUCT="`sort -u ${TMPDIR}/db_pkg_list.$$`"
							for prod in ${PRODUCT}; do
								get_pkg_info ${prod}
								echo "        NetBackup for ${real_product}"
							done
						else
							echo "	NONE"
						fi
						echo ""
						rm -f ${TMPDIR}/db_pkg_list.$$

						#
						#  If they don't like what they picked, whack
						#  the list and display the choices again.
						#

						if confirm_yes "Is this list correct?"
						then
							good_list=1
						else
							PRODUCT=""
						fi
						done_selecting=1
					else

						#
						#  If they picked all, whack the list and use the
						#  product list originally determined from MAIN.
						#  Keep track if all was picked so if they pick
						#  a single extension afterwards, we know to
						#  re-initialize the chosen list.  If they pick
						#  just single extensions, keep tacking them on
						#  the end of the existing list.  We use sort -u
						#  later to make sure there's no dupes.
						#

						if [ "${opt}" = `expr ${num_products} + 1` ]; then
							PRODUCT=""
							for product_name in ${product_list}; do
								PRODUCT="${PRODUCT} ${product_name}"
							done
							all_picked=1
						else
							if [ ${all_picked} -ne 1 ]; then
								eval PRODUCT=\"${PRODUCT} \${product_${opt}}\"
							else
								eval PRODUCT=\${product_${opt}}
								all_picked=0
							fi
						fi
					fi
				done
			done

			echo "${Stars}"
			echo ""
		done
		if [ "${PRODUCT}" = "" ] ; then
			echo "No extensions were selected."
		fi
	fi
}

#
#  This function gets the NetBackup client version level and the version
#  level for the product passed in as parameter 1.  If they are not the same,
#  we don't recommend installing.  We give them an override option except
#  if we're in batch mode.  The value of the vok variable is what controls
#  whether the extension gets loaded.
#

check_version ()
{
	pass=${pass:=0}
	prd=${1}
	ext_level=""
	vok=0

	#
	#  Only get the client level the first time this is called.
	#  Useful when more than one extension is being loaded.
	#

	if [ ${pass} -eq 0 ] ; then
		tmp_level=`cat ${NB_PATH}/bin/version | cut -f2 -d" "`
		client_level=`expr ${tmp_level} : '\([0-9.]*\)'`
		pass=1
	fi

	#
	#  Get the level of the extension from the version file.
	#
	#  If we're on a 64 bit machine and have 32 bit products, set prod
	#  to <product>_32.  That way, the text displayed to the user
	#  via get_pkg_info/real_product comes out right.
	#

	eval inst_file=\${${prd}_version_file}
	plat=`basename ${inst_file} .version | cut -f2,3 -d'.'`
	prod=`echo ${inst_file} | cut -f1 -d'.'`
	if [ ${is_64_bit} -eq 1 ] ; then
		if [ "${prod}" = "OEBU" -o "${prod}" = "INFORMIX" ] ; then
			if [ "${plat}" = "hp11.00" -o "${plat}" = "solaris2.6" ] ; then
				prod=`echo ${inst_file} | cut -f1 -d'.'`_32
			fi
		fi
	fi

	if [ -s ${TARPATH}/${inst_file} ] ; then
		tmp_level=`cat ${TARPATH}/${inst_file} | cut -f2 -d" "`
		ext_level=`expr ${tmp_level} : '\([0-9.]*\)'`
	else
		echo ""
		echo "WARNING:  ${TARPATH}/${inst_file} is missing."
		echo ""
		ext_level="Unknown"
	fi

	#
	#  If the client level and the extension level are not the same, 
	#  we don't recommend installing.  If not running in batch mode,
	#  we give them the option to override.  If in batch, we issue
	#  a specific message that update_dbclients greps for.
	#

	if [ "${client_level}" != "${ext_level}" ]; then
		get_pkg_info ${prod}
		echo ""
		echo "--> NetBackup client version level ${client_level} is not equal"
		echo "--> to the database extension level ${ext_level}.  This"
		echo "--> combination is not recommended."
		echo ""

		if [ "${batch_mode}" = "" ] ; then
			if confirm_no "Do you wish to override and install ${real_product}?"
			then
				:
			else
				vok=1
			fi
		else
			echo "Extension and Client version files do not match - cannot update."
			wrap_up 1
		fi
	else
		vok=1
	fi
}

#
#  Based on the info in /usr/openv/netbackup/bin/version, determine
#  what platform type we're going to install.  If we're on a machine
#  or an operating system the database extensions don't support, we exit.
#  Note I used cat/sed to obtain MACHINE rather than head because for
#  sgi65, head is in /usr/bsd and ksh (linked to /bin/sh) doesn't have
#  /usr/bsd built into its internal paths.  Not a problem if logged in
#  but it fails when executed via bpinst.  Add the paths built into
#  the old IRIX shell since zcat is also in /usr/bsd and adding only that
#  causes me to lose other paths.
#

get_platform ()
{
	is_64_bit=0
	if [ -f ${NB_PATH}/bin/version ]; then
		MACHINE=`cat ${NB_PATH}/bin/version | sed -e '1q' | cut -f2- -d-`
		PLATFORM=nosupport

		case "${MACHINE}" in
			AIX*)
				OS=`uname -a | cut -f3,4 -d" "`
				if [ "${OS}" = "2 4" ] ; then
					PLATFORM=rs6000_42
				elif [ "${OS}" = "3 4" ] ; then
					PLATFORM=rs6000_43
				fi
				;;
			DYNIX*)
				OS=`uname -v | cut -c1-4`
				REV=`uname -v | cut -c6`
				if [ "${OS}" = "V4.5" -o "${OS}" = "V4.4" -o \
				  \( "${OS}" = "V4.2" -a "${REV}" -ge "3" \) ] ; then
					PLATFORM=sequent4.2
				fi
				;;
			HP*)
				OS=`uname -r | cut -f2 -d"."`
				if [ "${OS}" = "10" ] ; then
					PLATFORM=hp10.20
				elif [ "${OS}" = "11" ] ; then
					PLATFORM=hp11.00
					os_bits=`getconf _CS_KERNEL_BITS`
					if [ "${os_bits}" = "64" ] ; then
						is_64_bit=1
					fi
				fi
				;;
			IRIX*)
				PATH="/usr/sbin:/usr/bsd:/bin:/usr/bin:${PATH}"
				OS_f1=`uname -r | cut -f1 -d"."`
				OS_f2=`uname -r | cut -f2 -d"."`
				if [ "${OS_f1}" = "6" -a "${OS_f2}" -ge "5" ] ; then
					PLATFORM=sgi65
				elif [ "${OS_f1}" = "6" ] ; then
					PLATFORM=sgi6
				fi
				;;
			OSF1*)
				OS_f1=`uname -r | cut -f1 -d"."`
				if [ "${OS_f1}" = "V4" -o "${OS_f1}" = "V5" ] ; then
					PLATFORM=alpha
				fi
				;;
			Solaris*)
				OS_f1=`uname -r | cut -f1 -d"."`
				OS_f2=`uname -r | cut -f2 -d"."`
				if [ "${OS_f1}" = "5" -a "${OS_f2}" = "6" ] ; then
					PLATFORM=solaris2.6
				elif [ "${OS_f1}" = "5" -a "${OS_f2}" -ge "7" ] ; then
					PLATFORM=solaris2.6
					os_bits=`isainfo -nv`
					echo ${os_bits} | grep "64\-bit" > /dev/null 2>&1
					if [ $? -eq 0 ] ; then
						is_64_bit=1
					fi
				fi
				;;
			UNIX*)
				unix_level=`uname -s`
				if [ "${unix_level}" = "dgux" ]; then
					PLATFORM=dgux
				else
					PLATFORM=ncr
				fi
				;;
		esac

		if [ "${PLATFORM}" = "nosupport" ] ; then
			echo "WARNING: Platform ${MACHINE} is not supported."
			wrap_up 1
		fi
	else
		echo ""
		echo "${NB_PATH}/bin/version not found."
		echo "Installation Aborted."
		wrap_up 1
	fi
}

#
#  This function gets the product name as input (ex. OEBU).  It
#  determines whether it has a runscript.  If not, this function is
#  done.  If so, it makes sure the file exists and runs it.  If set,
#  the batch mode parameter is passed down to the runscripts so they
#  don't ask any questions in that situation either.
#

execute_run_script ()
{
	prod=${1}

	get_pkg_info ${prod}
	if [ "${run_script}" != "" ] ; then
		if [ -f	${run_script} ] ; then
			${run_script} ${batch_mode} ${run_Trace_File}
		else
			echo ""
			echo "WARNING: ${run_script} is not found."
			return 1
		fi
	fi
}

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

Stars="**********"
TMPDIR=${TMPDIR:=/tmp}
NB_PATH=/usr/openv/netbackup

#
#  This is the list of all possible UNIX database extensions.
#  These names should match the extension name part of the tar.Z
#  file names.
#

All_Extension_List="BACKTRACK
DB2
INFORMIX
LOTUS
OEBU
PROXY
SAP
SYBASE
TERADATA"

#
#  This program can be called three ways.  The first is with no parameters
#  at all (like it has been in the past).  Version checking will occur.  It
#  will run and ask questions as before but we now create a trace file in
#  TMPDIR.  The second way is from cdrom_install_dbext (during a local install)
#  which calls it with a specific extension name and a trace file name.
#  Version checking has already happened in cdrom_install_dbext so skip it
#  here.  Since it's being executed as part of a local install, we still want
#  to be able to ask the user questions so it's still not considered batch.
#  The last way is called from update_dbclients and this script is run on the
#  client on behalf of the server.  That must run "in batch" and ask no questions
#  so update_dbclients calls this script with a -B parameter, a specific extension
#  name and a trace file name.  If the client update_dbclients is updating
#  is also the master server, version checking will occur.  If update_dbclients
#  is being run with -ForceInstall, it passes it along and we will override a
#  version check failure here.  On a successful install, install_dbext puts a
#  copy of the version file in /usr/openv/netbackup/ext and puts a time stamp
#  in it.
#
#  Have to be a bit careful with the trace files.  If called with no parameters,
#  a trace file is created in TMPDIR.  If called via cdrom_install_dbext, a
#  tee is being done in that script to capture the trace, so throw away the
#  tee to a trace file here.  Use the cdrom_install_dbext trace file for the
#  runscript and for answers in this script or we lose all the answers.  When
#  called in batch mode, no tee is happening so we do need to do the tee here.
#

batch_mode=""
cmd_ext=""
VC=""
noparams=0

if [ $# -gt 4 -o $# -eq 1 ] ; then
	USAGE
else
	if [ $# -eq 0 ] ; then
		noparams=1
		VC="TRUE"
		Trace_File=${TMPDIR}/trace_install_dbext.$$
		run_Trace_File=${Trace_File}
	else
		if [ "${1}" = "-enforce_version_check" ] ; then
			VC="TRUE"
			shift
		fi
		if [ "${1}" = "-B" ] ; then
			batch_mode="-B"
			shift
		fi
		if [ $# -ne 2 ] ; then
			USAGE
		else
			char=`echo ${2} | cut -c1`
			if [ "${char}" = "-" ] ; then
				USAGE
			else
				if [ "${batch_mode}" = "" ] ; then
					Trace_File=/dev/null
					run_Trace_File=${2}
				else
					Trace_File=${2}
					run_Trace_File=${Trace_File}
				fi
				cmd_ext=${1}
			fi
		fi
	fi
fi

#
#  Make sure we can create the trace file and that it is
#  not a directory.
#

if [ ! -d `dirname ${run_Trace_File}` ] ; then
	mkdir -p `dirname ${run_Trace_File}`
elif [ -d ${run_Trace_File} ] ; then
	echo ""
	echo "${run_Trace_File} is a directory not a file."
	exit 1
fi

(

#
#  Make sure this is being run from root.
#

ISROOT=`/usr/bin/id | egrep "^uid=0\("`
if [ "${ISROOT}" = "" ] ; then
	echo ""
	echo "$0 must be run while logged in as root."
	echo ""
	wrap_up 1
fi

#
#  If an extension was passed in on the command line,
#  make sure the name is valid.
#

if [ "${cmd_ext}" != "" ] ; then
	found=0
	for ext in ${All_Extension_List}; do
		if [ "${ext}" = "${cmd_ext}" ] ; then
			found=1
			break
		fi
	done
	if [ ${found} -eq 0 ] ; then
		echo ""
		echo "${cmd_ext} is not a valid database extension name.  Possible"
		echo "names are:"
		echo ""
		echo "${All_Extension_List}"
		echo ""
		wrap_up 1
	fi
fi

#
#  Make sure user_ops exists with the right permissions.  This is
#  used by all extensions.
#

if [ ! -d ${NB_PATH}/logs/user_ops ]; then
	mkdir -p ${NB_PATH}/logs/user_ops
fi
chmod 1777 ${NB_PATH}/logs/user_ops

error=0

get_platform

get_tar_images

choose_product

if [ "${opt}" != "x" ] ; then
	for product in ${PRODUCT} ; do
		if [ "${VC}" = "TRUE" ] ; then
			check_version ${product}
		else
			vok=1
		fi

		if [ ${vok} -eq 1 ] ; then
			status=0
			get_pkg_info ${product}

			echo ""
			echo ""
			echo "   Installing NetBackup for ${real_product}..."

			eval untar_image ${TARPATH}/\${${product}_tar_image}

			if [ $? -ne 0 ]; then
				status=1
			else
				execute_run_script ${product}
				if [ $? -ne 0 ]; then
					status=1
				fi
			fi

			#
			#  Remove the old version file that indicates a successful
			#  install (if it exists).  Don't want to leave one around
			#  in case this install fails.
			#

			eval inst_file=\${${product}_version_file}
			rm -f ${NB_PATH}/ext/${inst_file}

			if [ ${status} -ne 0 ]; then
				echo "NetBackup for ${real_product} installation failed."
				error=1
			else
				if [ ! -d ${NB_PATH}/ext ] ; then
					mkdir -p ${NB_PATH}/ext
				fi

				#
				#  On a successful install, put a copy of the version file
				#  in /usr/openv/netbackup/ext and put a time stamp in it.
				#

				cp ${NB_PATH}/dbext/${inst_file} ${NB_PATH}/ext
				echo "Installation Date: `date`" >> ${NB_PATH}/ext/${inst_file}
				echo ""
				echo "NetBackup for ${real_product} installation completed."
			fi
			echo ""
			echo "${Stars}"
		fi
	done
fi

echo ""

if [ ${error} -ne 0 ] ; then
	echo "One or more of the installations failed."
	wrap_up 1
else
	echo "Execution of $0 is complete."
	wrap_up 0
fi

)  2>&1 | tee -a ${Trace_File}

if [ ${noparams} -eq 1 ] ; then

	#
	#  Find out our real exit status.
	#

	if [ "${batch_mode}" = "" ] ; then
		echo "File ${Trace_File} contains a trace of this install."
	fi
fi
exit_value=`cat ${TMPDIR}/install_dbext.$$.status`
rm -f ${TMPDIR}/install_dbext.$$.status
exit ${exit_value}
