#! /bin/sh
#
# Copyright (c) 1996-2003 by Sun Microsystems, Inc.
# All rights reserved.
#

###########################################################################
#
# postbackout
#
#	* Update ${ROOTDIR}/etc/inetd.conf for SUNWscu daemons
#	* Update ${ROOTDIR}/etc/rpc        for SUNWscu daemons
#	* Update ${ROOTDIR}/etc/services   for SUNWscu daemons
#	* Update ${ROOTDIR}/var/spool/crontabs/root for SUNWscu log rotation.
#
###########################################################################

PATH=/usr/bin:/usr/sbin
export PATH

PKGNAME=SUNWscu
STARTTAG="Start of lines added by ${PKGNAME}"
  ENDTAG="End   of lines added by ${PKGNAME}"
PROG=${PKGNAME}.postinstall

TMPFILES=$ROOTDIR/etc/rcS.d/S68did_update_vfstab
CLPRIVNET_TMPFILE=/tmp/rem_drv_output.$$

###########################################
#
# Cleanup any temporary files on exit/interrupt
#
###########################################
cleanup()
{
	exit_code=$1

	if [ "${TMPFILES}" ]; then
		rm -f ${TMPFILES}
	fi

        if [ -f "${CLPRIVNET_TMPFILE}" ]; then
                rm -f "${CLPRIVNET_TMPFILE}"
        fi

	exit ${exit_code}
}

###########################################
#
# remove_SUNWscu_edits <filename>
#
#	Backout all lined from the given ascii <filename>
#	indicated by the SUNWscu comment tag lines.
#
###########################################
remove_SUNWscu_edits()
{
	update_file=$1

	while :
	do
		grep "${STARTTAG}" ${update_file} >/dev/null 2>&1
		case $? in
		2)	# error reading file
			echo "${PROG}:  error reading ${update_file}"
			return 1
			;;

		0)	# grep found the string, so get rid of the entries
			ed -s ${update_file} << EOF >/dev/null 2>&1
/${STARTTAG}/,/${ENDTAG}/d
w
q
EOF
			if [ $? -ne 0 ]; then
				echo "${PROG}: problem updating ${update_file}"
				return 1
			fi
			;;

		*)
			break
			;;
		esac
	done

	return 0
}

###########################################
#
# add entries to /etc/inet/inetd.conf
#
###########################################
update_etc_inetd_conf()
{
	update_file=${ROOTDIR}/etc/inet/inetd.conf

	# remove the old entries
	remove_SUNWscu_edits ${update_file}

	# add the new entries to the copy
cat << EOF >> ${update_file}
# ${STARTTAG}
100145/1 	tli	rpc/circuit_v	wait root /usr/cluster/lib/sc/rpc.scadmd	rpc.scadmd
100151/1 	tli	rpc/circuit_v	wait root /usr/cluster/lib/sc/rpc.sccheckd	rpc.sccheckd -S
# ${ENDTAG}
EOF

	if [ $? -ne 0 ]; then
		echo "${PROG}: unable to update ${update_file}"
		return 1
	fi

	return 0
}

###########################################
#
# add entries to /etc/rpc
#
###########################################
update_etc_rpc()
{
	update_file=${ROOTDIR}/etc/rpc

	# remove the old entries
	remove_SUNWscu_edits ${update_file}
	
	# add the new entries to the copy
cat << EOF >> ${update_file}
# ${STARTTAG}
scadmd		100145
sccheckd	100151
# ${ENDTAG}
EOF

	if [ $? -ne 0 ]; then
		echo "${PROG}: unable to update ${update_file}"
		return 1
	fi

	return 0
}

###########################################
#
# add entries to /etc/services
#
###########################################
update_etc_services()
{
	update_file=${ROOTDIR}/etc/services

	# remove the old entries
	remove_SUNWscu_edits ${update_file}
	
	# add the new entries to the copy
cat << EOF >> ${update_file}
# ${STARTTAG}
pnmd		6499/tcp			# Sun Cluster pnm daemon
# ${ENDTAG}
EOF

	if [ $? -ne 0 ]; then
		echo "${PROG}: unable to update ${update_file}"
		return 1
	fi

	return 0
}

