#!/bin/echo Do_not_execute
#
# This script can not be executed as a separate program:
#     it must be source'd in other shell scripts
# To enforce this, echo is used as the interpreter
#

# OS dependent options
uname=`uname`
if [ ${uname} = "Linux" ]
then
	ECHO="echo -e"
else
	ECHO="echo"
fi

cls
#ask_yesno 10 1 "Do you want to install the Oracle Required Support Files ? " $YES
OK=$YES
if [ "$OK" = $YES ]
then
	ORA_CLIENT_DIST=$BSE/bin/ora_client_files
	ORA_RSF_DIR=$BSE/lib/ora/oracle_home

	# Get the Oracle version of the Required Suppoort Files in this distribution.
	get_RSF_version_from_dist()
	{
		# Format of the $ORA_CLIENT_DIST file:
		# One line containing the Oracle version number, followed by a compressed cpio immage
		# of the Oracle RSF files.

		if [ -f $ORA_CLIENT_DIST ]
		then
			head -1 $ORA_CLIENT_DIST
		else
			${ECHO} ""
		fi
	}

	# Get the Oracle version of the Required Suppoort Files in the current installation
	get_RSF_version_from_bse()
	{
		# The directory $BSE/lib/ora/oracle_home should contain the file
		# orainst/RELVER containing a line like:
		# RELEASE_VERSION=8.0.5.0.0

		if [ -f $ORA_RSF_DIR/Version ]
		then
			cat $ORA_RSF_DIR/Version
		else
			${ECHO} ""
		fi
	}

	cls
	effect 10 1 $UNDERLINE "Install Oracle Required Support Files"

	effect 10 3 "" "Checking Oracle versions ..."
	effect 10 20 "" ""
	ORA_NEW_RSF=`get_RSF_version_from_dist `
	ORA_OLD_RSF=`get_RSF_version_from_bse `
	effect 10 3 "" "$CLEAR_EOL"

	effect 10 4 "" "Currently installed version :"
	effect 40 4 "" "${ORA_OLD_RSF:-none}"
	effect 10 5 "" "New version to be installed :"
	effect 40 5 "" "${ORA_NEW_RSF:-unknown}"

	if [ -z "$ORA_NEW_RSF" ]
	then
		prompt 10 22 "Fatal error: Oracle RSF files missing" 1 "." $DISPLAY
		user_end
	fi

	effect 10 7 "" "Oracle RSF files location: $ORA_RSF_DIR"

	if [ -z "$ORA_OLD_RSF" ]
	then
		effect 10 9 "" "There is no Oracle client environment yet, installation required"
		MUST_INSTALL=$YES
	elif [ "x$ORA_NEW_RSF" = "x$ORA_OLD_RSF" ]
	then
		effect 10 9 "" "The same version has already been installed, no installation needed"
		QUESTION="Do you want to re-install"
		DEFAULT_ANSWER=$NO
		MUST_INSTALL=$NO
	else
		effect 10 9 "" "This porting set contains a different version, installation recommended"
		QUESTION="Do you want to update"
		DEFAULT_ANSWER=$YES
		MUST_INSTALL=$NO
	fi

	if [ "$MUST_INSTALL" = $NO ]
	then
		ask_yesno 10 11 "$QUESTION the Oracle RSF files ? " $DEFAULT_ANSWER

		if [ "$OK" = $YES ]
		then
			MUST_INSTALL=$YES

			ask_yesno 10 13 "Do you want to save the current Oracle RSF files ? " $NO

			if [ "$OK" = $YES ]
			then
				SAVE_RSF_DIR=$ORA_RSF_DIR.`date '+%y%m%d.%H%M%S'`
				
				effect 10 14 "" "Old Oracle RSF files saved in $SAVE_RSF_DIR"
				mv $ORA_RSF_DIR $SAVE_RSF_DIR
			else
				effect 10 14 "" "Deleting files ..."
				rm -rf $ORA_RSF_DIR
				effect 10 14 "" "Old Oracle RSF files deleted"
			fi
		fi

	fi

	if [ "$MUST_INSTALL" = $YES ]
	then
		effect 10 15 "" ""

		Old_mask=`umask`
		umask 02

		mkdir $ORA_RSF_DIR

		if [ -d $ORA_RSF_DIR ]
		then
			effect 10 16 "" "installing ..."
			(
				cd $ORA_RSF_DIR
				read Version
				${ECHO} "$Version" > $ORA_RSF_DIR/Version
				zcat | cpio -icdu	# unpack distribution
			) < $ORA_CLIENT_DIST
			effect 10 16 "" "${CLEAR_EOL}Oracle RSF files installed."
		else
			prompt 10 22 "Fatal error: directory $ORA_RSF_DIR does not exist" 1 "." $DISPLAY
			user_end
		fi

		baan_chown $ORA_RSF_DIR
		umask $Old_mask
		pause
	fi
fi
