#!/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 ${PATCH_DISTPATH} | 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 
                        # was successful, need to reboot.
			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 ${PATCH_DISTPATH} | 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 "  Machine will reboot and you must restart INSTALL" -t "  Hit REBOOT to proceed" -B REBOOT 
		/etc/reboot
                exit 0
	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
}


install_packages () {

PACKAGES_OS_VERSION=`uname -R | cut -d' ' -f2  | sed 's/ //g'`

# We have to select the proper packages by reading the
# the package list and creating symbolic link in a 
# platform sub-folder
PLATFORM_PACKAGE_DIR=/tmp/.installpackage.links.$$     

# Packages in a sub-directory for the os version + a common dir.
for PACKAGES_DISTPATH in ${distpath}/Packages/${PATCH_OS_VERSION} ${distpath}/Packages/common;
do
  if [ -d ${PACKAGES_DISTPATH} ]; then
    # check if a package list file exists
    if [ -e ${PACKAGES_DISTPATH}/${PACKAGE_FILE_LIST} ]; then
      echo "Checking for Packages to install in ${PACKAGES_DISTPATH}"

      # Re-create directory if needed.
      rm -rf ${PLATFORM_PACKAGE_DIR}
      mkdir -p  ${PLATFORM_PACKAGE_DIR}

      # read <platform>.list file
      PACKAGE_LIST=`cat ${PACKAGES_DISTPATH}/${PACKAGE_FILE_LIST}`
   
      # for every package listed in the file, 
      # create a symbolic link in a subdirectory
      # for this platform. 
      for PACKAGE_NAME in ${PACKAGE_LIST};
      do 
        if [ -e ${PACKAGES_DISTPATH}/${PACKAGE_NAME} ];then
          ln -s ${PACKAGES_DISTPATH}/${PACKAGE_NAME} ${PLATFORM_PACKAGE_DIR}
          ln -s ${PACKAGES_DISTPATH}/${PACKAGE_NAME}.* ${PLATFORM_PACKAGE_DIR}
        else
          echo "Error: Could not find package ${PACKAGE_NAME}"
        fi
      done
    fi
  fi
done

if [ -d ${PLATFORM_PACKAGE_DIR} ]; then
  # Check if we really have stuff to install (dryrun mode of inst)
  PLATFORM_INST_LOG=/tmp/.installpackage.instlog.$$ 
  inst -Vinteractive:false -Vverbosity:0 -a -n -f ${PLATFORM_PACKAGE_DIR} > ${PLATFORM_INST_LOG}
  PACKAGE_SOMETHING_TO_INSTALL=`grep "Installations and removals were successful" ${PLATFORM_INST_LOG}`
  
  if [ ${#PACKAGE_SOMETHING_TO_INSTALL} != 0 ]; then
    # Double check if kernel is in good state.	
	 ./INSTALL_PATCH KERNEL_CHECK_ONLY
	 if [ $? -ne 0 ]; then
	   echo "INSTALL aborted"
	   exit 1
	 fi
						
    # Let's do it
    PLATFORM_INST_LOG=/tmp/.installpackage.instlog.$$ 
    inst -Vinteractive:false -Vverbosity:0 -a -f ${PLATFORM_PACKAGE_DIR} | tee ${PLATFORM_INST_LOG}

    PACKAGE_CONFLICT=${PACKAGE_CONFLICT}"`grep -i 'conflicts' ${PLATFORM_INST_LOG}`"
    PACKAGE_ERRORS=`grep ERROR ${PLATFORM_INST_LOG}`
    PACKAGE_INSTALLED=`grep "Installations and removals were successful" ${PLATFORM_INST_LOG}`
    if [ ${#PACKAGE_ERRORS} != 0 ]; then
      echo
      echo "Error with selected packages"
      echo ${PACKAGE_ERRORS}
      echo
    else
      if [ ${#PACKAGE_INSTALLED} != 0 ]; then
        if [ ${PACKAGE_REBOOT_ALLOWED} -eq 0 ]; then
		    # Wait for vbob to come back 
			 echo "Sleeping 30 seconds..."
			 sleep 30
		  else
          user_confirm -t "SGI Packages Installation done:" -t "  Machine will reboot and you must restart INSTALL" -t "  Hit REBOOT to proceed" -B REBOOT 
          /etc/reboot
          exit 0
        fi
      fi
    fi
  fi
fi 

}


######################################################################
# 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

# directory for patches
PATCH_OS_VERSION=`uname -R | cut -d' ' -f2  | sed 's/ //g'`
if [ -d ${distpath}/Patches/${PATCH_OS_VERSION} ]; then
  # New scheme, patches in a sub-directory for the os version.
  PATCH_DISTPATH=${distpath}/Patches/${PATCH_OS_VERSION}
else
  PATCH_DISTPATH=${distpath}/Patches
fi

PATCH_PRESENT=""
if [ -d ${PATCH_DISTPATH} ]; then
    PATCH_PRESENT=`ls ${PATCH_DISTPATH} | head -1`
fi


# Before we actually install anything, work around a problem in IRIX 6.5.16f
# with IR4 graphics, where /var/inst/machfile is missing an entry for
# IR4. Without this entry, IR4 graphics is not recognized by inst, so
# any packages which are conditionally installed on the type of graphics
# (DLvfo for instance) will not install correctly.
# Although only necessary on machines with IR4 graphics, it doesn't hurt
# to add the line everywhere. This code will become redundant (and
# won't be executed) once we go past IRIX 6.5.16f.
if ! grep -q '7     23    -1    -1       0     KONA' /var/inst/machfile ; then
sed '/7     22    -1    -1       0     KONA/a\
7     23    -1    -1       0     KONA' /var/inst/machfile > /tmp/newmachfile
cp /var/inst/machfile /var/inst/machfile.orig
cp /tmp/newmachfile /var/inst/machfile
fi


######################################################################
# Figure out running platform
######################################################################

GRAPHICS=`hinv -c graphics|sort|uniq`
GRAPHICS_VPRO=`echo $GRAPHICS | grep "Graphics board: V1"`
BOARD_ONYX350=`hinv -c processor -m | grep 2U_INT_53`
IPNUM=`uname -m`
case $IPNUM in
  IP32)
    echo "Platform $IPNUM is NOT supported"
    exit -1
    ;;

  IP27)
    CURRENT_PLATFORM="onyx2"
    ;;

  IP30)
    if [ ${#GRAPHICS_VPRO} != 0 ]; then
      CURRENT_PLATFORM="octane2"
    else
      CURRENT_PLATFORM="octane"
    fi
    ;;

  IP35)
    if [ ${#GRAPHICS_VPRO} != 0 ]; then
      CURRENT_PLATFORM="tezro"
    else
      if [ ${#BOARD_ONYX350} != 0 ]; then
        CURRENT_PLATFORM="onyx350"
      else
        CURRENT_PLATFORM="onyx3"
      fi
    fi
    ;;

  *) 
    echo "ERROR unknown platform: $IPNUM"
    CURRENT_PLATFORM="unknown"
    ;;

esac


######################################################################
# Packages (non-discreet packages)
# Done in two passes (pass #1, reboot, pass #2), mainly
# for the vbob package which requires ml to be up and running.
######################################################################

PACKAGE_FILE_LIST=${CURRENT_PLATFORM}.list
PACKAGE_REBOOT_ALLOWED=1
install_packages

# Second pass (vbob).  No reboot, since it will be done after
# patch installation anyway.
PACKAGE_FILE_LIST=${CURRENT_PLATFORM}.list.secondpass
PACKAGE_REBOOT_ALLOWED=0
install_packages


######################################################################
# Patches are installed after Packages (and a reboot), 
# but before the software.
#
# Example: the vbob patch needs to have the mldaemon up and running.
######################################################################
#
# Let's make sure the kernel is OK before we start
#
if [ ${#PATCH_PRESENT} -gt 0 ]; then
    ./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
fi


######################################################################
# Softwares install
######################################################################

nostrip="${distpath}/../`basename $distpath`.NoShip"

if [ -d $nostrip ]; then
   # we only want to rqsall once so if we are going to rqsall after the
   # internal packages lets skip it after the release packages.
   SKIPRQSALL="-Vskip_rqs:on"
fi

${INSTPRGM} ${SKIPRQSALL} -Vbackground:false -Vshow_log_pane:true -Vstartup_script:execute -Vinteractive:false -Vpost_install_dialog:false -f $distpath


#
# Discreet Logic Internal packages
#
if [ -d $nostrip ]; then
	echo "Installing DL internal packages"
	inst -a -f $nostrip
fi


######################################################################
# Reboot if needed.
######################################################################

if [ -f /unix.install ]; then
	user_confirm -t "Installation complete:" -t "   About to reboot" -t "   Hit REBOOT to proceed" -B REBOOT 
	/etc/reboot
        exit 0
else
	user_confirm -t "Installation complete:" -t "   No reboot is neccessary"
fi