###########################################
#
# add entries to /kernel/drv/log.conf to turn on
# Solaris message id logging.
#
###########################################
update_kernel_drv_log_conf()
{
	update_file=${ROOTDIR}/kernel/drv/log.conf

	# remove the old entries
	remove_SUNWscu_edits ${update_file}

	# add the new entries to the copy
cat << EOF >> ${update_file}
# ${STARTTAG}
msgid=1;
# ${ENDTAG}
EOF

	if [ $? -ne 0 ]; then
		echo "${PROG}: unable to update ${update_file}"
		return 1
	fi

	return 0
}

###########################################
#
# sig_hup_inetd - ask inetd to reread conf file
#	if we're not in a jumpstart environment
#
#	always return success regardless of
#	pkill's return code; don't want to abort
#	install just for inetd HUP failure
#
###########################################
sig_hup_inetd()
{

	# tweak inetd only if we're not jumpstarting
	if [ "${ROOTDIR}" = "/" ]
	then
		/usr/bin/pkill -HUP -x -u 0 -P 1 inetd
		status=$?
		# require exact match on 'inetd', user root, ppid 1

		if [ $status -ne 0 ]
		then
			echo "${PROG}:  unable to restart inetd."
		fi
	fi
	return 0
}

###########################################
#
# Remove our updated to root's crontab file
# since SC 3.1 U1.
#
# The assumption is that there we had no entries
# prior to SC 3.1 U1 so we just remove 
# our entries without adding anything back.
# 
#
###########################################
update_root_crontab()
{
	update_file=${ROOTDIR}/var/spool/cron/crontabs/root

	# remove the old entries
	remove_SUNWscu_edits ${update_file}

	if [ $? -ne 0 ]; then
		echo "${PROG}: unable to update ${update_file}"
		return 1
	fi

	return 0
}

###########################################
#
# remove clprivnet driver 
#
# This function is executed here rather than in preremove to support
# patch removal as well as package removal.
#
###########################################
remove_clprivnet_driver()
{

#
# If the clprivnet driver still exists (the previous version) then 
# rem_drv should not be done.
#
if [ -f "${ROOTDIR}/kernel/drv/clprivnet" ]; then
	return 0
fi

#
# The rem_drv command may output to stderr but continue on successfully.
# Stderr output is displayed here only in the event of an error exit.
# 
/usr/sbin/rem_drv  clprivnet 2> "${CLPRIVNET_TMPFILE}"

# report any errors and print output collected from stderr
if [ $? -ne 0 ]; then
	echo "${PROG}: rem_drv of clprivnet driver failed"
	if [ -f "${CLPRIVNET_TMPFILE}" ]; then
		echo "rem_drv stderr output:"
		cat "${CLPRIVNET_TMPFILE}"
	fi
	return 1
fi

return 0
}

###########################################
#
# main
#
# Up to U1 FCS version of this patch, the undo
# is to effectively put modified files back to
# the way they were modified at SC 3.1 FCS.
# This is partly accomplished by executing
# the same changes as SUNWscu/postinstall
#
###########################################
errs=0

# catch common signals
trap 'echo "${PROG}: caught signal";  cleanup 3' 1 2 3 15

###########################################
#
# Undo SUNWscu/postinstall changes
#
# Up to U1 FCS version of this patch, the undo
# is to effectively put modified files back to
# the way they were installed by the SUNWscu/postinstall
# script from SC 3.1 FCS (SUNWscu/postinstall 1.10).
# So we'll execute the same actions as were done in
# that rev of the postinstall script.
#
# There is an additional change in that we need to
# remove the root crontab entry made for 
# newclevent to rotate event logs. This was added
# to SUNWscu/postinstall 1.11.
#
###########################################
remove_clprivnet_driver    || errs=`expr ${errs} + 1`
update_etc_inetd_conf  	   || errs=`expr ${errs} + 1`
update_etc_rpc	  	   || errs=`expr ${errs} + 1`
update_etc_services  	   || errs=`expr ${errs} + 1`
update_kernel_drv_log_conf || errs=`expr ${errs} + 1`
update_root_crontab    	   || errs=`expr ${errs} + 1`
sig_hup_inetd		   || errs=`expr ${errs} + 1`

if [ ${errs} -ne 0 ]; then
	cleanup 1
else
	cleanup 0
fi
