#! /bin/sh
#
# 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 " "
echo "dhcpagent supports a new keyword in /etc/default/dhcpagent. "
echo "Please merge the existing /etc/default/dhcpagent with the "
echo "patched version of /etc/default/dhcpagent shipped with this "
echo "patch."
echo " "
echo


kill_powerd() {  


# Check if powerd is running.  If so, kill the process.
# If kill is not successful, generate a warning.
# Bypass kill if not on client.

if [ "$ROOTDIR" = "/" ]
then

    pkginfo -q SUNWpmu > /dev/null 2>&1
    pmu_sys=$?
    
    [ $pmu_sys -ne 0  ] && exit 0 
    
    CHK_POWERD=`pgrep -x powerd`
    echo ""
    echo "If /usr/lib/powerd/powerd is running, it will be stopped before "
    echo "patch installation can commence."
    echo ""
    
    if [ -n "$CHK_POWERD" ]
    then
         kill $CHK_POWERD
         sleep 10
         CHK_KILL=`pgrep -x powerd`
         if [ -n "$CHK_KILL" ]
         then
           echo
           echo "Warning:  Kill powerd may have failed.  See patch README."
           echo
         fi
    fi

fi

}


#
# 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_powerd
   exit 0
}

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

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)
                         
			# Be sure to kill powerd before install.
			# Per bugid: 4489520 

			kill_powerd

                        kill -9 $child_pid
                        exit 0 ;;
                *)  echo "yes or no please. \\c"
                        read Response ;;
        esac
done

