#!/bin/sh
#


install_patches () {
	# Check for Patch to install
	NEED_PATCHES_CHECK=1;
	if [ -z ${MANUAL_PATCH:-""} -a -f /usr/tmp/patchslst ]; then
		versions -b | grep Patch > /usr/tmp/patchslst.new
		diff -q /usr/tmp/patchslst.new /usr/tmp/patchslst > /dev/null
		DIFF_EXIT_STATUS=$?;
		if [ ${DIFF_EXIT_STATUS} -eq 0 ];  then
			# The system look the same 
			# Maybe the distribution changed.....
			if [ -f /usr/tmp/patchsdir ]; then
				ls -1R ${distpath}/Patches | grep patch | grep -v README | cut -c11-14 | sort | uniq >  /usr/tmp/patchsdir.new
				diff -q /usr/tmp/patchsdir.new /usr/tmp/patchsdir > /dev/null
				DISTDIR_EXIT_STATUS=$?;
				if [ ${DISTDIR_EXIT_STATUS} -eq 0 ]; then
					NEED_PATCHES_CHECK=0;
					echo "Hmmm... Looks like you have all the required patches"
					echo "BRILLIANT"
				fi
			fi 
		fi
	fi

	NEED_REBOOT=0
	if [ ${NEED_PATCHES_CHECK} -eq 1 ]; then
		echo "Please wait, The Install program is checking for required patches"
		echo "This can take a while..."
		./INSTALL_PATCH ${distpath}
		EXIT_STATUS=$?;
		if [ ${EXIT_STATUS} -eq 0 -o ${EXIT_STATUS} -eq 4 ]; then 
			# Reboot need patch install
			NEED_REBOOT=1
		elif  [ ${EXIT_STATUS} -eq 1 ]; then 
			# All goes well and no reboot is needed
			NEED_REBOOT=0	
		elif [ ${EXIT_STATUS} -eq 5 ]; then
			NEED_REBOOT=0
         # We should pause here so they can see the message
         echo -n "**** Please Press <ENTER> to continue INSTALL"
         read tt
		elif [  ${EXIT_STATUS} -gt -a ${EXIT_STATUS}  -lt 4 ]; then
			# Something went wrong we can not continue	
			echo "problem with patch install"
			echo "INSTALL program aborted"
			exit 1;
		fi
		versions -b | grep Patch > /usr/tmp/patchslst
		ls -1R ${distpath}/Patches | grep patch | grep -v README | cut -c11-14 | sort | uniq >  /usr/tmp/patchsdir
		echo "Done installing patches."
	fi

	if [ ${NEED_REBOOT} -eq 1 ]; then 
		user_confirm -t "Patches Installation done:" -t "You must reboot and restart INSTALL" -B OK
		/etc/reboot
	fi
}

function user_confirm {
    if [ ${DISPLAY:-""} != "" ]; then
		  xconfirm "$@";
	 else
	     MSG=" ";
		  while [ "${1:-""}" != "" ]; do
				case "${1}" in
				 -t) shift
				     MSG="$MSG $1"
					  shift
                 ;;
				  *) shift
					  ;;
		       esac
		  done
		  echo ${MSG}
		  read tt
    fi
	 return
}



####################
# Begin of the Main
#

#
# Ensure that the /var/inst
# directory is writable by
# the current user.
#
if [ ! -w /var/inst ]; then
	echo "You must be super-user to install the software.";
	exit;
fi 

#
# Parameter processing
#
case ${1:-""} in 
	-nopat*) MANUAL_PATCH=0;
		  ;;
	-forcepat*) MANUAL_PATCH=1;
		  ;;
esac

#
# Using X display OR text...
#
if [ ${DISPLAY:-""} != "" ]; then
   INSTPRGM=swmgr
else
	INSTPRGM="inst -a";
fi

#
# Distribution Path finding
#
distpath=$PWD

# be smart and look for architecture
ipnum=`/sbin/uname -m`
if [ -d $ipnum ]; then
	distpath=$PWD/$ipnum
	if [ -d ${ipnum}/dist ]; then
		distpath=$PWD/${ipnum}/dist
	fi
fi

# be smart & look for dist subdirectory
if [ -d dist ]; then
	distpath=$PWD/dist
fi

# check for OS dependant stuff
osver=`/sbin/uname -r`
if [ -d $osver ]; then
	distpath=${PWD}/${osver}
fi

#
# Let's make sure the kernel is OK before we start
#
./INSTALL_PATCH KERNEL_CHECK_ONLY
if [ $? -ne 0 ]; then
	echo "INSTALL aborted"
	exit 1
fi
#
# Install Patches ....
#
if [ ${MANUAL_PATCH:-1} -ne 0 ]; then
	install_patches
fi

#
# Softwares install
#
if [ $osver = "6.2" ]; then
	${INSTPRGM} -Vbackground:false -Vshow_log_pane:true -Vstartup_script:execute -Vinteractive:false -Vpost_install_dialog:false -Vautomatic:true -f $distpath
else
	${INSTPRGM} -Vbackground:false -Vshow_log_pane:true -Vstartup_script:execute -Vinteractive:false -Vpost_install_dialog:false -f $distpath
fi
#
# Discreet Logic Internal packages
nostrip="${distpath}/../`basename $distpath`.NoShip"
if [ -d $nostrip ]; then
	echo "Installing DL internal packages"
	inst -a -f $nostrip
else
	user_confirm -t "Installation complete:" -t "    You must reboot" -B OK
	/etc/reboot
fi
