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

#  Remove blank lines from $ROOTDIR/var/sadm/pkg/SUNWcsr/install/space.  
#  Although the space file is only used during initial SunOS installation, 
#  presence of blank lines interfere with installation of SUNWcsr patches.
#

Date=`date +%m%d%y%M`
if test -f $ROOTDIR/var/sadm/pkg/SUNWcsr/install/space
then
    cd $ROOTDIR/var/sadm/pkg/SUNWcsr/install
       if `ls space.* > /dev/null 2>&1 `; then
       rm space.* 
       fi
    sed '/^$/d' space > space.$Date
    cp -p space.$Date space
fi

#
# Check if NewsPrint 2.5 is installed.  If it is, print message
# informing user that patch 102113 is required.
#

vers=`pkgparam -R $ROOTDIR SUNWsteNP VERSION 2>/dev/null`
if test $vers
then
	if test $vers = "2.5"
	then
		echo "Systems running NeWSprint 2.5 should also apply"
		echo "the NeWSprint patch 102113 if you are printing on"
		echo "a NeWSprint printer.  Patch 102113 prevents a hang"
		echo "from occurring when printing."
	fi
fi
echo
echo

# This prepatch script checks to see if a patch is installed. If it is it
# returns harmlessly. If it isn't installed and is found listed as a required
# patch then it reconstructs the PATCH_INFO_<patchid> line by removing the
# target patch.  This check is needed on starfire systems since it causes the
# KU (this patch) not to back out.

PATH=/usr/bin
export PATH
umask 022
PATCH=105492-02
PKGDIR=$ROOTDIR/var/sadm/pkg
PATTERN="PATCH_INFO.*=Installed:.*Requires:.*"$PATCH""
PATCHDB=$ROOTDIR/var/sadm/patch/.patchDB
PKGLIST="SUNWcsu SUNWarc SUNWhea SUNWcsr"

replacePatchEntry() {
	pkginfo=$1

	grep -v $PATTERN $pkginfo > "$pkginfo"_$$
	grep $PATTERN $pkginfo |
		sed 's/'"$PATCH"' //g' >> "$pkginfo"_$$
	ln $pkginfo "$pkginfo"_mvd_$$
	mv "$pkginfo"_$$ $pkginfo
	rm -f "$pkginfo"_mvd_$$
}

# If SUNWcg6 is installed then don't execute this script.
pkginfo -q -R $ROOTDIR SUNWcg6
if [ $? != 0 ] ; then

	cd $PKGDIR

	for pkg in $PKGLIST; do
		# See if $PATCH exists in any Requires entry
		if grep $PATTERN $pkg/pkginfo > /dev/null; then
			replacePatchEntry $pkg/pkginfo
		fi
	done

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()
{
   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
}


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


