#! /bin/sh 

#
# For Serengeti and Lightweight8 Check Firmware Level
#
platform=`/usr/bin/uname -i` 

case $platform in
    "SUNW,Sun-Fire" )
    # For Serengeti:
    # A) Firmware Major number must be 5 or greater.
    #
    # B) Firmware Minor number must be 14 or higher.
    #    If Firmware Minor number is 14, the Rev number must be 4 or 
    #    greater.
    major_num=`/usr/sbin/prtconf -V | nawk -F" " '{print $2}' | nawk -F. '{print $1}'`
    minor_num=`/usr/sbin/prtconf -V | nawk -F. '{print $2}'`
    rev_num=`/usr/sbin/prtconf -V | nawk -F. '{print $3}' | nawk -F" " '{print $1}'`

    if [ $major_num -gt "5" ] || ([ $major_num -eq "5" ] && [ $minor_num -gt "14" ]) || ([ $major_num -eq "5" ] && [ $minor_num -eq "14" ] && [ $rev_num -ge "4" ]); then 
      echo ""
      echo "Correct Serengeti firmware version detected.  Begin patch installation ... "
      echo ""  
    else
        echo ""
        echo "The target system does not have the correct firmware that is needed"
        echo "to install this patch.  Please install firmware version 5.14.4 "
        echo "or greater before attempting this installation." 
        echo ""
        exit 1
    fi
    ;;
    "SUNW,Netra-T12" )
    # For Lightweight8:
    # A) Firmware Major number must be 5 or greater.
    #
    # B) Firmware Minor number must be 13 or higher.
    #    If Firmware Minor number is 13, the Rev number must be 0013 or 
    #    greater.
    major_num=`/usr/sbin/prtconf -V | nawk -F" " '{print $2}' | nawk -F. '{print $1}'`
    minor_num=`/usr/sbin/prtconf -V | nawk -F. '{print $2}'`
    rev_num=`/usr/sbin/prtconf -V | nawk -F. '{print $3}' | nawk -F" " '{print $1}'`

    if [ $major_num -gt "5" ] || ([ $major_num -eq "5" ] && [ $minor_num -gt "13" ]) || ([ $major_num -eq "5" ] && [ $minor_num -eq "13" ] && [ $rev_num -ge "13" ]); then 
      echo "" 
      echo "Correct Netra firmware version detected.  Begin patch installation ..."  
      echo ""
    else
        echo ""
        echo "The target system does not have the correct firmware that is needed"
        echo "to install this patch.  Please install firmware version 5.13.0013 "
        echo "or greater before attempting this installation." 
        echo ""
        exit 1
    fi
    ;;
esac

#
# Script to check that the system is at a quiet state and warn the installer
# about memory requirements.  
#
# Check what the run level is.  If run level is not "1" then print message.
# Message will not be displayed for diskless client configurations.

PKG_INSTALL_ROOT=$ROOTDIR
if test $PKG_INSTALL_ROOT = "/"
then
	who -r > /tmp/run$$
	awk '{if (int($3) > 1){
       		print ("\n \n")
        	print ("If possible, perform patch installation in single user mode.")
        	print ("If this can not be done, we recommend having the system in as")
        	print ("quiet a state as possible: no users logged on, no user jobs")
        	print ("running.")} }' /tmp/run$$

fi 

echo
echo
#
# Asks user is they would like to continue.  It will timeout in
# SLEEP_TIME and installation will continue if no response is given.
#

SLEEP_TIME=60
times_up()
{
   exit 0
}

trap 'times_up' ALRM
( sleep $SLEEP_TIME ; kill -ALRM $$ ) &
child_pid=$!

# Set up signal trap before reading user's response.  This command
# will trap a cntl-C that is issued by the user and abort the 
# installation It will also kill the child process that was created 
# by the timesup() function.  The INT symbolic name translates into 
# signal 2.

abrt_install()
{
  kill -9 $child_pid
  echo KU installation aborted by user interrupt!   
}


trap 'abrt_install; exit 1' INT

echo "Do you wish to continue this installation {yes or no} [yes]? \\c"
echo "\n(by default, installation will continue in $SLEEP_TIME seconds)"

read Response
while [ 1 ]
do
        case $Response in
                n | no | N | NO | No)
                        kill -9 $child_pid
                        exit 1 ;;
                "" | y | yes | Y | Yes | YES)
                        kill -9 $child_pid
                        exit 0 ;;
                *)  echo "yes or no please. \\c"
                        read Response ;;
        esac
done
