#!/bin/sh
#
#  (c) 1996-1998 Enlighten Software Solutions, Inc.
#  All Rights Reserved
#
#  Script:  $CDROM_DIR/CDINSTAL
#
#         * CDINSTAL should be in the root dir of the CDROM ($CDROM_DIR).
#         * CDINSTAL assumes there is the following file structure:
#
#           $CDROM_DIR/:
#            AIX41          COMMON         SCO32          SOL25 
#            CDINSTAL       HPUX905        SCO5           SUN413
#            IRIX           SOL24
#
#           $CDROM_DIR/<os name>/:
#            ADMIN.TXT     EMD.TXT       INFORMIX.TXT  ODBC.TXT      RENLD.TXT
#            ADMIN.Z       EMD.Z         INFORMIX.Z    ODBC.Z        RENLD.Z
#            COMMON.TXT    EVENTS.TXT    INSTALL.TXT   PEP.TXT
#            COMMON.Z      EVENTS.Z      INSTALL.Z     PEP.Z
#
#            (Note that due to the limitations of some OSes, all these 
#             components may not exist).
#
#         * CDINSTAL and enl_xinstall require installations from tape
#           to be extracted (creating a directory structure like above)
#           before they are invoked.
#
# 'CDINSTAL' does the following:
#  1) auto-detects the local host's OS 
#  2) asks to archive any pre-2.0 Enlighten configuration files
#  3) prompts for the local installation directory 
#  4) stores that directory pathname into /etc/enlighten
#  5) cp's the necessary applications to the installation directory
#  6) execs the installation program (enl_xinstall)
#
#
#

#
# Be anal. For security purposes any file or directory created 
# by the execution of this script should have all permission 
# on group and other turned off.
#
umask 077

E_REL="2.6.0" # Enlighten Release number

PATH=/bin:/usr/bin:/usr/sbin:/usr/bsd:/usr/ucb:/etc:/usr/etc:$PATH
export PATH

hit_return(){
	echo
	echo "Please hit <return> to continue ..."
	read nothin
	case "$nothin" in
		q | quit | x | e | exit ) 
			echo "Exitting.  All partitions left untouched."
			echo
			exit 2
			;;
	esac
}

get_answer(){
	read response;
	case "$response" in
		"" )
			response=y
			;;
		*)  
			;;
	esac
}

proc_version_1(){
	echo "An old version of Enlighten is installed locally."
	echo
	echo "Please choose from the following:"
	echo
	echo "keep)    Keep the old version of Enlighten"
	echo "         and install Enlighten $REL components in a different directory"
	echo
	echo "archive) Deinstall the old version of Enlighten"
	echo "         and archive files in $OLD_INSTALL_DIR"
	echo
	echo "remove)  Deinstall the old version of Enlighten"
	echo "         and delete all files in $OLD_INSTALL_DIR"
	echo
	$ECHO "[keep|archive|remove|exit] ? \c"

	read response

	case "$response" in
		q | quit | x | e | exit ) 
			echo "Exitting.  All partitions left untouched."
			echo
			exit 2
			;;
		k* | K* )
			echo Keeping Enlighten stored in $OLD_INSTALL_DIR intact.
			;;
		a* | A* )
			echo "To which directory should I archive the contents of $OLD_INSTALL_DIR"
			$ECHO "(default = '$ARCHIVE_DIR') ?\c"
			read userArchiveDir
			case "$userArchiveDir" in
				"" ) 
					userArchiveDir=$ARCHIVE_DIR ;;
				* )
					if [ ! -d $userArchiveDir ] ; then
						mkdir -p $userArchiveDir
						if [ $? -ne 0 ] ; then
							echo "Could not create directory $userArchiveDir. "
							echo "Exitting. All partitions left untouched."
							echo
							exit 2
						fi
					fi
					;;
			esac

			#
			# Make sure that they're not putting the tar file archive in $OLD_INSTALL_DIR...
			#

			blah=`echo "$userArchiveDir" | grep "$OLD_INSTALL_DIR"`
			export blah


			if [ ! "$blah" = "" ] ; then
				echo "Cannot create tar file inside previous install directory."
				echo "Exitting. All partitions left untouched."
				echo
				exit 2
			fi

			echo "Creating tar file $userArchiveDir/$ARCHIVE_NAME."
			tar -cf $userArchiveDir/$ARCHIVE_NAME $OLD_INSTALL_DIR
			if [ $? -ne 0 ] ; then
				echo "The tar operation failed."
				echo "Exitting. All partitions left untouched."
				echo
				exit 2
			else
				compress $userArchiveDir/$ARCHIVE_NAME
				echo "Archival of $OLD_INSTALL_DIR was successful. "
				echo "The tarred and compressed file is stored in"
				echo "$userArchiveDir/$ARCHIVE_NAME."
				echo
				echo "Are you sure you want to REMOVE ALL FILES stored in"
				$ECHO "$OLD_INSTALL_DIR [y|n|exit] (default is 'y') ?\c"
				get_answer
				case $response in
					y* | Y* )
						/bin/rm -rf $OLD_INSTALL_DIR
						ToastConfigFiles
						;;
				esac
			fi
			;;

		r* | R* )
			echo "Are you sure you want to REMOVE ALL FILES stored in"
			$ECHO "$OLD_INSTALL_DIR [y|n|exit] (default is 'y') ?\c"
			get_answer;
			case $response in
				y* | Y* )
					/bin/rm -rf $OLD_INSTALL_DIR
					ToastConfigFiles
					;;
				*)
					echo "Exitting. All partitions left untouched."
					echo
					exit 2
					;;
			esac
			;;
	esac
}

