#!/bin/sh
#
#       This script checks for the revision of the Kernel Update
#       installed and takes the necessary action to back out changes
#	made to the devfsadm startup script
#
#

PATID=108528
BEG_REV=05
NOUNDO_SYS_API=07
NOUNDO_SYS_CONFD=18
rmv_all=1

INFO="$PKG_INSTALL_ROOT/var/sadm/pkg/SUNWcsr/pkginfo"
pids=`egrep '^PATCHLIST' $INFO | nawk -F= '{print $2}'`


undo_syseventd() {

fname=devfsadm

sed "s:sysevent/syseventd:devfsadm/devfseventd:"			\
	$PKG_INSTALL_ROOT/etc/init.d/$fname				| \
sed "/^# devfsadmd and rcm_daemon are started on-demand by syseventd/d"	| \
sed "/^# devfsadmd, rcm_daemon and syseventconfd are started on-demand/d" | \
sed "/^# by syseventd\. syseventd should be stopped before others\./d" | \
sed "/\/usr\/sbin\/devfsadm >\/dev\/msglog 2>&1/d"			| \
awk '{print $0} /devfseventd/&&/msglog/					\
	{print "      /usr/lib/devfsadm/devfsadmd >/dev/msglog 2>&1"}'| \
sed "/echo \'Configuring \/dev and \/devices\'/d"  > /tmp/$fname.$$

cp /tmp/$fname.$$ $PKG_INSTALL_ROOT/etc/init.d/$fname
rm /tmp/$fname.$$

return 0
}

undo_sys_api() {

fname=devfsadm

sed "s:-P 1 rcm_daemon:-f /usr/lib/rcm/rcm_daemon:"			\
	$PKG_INSTALL_ROOT/etc/init.d/$fname				| \
sed "s:-P 1 devfsadm:-f /usr/lib/devfsadm/devfsadmd:"			| \
sed "s:-P 1 syseventd:-f /usr/lib/sysevent/syseventd:"			| \
sed "/\/usr\/lib\/sysevent\/syseventconfd/d"				| \
sed "/\/usr\/bin\/pkill -x -u 0 -P 1 syseventconfd/d" > /tmp/$fname.$$

cp /tmp/$fname.$$ $PKG_INSTALL_ROOT/etc/init.d/$fname
rm /tmp/$fname.$$

return 0
}

undo_syseventconfd() {

fname=devfsadm

awk '{print $0} /syseventd/&&/msglog/					\
	{print "      /usr/lib/sysevent/syseventconfd >/dev/msglog 2>&1"}' \
	$PKG_INSTALL_ROOT/etc/init.d/$fname				| \
sed "s:# devfsadmd, rcm_daemon and syseventconfd are started on-demand:\
# devfsadmd and rcm_daemon are started on-demand by syseventd:" | \
sed "/^# by syseventd\. syseventd should be stopped before others\./d" | \
sed "s:-P 1 syseventd:-P 1 1rcm_daemon:"  |\
sed "s:-P 1 rcm_daemon:-P 1 2devfsadm:"  |\
sed "s:-P 1 devfsadm:-P 1 3syseventd:" |\
sed "s:-P 1 1rcm_daemon:-P 1 rcm_daemon:"  |\
sed "s:-P 1 2devfsadm:-P 1 devfsadm:"  |\
sed "s:-P 1 3syseventd:-P 1 syseventd:" > /tmp/$fname.$$

cp /tmp/$fname.$$ $PKG_INSTALL_ROOT/etc/init.d/$fname
rm /tmp/$fname.$$

return 0

}

for i in `echo $pids`; do
	echo $i | egrep -s $PATID || continue
	rev=`expr //$i : '.*-\(.*\)'`

	#No undo edits for rev-18 or higher.
	[ $rev -ge $NOUNDO_SYS_CONFD ] && exit

        #Remove syseventdconf changes for rev-07 and higher but
        #less than rev-17. No undo sys api changes for rev-07 or higher.
	[ $rev -ge $NOUNDO_SYS_API -a \
          $rev -lt $NOUNDO_SYS_CONFD ] && undo_syseventconfd && exit 

	#Remove syseventd and syseventdconf changes for -05 higher.
	[ $rev -ge $BEG_REV ] && no_sys_api=1 && continue

	#Remove all changes if highest rev is less than -05.
	[ $rev -le 04 ] && rmv_all=1 && continue
done

[ "$no_sys_api" ] && undo_syseventconfd && undo_sys_api && exit
[ "$rmv_all" ] && undo_syseventconfd && undo_sys_api && undo_syseventd && exit

exit 0
