#! /bin/sh
#
#ident	"@(#)postremove	1.10	04/01/07 SMI"
#
# Copyright 1996-2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#

###########################################################################
#
# postremove for SUNWscu
#
#	* Remove our changes to /etc/inetd.conf
#	* Remove our changes to /etc/rpc
#	* Remove our changes to /etc/services
#
###########################################################################

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}.postremove

TMPFILES=

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

	if [ "${TMPFILES}" ]; then
		rm -f ${TMPFILES}
	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

	# If the file does not exist, return without error
	if [ ! -f "${update_file}" ]; then
		return 0
	fi

	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
}

###########################################
#
# main
#
###########################################
errs=0

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

remove_SUNWscu_edits /etc/inetd.conf	|| errs=`expr ${errs} + 1`
remove_SUNWscu_edits /etc/rpc		|| errs=`expr ${errs} + 1`
remove_SUNWscu_edits /etc/services	|| errs=`expr ${errs} + 1`
remove_SUNWscu_edits /kernel/drv/log.conf	|| errs=`expr ${errs} + 1`
remove_SUNWscu_edits /var/spool/cron/crontabs/root || errs=`expr ${errs} + 1`

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