#
#  1) get the local host's OS type 
#  -------------------------------------------------------------------
#  Note: GetHostInfo() is that same in buildcd, os_type, and CDINSTAL.
#  Any changes made here should be pushed into these other files as well.
#

GetHostInfo(){
	OS=`uname -s`
	REL=`uname -r`
	ORIG_OS=$OS

	PSOPTS=""

	ECHO=echo
	case "$OS" in

	OSF1)
		case "$REL" in

		V3.2 | V4.0 ) 
			OS="DEC3240" ;;

		* ) 
			if [ "$install_any" -eq 1 ] ; then
				echo ""
				echo "Detected OS: $OS $REL"
				echo "Using DEC 4.0 binaries...."
				OS="DEC3240"
			else
				OS="unknown" 
			fi
			;;

		esac ;;
		
	HP-UX)
		case "$REL" in

		A.09.04 | A.09.05 ) 
			OS="HP904905" ;;

		?.10.* ) 
			OS="HP10X" ;;

		* ) 
			if [ "$install_any" -eq 1 ] ; then
				echo ""
				echo "Detected OS: $OS $REL"
				echo "Using HPUX 10.x binaries...."
				OS="HP10X"
			else
				OS="unknown" 
			fi
			;;

		esac ;;

	AIX)
		VER=`uname -v`
		if [ "$VER" = "4" ] ; then
			if [ "$REL" = "1" -o "$REL" = "2" ] ; then
				OS="AIX41"
			elif [ "$install_any" -eq 1 ] ; then
				echo ""
				echo "Detected OS: $OS $REL"
				echo "Using AIX 4.1 binaries...."
				OS="AIX41"
			else
				OS="unknown"
			fi
		elif [ "$install_any" -eq 1 ] ; then
			echo ""
			echo "Detected OS: $OS $REL"
			echo "Using AIX 4.1 binaries...."
			OS="AIX41"
		else
			OS="unknown"
		fi 
		;;

	SunOS)
		case "$REL" in

		5.4 | 5.5 | 5.5.1 ) 
			OS="SOL2X" ;;

		4.1.3* | 4.1.4* )   
			OS="SUNOS41X"

			if [ -x /usr/5bin/echo ] ; then
				ECHO=/usr/5bin/echo
			fi
			PSOPTS="-auxww" ;;

		*) 
			if [ "$install_any" -eq 1 ] ; then
				echo ""
				echo "Detected OS: $OS $REL"
				echo "Using Solaris 2.x binaries...."
				OS="SOL2X"
			else
				OS="unknown" 
			fi
			;;

		esac ;;

	IRIX | IRIX64 )
		case "$REL" in

		5.3 | 6.* ) 
			OS="IRIX5X6X" ;;
		*) 
			if [ "$install_any" -eq 1 ] ; then
				echo ""
				echo "Detected OS: $OS $REL"
				echo "Using IRIX 6.x binaries...."
				OS="IRIX5X6X"
			else
				OS="unknown" 
			fi
			;;

		esac ;;

	*)
		HW=`uname -m`
		case "$HW" in

		i386)
			case "`uname -s`" in

			SCO_SV) 
				OS="SCO5X" ;;

			*)
				case "$REL" in

				3.2)
					OS="SCO32" ;;

				*)
					if [ "$install_any" -eq 1 ] ; then
						echo ""
						echo "Detected OS: $OS $REL"
						echo "Using SCO 5.x binaries...."
						OS="SCO5X"
					else
						OS="unknown" 
					fi
					;;

				esac ;;
			esac ;;

		*)
			OS="unknown" ;;

		esac ;;
	esac

	if [ $OS = "unknown" ] ; then
		echo
		echo "### Sorry, the local OS ($ORIG_OS $REL)is not currently supported by Enlighten/DSM."
		echo "    A port may be on the way, and your request may speed up the effort!"
		echo "    Please contact Enlighten Software for more information."
		echo
		echo "Exitting.  No partitions were touched."
		echo
		exit 2;
	fi

	if [ -z "$PSOPTS" ] ; then
		OPTS="-e"
		PIDCOLUMN=1
	else
		OPTS=$PSOPTS
		PIDCOLUMN=2
	fi

	export OPTS PIDCOLUMN OS
}

