#!/sbin/sh
# Author:   Mitch Richling<richmit@member.ams.org>
# IP:       Copyright 1997 by Mitch Richling.  All rights reserverd.
# File:     kerpatch.pl
# Key word: Patch sun solairs compare check
# Notes:    This script is designed to install kernel patchs
#           on Solaris systems.  Kernel patchs require a reboot,
#           and are generally installed at run level 2.
#           * This script is intended to be installed as
#             /etc/rc2.d/S75kpatchme.  This install location can be 
#             changed as long as the variable PATCHMELOC is updated
#             to contain the new file location.
#           * A complete log is put into the file $LOGFILE, which 
#             defaults to /var/adm/kpatch.log
#           * The patch to be installed is assumed to live in the
#             directery pointed to by $DIRTOPATCH.
#           * In each case option, the variables GOODUNAMEV and
#             THEPATCH must be updated for the patch in question.
#           * The lock file is called /var/adm/kerpatch.lock
#           * E-mail is sent to the address in EMAILADDRESS
#
#            

DIRTOPATCH='/data/kerpatch'
LOGFILE='/var/adm/kpatch.log'
PATCHMELOC='/etc/rc2.d/S75kpatchme'
EMAILADDRESS='root'

case "$1" in
'start')
#   Figure out what version of Solaris we are on.
#   Print in each case block to make sure we got the correct one.
	UNAMER=`uname -r`
	echo "Solaris version: $UNAMER" >>$LOGFILE
	echo "Solaris version: $UNAMER"
	case "$UNAMER" in
	'5.5.1')
		echo 'Found patch information for Solaris 5.5.1' >>$LOGFILE
		echo 'Found patch information for Solaris 5.5.1'
		GOODUNAMEV='Generic_103640-34'
		THEPATCH='103640-34'
	;;
	'5.6')
		echo 'Found patch information for Solaris 5.6' >>$LOGFILE
		echo 'Found patch information for Solaris 5.6'
		GOODUNAMEV='Generic_105181-23'
		THEPATCH='105181-23'
	;;
	'5.7')
		echo 'Found patch information for Solaris 5.7' >>$LOGFILE
		echo 'Found patch information for Solaris 5.7'
		GOODUNAMEV='Generic_106541-14'
		THEPATCH='106541-14'
	;;
	'5.8')
		echo 'Found patch information for Solaris 5.8' >>$LOGFILE
		echo 'Found patch information for Solaris 5.8'
		GOODUNAMEV='Generic_108528-04'
		THEPATCH='108528-04'
	;;
	*)
		echo 'Patch information not avaliable' >>$LOGFILE
		echo 'Patch information not avaliable'
		exit 1
	esac
#   Now we see if the correct patch is installed.	
	UNAMEV=`uname -v`
	echo "Kernel patch level: $UNAMEV" >>$LOGFILE
	echo "Kernel patch level: $UNAMEV"
	if [ $UNAMEV = $GOODUNAMEV ] 
	then
		echo 'This box is at the correct patch level'
		echo 'This box is at the correct patch level' >>$LOGFILE
		echo 'I am killing myself'
		echo 'I am killing myself' >>$LOGFILE
		rm $PATCHMELOC
		exit 0
	fi
	echo 'We have to install the kernel patch'
	echo 'We have to install the kernel patch' >>$LOGFILE
#   Check to see if we had a failure last time.
	if [ -f /var/adm/kerpatch.lock ] 
	then
		echo 'ABORT! ABORT! ABORT! Patch install failure detected.'
		echo 'ABORT! ABORT! ABORT! Patch install failure detected.' >>$LOGFILE
		echo `/usr/bin/date` | mailx -s "Patch Abort for "`/usr/bin/hostname` $EMAILADDRESS
		exit 0
	fi
#   Create the lock file for next time.
	touch /var/adm/kerpatch.lock
#   No failure last time, so we continue and install.
#   If we are on 2.5.1 we have to do something different (no patchadd)
	if [ $UNAMER = '5.5.1' ]
	then
		echo 'GRR.. We have to do it without patchadd' >>$LOGFILE
		echo 'GRR.. We have to do it without patchadd'
#   Check if the directory exists.
		if cd $DIRTOPATCH/$THEPATCH
		then			
			./installpatch . >>$LOGFILE
		else
			echo 'Can not find the patches.  Will try later' >>$LOGFILE
			echo 'Can not find the patches.  Will try later'
        fi
	else
		echo 'We get to do it with patchadd' >>$LOGFILE
		echo 'We get to do it with patchadd'
		if [ -d $DIRTOPATCH/$THEPATCH ]
		then			
		/usr/sbin/patchadd $DIRTOPATCH/$THEPATCH >>$LOGFILE
		else
			echo 'Can not find the patches.  Will try later' >>$LOGFILE
			echo 'Can not find the patches.  Will try later'
		fi
    fi
	echo 'Patch install complete' >>$LOGFILE
	echo 'Patch install complete'
	echo 'Reboot'
	/usr/sbin/reboot
	;;
'stop')
	echo 'Nothing to do. bye.' >>$LOGFILE
	echo 'Nothing to do. bye.'
	exit 0
	;;
*)
	echo "Usage: $0 { start | stop }"
	exit 1
esac
exit 0

