#!/bin/sh

## Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
## Use is subject to license terms.

PATH="/usr/bin:/usr/sbin:${PATH}"; export PATH
PKGCOND=/usr/bin/pkgcond

## 
## give everyone a chance to install this patch later
##

time_out() {
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()
{
   kill -9 $child_pid > /dev/null 2>&1

}

trap 'times_up' ALRM
( sleep $SLEEP_TIME ; kill -ALRM $$ > /dev/null 2>&1 ) &
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 > /dev/null 2>&1
  echo KU installation aborted by user interrupt!   
}


trap 'abrt_install; exit 1' INT

echo "Once this KU patch is installed and you have rebooted your system,"
echo "you will not be able to patchrm this patch safely.  Once the NEWBOOT"
echo "support is installed, removing it may cause your system to not"
echo "be bootable."
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 > /dev/null 2>&1
                        exit 1 ;;
                "" | y | yes | Y | Yes | YES)
                        kill -9 $child_pid > /dev/null 2>&1
			break ;;
                *)  echo "yes or no please. \\c"
                        read Response ;;
        esac
done
}



CheckZones()
{
        if [ "$ROOTDIR" = "/" -a -x /usr/bin/zonename ]; then
                ZONENAME=`/usr/bin/zonename`
                if [ ${ZONENAME} = "global" ]; then
                        GLOBAL_ZONE=true
                else
                        GLOBAL_ZONE=false
                fi
        else
                # Unable to determine zone
                GLOBAL_ZONE=true
        fi
}

ExecuteInProperEnvironment () {


   if $PKGCOND is_global_zone > /dev/null 2>&1 ; then
       # In a global zone and system is mounted on /.
	time_out
       return 0
   fi

   return 0
} 

ZONENAME=global
[ -x /sbin/zonename ] && ZONENAME=`/sbin/zonename`

if [ -x "$PKGCOND" ] ; then
   ExecuteInProperEnvironment && exit 0 || exit 1
else
   CheckZones
   if [ "${GLOBAL_ZONE}" = "true" ]; then
        time_out
   fi
fi 
exit 0