buildVirtualCDROM () {

	actualCdromDir=$cdromDir
	cdromDir=/tmp/cdrom

	echo
	echo "$appName: The CD-ROM was mounted with lowercase filenames."

	INSTALL_FILE=$cdromDir/$OS/INSTALL

	if [ -d $cdromDir ] ; then
		if [ -d $cdromDir.old ] ; then
			rm -rf $cdromDir.old
			if [ $? -ne 0 ] ; then
				echo "Could not make virtual CD-ROM in $cdromDir. Exitting."
				exit 1
			fi
		fi
		mv -f $cdromDir $cdromDir.old
	fi

	mkdir $cdromDir
	if [ $? -ne 0 ] ; then
		echo "Could not make a virtual CD-ROM in $cdromDir. Exitting."
		exit 1
	fi

	cd $actualCdromDir

	echo "$appName: A virtual CD-ROM is now being built in $cdromDir."

	for i in * ; do
		$ECHO ".\c"
		if [ -d $i ] ; then
			mkdir $cdromDir/`echo $i | tr [a-z] [A-Z]`
			if [ $? -ne 0 ] ; then
				echo
				echo "Could not make virtual CD-ROM in $cdromDir. Exitting."
				exit 1
			fi
		else
			ln -s `pwd`/$i $cdromDir/`echo $i | tr [a-z] [A-Z]`
			if [ $? -ne 0 ] ; then
				echo
				echo "Could not make virtual CD-ROM in $cdromDir. Exitting."
				exit 1
			fi
		fi
	done

	for i in */* ; do
		$ECHO ".\c"
		base=`basename $i`
		BASE=`echo $base | tr [a-z] [A-Z]`
		tree=`dirname $i`
		TREE=`echo $tree | tr [a-z] [A-Z]`

		ln -s `pwd`/$i  $cdromDir/$TREE/$BASE

		if [ $? -ne 0 ] ; then
			echo
			echo "Could not make virtual CD-ROM in $cdromDir. Exitting."
			exit 1
		fi
	done

	echo
	echo
	newAppName=`echo $appName | tr [a-z] [A-Z]`

	if [ -x $cdromDir/$newAppName ] ; then
		echo "In the future, please use $cdromDir/$newAppName."
		echo
		echo "Please note: for the installation to work successfully, you must use"
		echo "'$cdromDir' as your CD-ROM mount point in the Enlighten Netowrk Installer."
		echo "This has been set as your default CD-ROM mount point."
		hit_return

		echo "Starting $cdromDir/$newAppName..."
		cd $cdromDir
		exec ./$newAppName
	else
		echo "### $cdromDir/$newAppName was not executable. "
		echo "    Please contact Enlighten Software Support."
		echo
		echo "Exitting."
		exit 1
	fi
}

#
# Verify that the install component is there, and start sgiinst.sh if
# necessary

VerifyCDROM() {

	INSTALL_FILE=$cdromDir/$OS/INSTALL

	if [ ! -r $INSTALL_FILE -a ! -r $INSTALL_FILE.Z ] ; then

		#
		# Deal with non-caps-mounted cdroms.
		#
		LOWERCASE_OS=`echo $OS | tr [A-Z] [a-z]`
		if [ -f "$cdromDir/$LOWERCASE_OS/install" -o -f "$cdromDir/$LOWERCASE_OS/install.z" ] ; then
			buildVirtualCDROM

		else
			echo
			echo "### Fatal: The $OS/INSTALL component is missing."
			echo
			echo "Please 'cd' to the root directory of the Enlighten CD-ROM and"
			echo "restart $appName"
			echo
			echo "Exitting.  All partitions left untouched."
			echo
			exit 2
		fi
	fi
}

#
#  2) see if there are any previous installations of Enlighten, and
#     toast.
#  -------------------------------------------------------------------
#

GetOldInstallDir (){

	#
	#  GetOldInstallDir sets OLD_VERSION to be
	#   '0' if no installation
	#   '1' if release 1.1, 1.2
	#   '2' if release 2.x
	#
	#  and OLD_INSTALL_DIR to be
	#   'virgin' if no installation
	#   'missing' if the enlighten directory is missing
	#    or the actual name of the installation directory
	#

	OLD_INSTALL_DIR=virgin
	OLD_VERSION=0

	if [ -f /usr/lib/enlighten/HOME ] ; then
		
		OLD_VERSION=1
		OLD_INSTALL_DIR=`cat /usr/lib/enlighten/HOME`

		if [ ! -d $OLD_INSTALL_DIR ] ; then
			OLD_INSTALL_DIR=missing
		fi

	elif [ -f $KEY_FILE ] ; then

		OLD_VERSION=2
		OLD_INSTALL_DIR=`cat $KEY_FILE | egrep "^enlighten=" | cut "-d=" -f2-`

		case $OLD_INSTALL_DIR in

			"")	OLD_INSTALL_DIR=missing
				;;

			*)	if [ ! -d $OLD_INSTALL_DIR ] ; then
					echo 
					echo "Error: The 'enlighten' directory stored in /etc/enlighten, "
					echo "'$OLD_INSTALL_DIR,' does not exist."
					OLD_INSTALL_DIR=missing
				fi
				;;
		esac

	fi
}

ToastConfigFiles() {

	#
	# TODO: Remove any modifications to files (etc/rc, etc/services) added to by 
	#       Enlighten 1.x installation scripts.
	#
	rm -rf /usr/lib/enlighten
}

#
#  3) prompt for the installation directory from the user
#  -------------------------------------------------------------------
#

GetNewInstallDir() {

	if [ $OLD_INSTALL_DIR = missing ] ; then
		echo
		echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
		echo "            Error Found - The Enlighten directory is missing. "
		echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
		echo
		echo "A previously installed version of Enlighten was not removed properly."
		echo "To deinstall the product, check that"

		if [ $OLD_VERSION = 1 ] ; then
			echo "  * the /usr/lib/enlighten/HOME file is removed or points to where Enlighten is installed"
		elif [ $OLD_VERSION = 2 ] ; then
			echo "  * the $KEY_FILE file is removed"
		fi
		echo "  * references in startup files (rc files) are deleted"
		echo "  * the Enlighten directory is recursively removed" 
		echo
		#echo "After Enlighten is properly deinstalled from this host, try $0 again."
		#echo
		echo $appName exitting...
		echo
		exit 2
	fi

#	if [ $VERBOSE = yes ] ; then
		echo
		echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
		echo "           Welcome to Enlighten for Unix/DSM (release $E_REL)"
		echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
		echo
		echo "  $appName does the following:"
		echo
		echo "  * auto-detects the local host's OS"
		echo "  * prompts for the directory to install Enlighten locally"
#		echo "  * transfers and executes the license manager (lmgrd)"
#		echo "    and the network installation GUI (enl_xinstall)"
		echo "  * transfers and executes the network installation GUI (enl_xinstall)"
		echo
		echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
		echo "      (type 'exit' at any of this installer's prompts to exit)         "

		hit_return

	#
	# Default to asking the user a installation directory.
	# validDir is set to "yes" if a 2.0 installation previously exists.
	#

	validDir=no

	if [ $OLD_VERSION = 0 ] ; then
			echo "Enlighten products are not currently installed on this system."
		#
		# Get a valid directory from the user
		#
		installDirectory=$DEFAULT_INSTALLDIR

	elif [ $OLD_VERSION = 1 ] ; then

		proc_version_1;

	elif [ $OLD_VERSION = 2 ] ; then

		echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
		echo
		echo "  Enlighten 2.x components are currently installed on this system in "
		echo "  $OLD_INSTALL_DIR.  Additional components of Enlighten must also be"
		echo "  installed into this directory."
		echo

#		if [ $VERBOSE = yes ] ; then
			echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"
			echo
			echo "  If you want to copy Enlighten to a different directory,"
			echo "  (e.g. copy /old/enlighten to /new/enlighten), do the following:"
			echo 
			echo "  1) Run /old/enlighten/bin/stop_enl_daemons"
			echo "  2) Copy /old/enlighten to /new/enlighten"
			echo "  3) Edit /etc/enlighten, changing the 'enlighten=...' line "
			echo "     to read 'enlighten=/new/enlighten'"
			echo "  4) Edit your rc file to reflect the new directory"
			echo
			echo "-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-"

		hit_return

		installDirectory=$OLD_INSTALL_DIR
		validDir=yes
	fi

	#
	# Must force the directory to be OLD_INSTALL_DIR if old_version = 2.
	#
			
	while [ $validDir = no ] 
	do
		echo
		echo "Install Enlighten components into '$installDirectory' ?"
		$ECHO "[y|n|exit] (default is 'y') \c"
		get_answer 
		case $response in
			q | quit | x | e | exit ) 
				echo "Exitting. $installDirectory untouched."
				exit 2
				;;
			y | yes | Y | YES )
				validDir=yes
				;;
			* )
				if [ $OLD_VERSION = 2 ] ; then
					echo "Exitting. $installDirectory untouched."
					exit 2
				else
					echo
					echo "Where would you like to install Enlighten on this machine"
					$ECHO "(instead of '$installDirectory')? \c"
					read newInstallDirectory
					case $newInstallDirectory in
						q | quit | x | e | exit ) 
							echo "Exitting. $installDirectory untouched."
							exit 2
							;;
						\/* )
							installDirectory=$newInstallDirectory
							;;
						* )
							echo 
							echo "Please enter full pathanames."
					esac
				fi
				;;
		esac
	done
}

#
#  4) store the ENLIGHTEN key into /etc/enlighten
#  -------------------------------------------------------------------
#

StoreInstallDirIntoEtcEnlighten(){
	if [ -f $KEY_FILE ] ; then
		mv $KEY_FILE $KEY_FILE.old
		egrep -v "^enlighten=" $KEY_FILE.old > $KEY_FILE
		mv $KEY_FILE $KEY_FILE.old
		egrep -v "^cdrom_dir=" $KEY_FILE.old > $KEY_FILE
		mv $KEY_FILE $KEY_FILE.old
		egrep -v "^install_host=" $KEY_FILE.old > $KEY_FILE
		mv $KEY_FILE $KEY_FILE.old
		egrep -v "^install_host_ip=" $KEY_FILE.old > $KEY_FILE
	else
		echo "#" > $KEY_FILE
		echo "# File: $KEY_FILE" >> $KEY_FILE
		echo "#" >> $KEY_FILE
		echo "# This file is used by all Enlighten components as a common repository" >> $KEY_FILE
		echo "# for the root Enlighten directory, the hostnames of the PEP and EMD servers," >> $KEY_FILE
		echo "# and the installation host (used for automatic license administration)." >> $KEY_FILE
		echo "#" >> $KEY_FILE
		echo "# Note that editting this file has the potential to corrupt the local " >> $KEY_FILE
		echo "# Enlighten installation. Please refer to the documentation for more info." >> $KEY_FILE
		echo "#" >> $KEY_FILE
		echo "# (c) 1996-1998 Enlighten Software Solutions, Inc." >> $KEY_FILE
		echo "# All Rights Reserved" >> $KEY_FILE
		echo "#" >> $KEY_FILE
		echo "" >> $KEY_FILE
	fi

	echo "enlighten=$installDirectory" >> $KEY_FILE
	local_hostname=`uname -n`
	echo "install_host=$local_hostname" >> $KEY_FILE

	#
	# Now put in the install_host_ip tag
	#

	if [ ! -r /etc/hosts ] ; then
		echo
		echo "Warning: Could not read the /etc/hosts file in order to resolve"
		echo "         the local host name to an absolute IP address. This may"
		echo "         cause problems with installations to remote hosts if"
		echo "         they cannot resolve $local_hostname to an IP address."
		echo
	else
		local_ip_address=`grep -v "^[ 	]*#" /etc/hosts | fgrep $local_hostname | awk '{ print $1 }'`
		if [ -z "$local_ip_address" ] ; then
			echo
			echo "Warning: The /etc/hosts file does not seem to resolve"
			echo "         the local host name to an absolute IP address. This may"
			echo "         cause problems with installations to remote hosts if"
			echo "         they cannot resolve $local_hostname to an IP address."
			echo
		else
			echo "install_host_ip=$local_ip_address" >> $KEY_FILE
		fi
	fi
	echo "cdrom_dir=$cdromDir" >> $KEY_FILE
	echo "Stored the new installation information into $KEY_FILE successfully."

	chown root $KEY_FILE
	chmod 444 $KEY_FILE
	if [ $? -ne 0 ] ; then
		echo
		echo "Error: Could not change access bits of $KEY_FILE."
		echo "       This may cause problems when starting the Enlighten GMS."
		echo "       ($KEY_FILE should be chmod'ed to 444)"
	fi
}

#
#  5) cp necessary applications to the installation directory
#  -------------------------------------------------------------------
#

CopyInstallFiles(){

	if [ ! -d $installDirectory ] ; then
		mkdir -p $installDirectory
		chmod 700 $installDirectory
		if [ $? -ne 0 ] ; then
			echo
			echo "### Fatal: 'mkdir $installDirectory' failed."
			echo
			echo "$appName halting ..."
			exit 2
		fi
	else 
		
		#
		# Kill any previously-started e_tokend.
		#

		OLD_PID=`ps $OPTS | grep e_tokend | egrep -v "enl_post|grep" \
			| awk '{ print $'$PIDCOLUMN'; }'`
		if [ ! -z "$OLD_PID" ] ; then
			echo "$appName: Killing a previously-started e_tokend [pid $OLD_PID]"
			kill $OLD_PID
			if [ $? -ne 0 ] ; then
				echo "$appName: Run as user 'root'." 1>&2
				exit $?
			fi
			sleep $SHUTDOWN_SECS
			PIDS_THAT_WOULD_NOT_DIE=`ps $OPTS | grep e_tokend \
				| egrep -v "enl_post|grep" \
				| awk '{ print $'$PIDCOLUMN'; }'`
			if [ ! -z "$PIDS_THAT_WOULD_NOT_DIE" ] ; then
				kill -9 $PIDS_THAT_WOULD_NOT_DIE
				if [ $? -ne 0 ] ; then
					echo "$appName: Run as user 'root'." 1>&2
					exit $?
				fi
			fi
		fi
	fi

	echo
	echo "Extracting the install component into $installDirectory ..."

	TAR_OPTIONS=-xvf

	if [ -f $INSTALL_FILE ] ; then	
		( cd $installDirectory ; tar $TAR_OPTIONS $INSTALL_FILE )
	elif [ -f $INSTALL_FILE.Z ] ; then
		( cd $installDirectory ; zcat $INSTALL_FILE.Z | tar $TAR_OPTIONS - )
	else
		echo
		echo "### Fatal: The install component is missing."
		echo "    Looked at $INSTALL_FILE and $INSTALL_FILE.Z"
		echo
		echo "Please cd to the root directory of the Enlighten CDROM and"
		echo "restart $appName"
		echo
		echo "Exitting.  All partitions left untouched."
		exit 2
	fi

	if [ $? -ne 0 ] ; then
		echo
		echo "### Fatal: Unarchiving $INSTALL_FILE failed."
		echo "    You may want to 'rm -rf $installDirectory' "
		echo "    if you are going to install into a different directory."
		echo
		echo "$appName halting ..."
		echo
		exit 2
	fi
}

#
#  6) setup the log file directory
#  -------------------------------------------------------------------
#

SetupLogs() {
	if [ ! -z "$ENLIGHTEN" -a ! "$ENLIGHTEN" = "$installDirectory" ] ; then
		ENLIGHTEN=$installDirectory
		export ENLIGHTEN
	fi

	if [ ! -d $installDirectory/logs ] ; then
		mkdir $installDirectory/logs
		if [ $? -ne 0 ] ; then
			echo "$appName: Log messages will be echoed to stderr." 1>&2
			echo "$appName: Please mkdir $installDirectory/logs to fix this." 1>&2
		fi
	fi
}


KickOffTheInstallProgram(){
	
	#
	# Check to see that DISPLAY is set properly before popping the gui.
	#
	echo

	if [ $OS = "SUN413" ] ; then
		XNLSPATH="$installDirectory/msg/C"
		export XNLSPATH
	fi

	if [ -x "$installDirectory/bin/$INSTALLER" ] ; then
		if [ -z "$DISPLAY" ] ; then
			echo "Your DISPLAY variable is not set."
			echo "Please set your DISPLAY variable to continue the installation,"
			echo "then invoke $installDirectory/bin/$INSTALLER"
			echo
			exit 2
		fi
		echo "Your DISPLAY variable is set to '$DISPLAY'."
		echo
		echo "Please wait while the Enlighten Network Installer starts up"
		echo "Invoking $installDirectory/bin/$INSTALLER..."
		echo
		exec $installDirectory/bin/$INSTALLER
	else
		echo "Unable to locate the executable $installDirectory/bin/$INSTALLER"
		echo "The install has failed.  Please contact Enlighten Software Support."
		echo
		exit 2
	fi
}

#
#  -------------------------------------------------------------------
#  -------------------------------------------------------------------
#  -------------------------------------------------------------------
#
#                     Main Chunk o' da Script
#
#  -------------------------------------------------------------------
#  -------------------------------------------------------------------
#  -------------------------------------------------------------------
#

# set the default archival directory (where to stash the upgradable preferences)
ARCHIVE_DIR=/tmp/
ARCHIVE_NAME=enlighten_archive.tar

STARTUP_SECS=5
SHUTDOWN_SECS=5

# set the default installation program to be the gui installer
INSTALLER=enl_xinstall

# set the default Enlighten directory
if [ ! -z "$ENLIGHTEN" ] ; then
	DEFAULT_INSTALLDIR=$ENLIGHTEN
else
	DEFAULT_INSTALLDIR=/opt/enlighten
fi

# get the appName
appName=`basename $0`

# get the cdrom directory from $0
partialcdromDir=`dirname $0`
cdromDir=`cd $partialcdromDir ; pwd`

# get command line argument to allow install to non-supported platforms
install_any=0
if [ "$#" -ge 1 ] ; then
	if [ "$1" = "-any" ] ; then
		install_any=1
	fi
fi

# set the default keys database file (normally /etc/enlighten)
if [ -z "$KEY_FILE" ] ; then
	KEY_FILE=/etc/enlighten
fi

USER_ID=`id | tr "\(" "=" | cut "-d=" -f2`

if [ ! $USER_ID = 0 ] ; then
	echo
	echo "### $appName must be run by the 'root' superuser."
	echo "    Please login as root and try again."
	echo
	echo "Exitting.  All partitions left untouched."
	echo
	exit 2
fi

#  1) get the local host's OS type and the pathname to ./platform/
#  -------------------------------------------------------------------

GetHostInfo

#
# Verify that the install component is there, and start sgiinst.sh if
# necessary

VerifyCDROM

#  2) see if there are any previous installations of Enlighten, and
#     get the old installation directory
#  -------------------------------------------------------------------

GetOldInstallDir $*

#  3) prompt for the installation directory from the user
#  -------------------------------------------------------------------

GetNewInstallDir $*

#  4) cp necessary applications to the installation directory
#  -------------------------------------------------------------------

CopyInstallFiles

#  5) store the ENLIGHTEN key into /etc/enlighten
#  -------------------------------------------------------------------

StoreInstallDirIntoEtcEnlighten


#  6) ensure ENLIGHTEN is set and log directory exists
#  -------------------------------------------------------------------

SetupLogs

#  7) exec the proper installer program (cli_installer or gui_installer)
#  -------------------------------------------------------------------

KickOffTheInstallProgram
