### BEGIN: walk through packages

### VARIABLE DEFINITIONS ###

# PLEASE NOTE: You can add packages this script should walk through to PKG_LIST
# variable. However, remember to create appropriate process_<pkg_name> function
# under PKG PROCESSING section.

PKG_LIST="SUNWsmmgr"
RemovalList=""
TempFile="${ROOTDIR}/tmp/tmp.${PatchNum}.$$"


### COMMON FUNCTIONS ###

#Get PKGSAV environment variable set as it would be by pre/post-install/remove.
#Call this function with package name as its parameter.
def_pkgsav()
{
	PKGSAV="`echo ${ROOTDIR}/var/sadm/pkg/${1}/save \
		| sed -e 's#\/\/#\/#'`"
}

### PKG SPECIFIC FUNCTIONS - SUNWsmmgr ###

nothing_changed()
{
	if test ! -f "${1}"; then
		return 0;
		else return 1; fi
}

mk_removal_list()
{
	if test -z "${RemovalList}"; then
		RemovalList="^${1}\$"
		else RemovalList="${RemovalList}|^${1}\$"; fi
}

### PKG PROCESSING ###

#With postinstall we have modified snmpd.conf file and saved list of modified 
#lines into a file in PKGSAV areas. Now we want to remove these line when 
#backing out the patch as there would be warnings at evert snmpd stard 
#otherwise.
process_SUNWsmmgr()
{
	def_pkgsav ${PKG_NAME}
	SNMPD_CONF="`echo ${ROOTDIR}/etc/sma/snmp/snmpd.conf \
		| sed -e 's#\/\/#\/#'`"
	SavedChangesFile="${PKGSAV}/snmpd.conf-added${PatchNum}"
	BackupFile="${SNMPD_CONF}-post${PatchNum}_$$"

	if nothing_changed "${SavedChangesFile}"; then
		return 0; fi

#Following is quite uggly, but it is necessary since sh forks into subshell
#for while/until cycles.
	IFS='
'	#This is actual newline for IFS!!! 
        for LineToRemove in `cat < "${SavedChangesFile}"`; do
		mk_removal_list "${LineToRemove}"; done
	#We need to reset IFS. There is one more new line!
	IFS=' 	
'
	
	echo -n "" > "${TempFile}" && \
		chmod 0600 "${TempFile}" && \
		chgrp bin "${TempFile}" && \
		egrep -v "${RemovalList}" "$SNMPD_CONF" > "${TempFile}"
	if test "$?" -eq "0" ; then
		echo ""
		echo "Backup copy of your:"
		echo "${SNMPD_CONF}"
		echo "was saved as:"
		echo "${BackupFile}"
		echo ""
		mv "${SNMPD_CONF}" "${BackupFile}"
		mv "${TempFile}" "${SNMPD_CONF}"
		rm "${SavedChangesFile}"
	fi



	#We also want to remove changes introduced into zones
	#created after patch installation
	#Postbackout is not called for such zones, so we need to work around 
	#this. :(

	ZoneIndex="${ROOTDIR}/etc/zones/index"

	if test -f "${ZoneIndex}"; then
	for zPath in `cat ${ZoneIndex} | egrep -v '^( *#|global:)' | cut -d ':' -f 3`; do 
		zSNMPD_CONF=${zPath}/root/etc/sma/snmp/snmpd.conf
		zSNMPD_CONF_Bck="${zSNMPD_CONF}-post${PatchNum}_$$"

		#We also want to remove changes introduced into zones
		#created after patch installation
		test -f "${zSNMPD_CONF}" \
			&& grep "#BEGIN: These lines were introduced by ${PatchNum}" ${zSNMPD_CONF} >/dev/null 2>&1 \
			&& grep "#BEGIN: and will be removed on patchrm ${PatchNum}" ${zSNMPD_CONF} >/dev/null 2>&1 \
			&& grep "#END: These lines were introduced by ${PatchNum}" ${zSNMPD_CONF} >/dev/null 2>&1
		if test "$?" -eq "0" ; then
			cp -p "${zSNMPD_CONF}" "${zSNMPD_CONF_Bck}"
			ed "${zSNMPD_CONF}" <<-EOT >/dev/null 2>&1
				/#BEGIN: These lines were introduced by ${PatchNum}/,/#END: These lines were introduced by ${PatchNum}/ d
				w
				q
			EOT
		fi
	done
	fi



	#Put postinstall and pkgmap back in place
	PkgMap=${PKGSAV}/pspool/SUNWsmmgr/pkgmap
	PostinstallFile=${PKGSAV}/pspool/SUNWsmmgr/install/postinstall
	PostinstallFile_Bck=${PKGSAV}/pspool/SUNWsmmgr/install/postinstall-pre${PatchNum}

	if test -f "${PostinstallFile_Bck}" ; then
		mv -f ${PostinstallFile_Bck} ${PostinstallFile}

        	PostinstallSum="`/usr/bin/sum ${PostinstallFile} | awk '{print($1);}'`"
        	PostinstallSize="`/usr/bin/wc -c ${PostinstallFile} | awk '{print($1);}'`"
		OldLine="`grep '^[0-9] i postinstall [0-9]* [0-9]* [0-9]*' ${PkgMap}`"
		NewLine="`echo ${OldLine} | sed -e \"s/^\([0-9] i postinstall\) [0-9]* [0-9]* \([0-9]*\)/\1 ${PostinstallSize} ${PostinstallSum} \2/\"`"
		ed ${PkgMap} <<-EOT >/dev/null 2>&1
			/${OldLine}/
			c
			${NewLine}
			.

			w
			q
		EOT
	fi


	#We also need to restore snmpd.conf in pspool as it gets overwiten by
	#system one on backout and we get duplicate entries dumped there by
	#original postinstall

	PspoolSnmpdConf="${PKGSAV}/pspool/SUNWsmmgr/reloc/etc/sma/snmp/snmpd.conf"
	PspoolSnmpdConf_Bck="${PspoolSnmpdConf}-pre${PatchNum}"
	if test -f "${PspoolSnmpdConf_Bck}" ; then
		mv -f "${PspoolSnmpdConf_Bck}" "${PspoolSnmpdConf}"
		SnmpdConfSum="`/usr/bin/sum ${PspoolSnmpdConf} | awk '{print($1);}'`"
		SnmpdConfSize="`/usr/bin/wc -c ${PspoolSnmpdConf} | awk '{print($1);}'`"
		OldLine="`grep '^[0-9] e preserve etc/sma/snmp/snmpd\.conf [0-9]* [a-z]* [a-z]* [0-9]* [0-9]* [0-9]*' ${PkgMap}`"
		NewLine="`echo ${OldLine} | sed -e \"s#^\([0-9] e preserve etc/sma/snmp/snmpd\.conf [0-9]* [a-z]* [a-z]*\) [0-9]* [0-9]* \([0-9]*\)#\1 ${SnmpdConfSize} ${SnmpdConfSum} \2#\"`"
		OldLine="`echo ${OldLine} | sed -e 's#/#\\\\/#g'`"
		ed ${PkgMap} <<-EOT >/dev/null 2>&1
			/${OldLine}/
			c
			${NewLine}
			.

			w
			q
		EOT
	fi



}

### MAIN ###

for PKG_NAME in ${PKG_LIST}; do
	process_${PKG_NAME}; done

### END: walk through packages
