#!/bin/sh
#
#ident "@(#)postremove   1.6     98/12/08 SMI"
#
# Copyright (c) 1998 by Sun Microsystems, Inc.
# All rights reserved.
#

#echo "begin with the $0 script"
eval=0

#
# set error part of exit value
#
seterr() {
	err=`expr "$eval" % 10`
	reb=`expr "$eval" - "$err"`
	if [ "$1" -gt "$err" ]; then
		eval=`expr "$reb" + "$1"`
	fi
}

#
# set reboot part of exit value
#
setreb() {
	err=`expr "$eval" % 10`
	reb=`expr "$eval" - "$err"`
	if [ "$1" -gt "$reb" ]; then
		eval=`expr "$1" + "$err"`
	fi
}

#
# set error part of exit value and exit
#
errout() {
	seterr $1
	#echo "done with the $0 script, exiting with $eval"
	exit $eval
}

#
# HUP a daemon
#
hupdaemon()
{
	progname=$1

	#
	# kill daemon - this avoids a debug copy getting in the way.
	#
	all=`ps -ef | grep ${progname} | grep -v grep | awk '{ print $2 }'`
	if [ "$all" ]; then
		kill -HUP $all ||
			echo "kill -HUP $all (${progname}) failed... continuing"
	fi
}

#
# catch common signals
#
trap 'echo "Caught Signal"; errout 3' 1 2 3

INSTALLED_PKGS=`pkginfo $PKG\\* | wc -l`

if [ $INSTALLED_PKGS -eq 1 ]; then
	#
	# Make sure we remove the additions that were made to /etc/inetd.conf
	#
	inetconf=${PKG_INSTALL_ROOT}/etc/inet/inetd.conf
	awk '
	/metamedd/ {next}
	/metacld/ {next}
	{print}
	' < ${inetconf} > ${inetconf}.tmp

	cmp -s ${inetconf} ${inetconf}.tmp
	case $? in
	0)	rm ${inetconf}.tmp
		;;

	1)	cat ${inetconf}.tmp > ${inetconf}
		if [ $? -ne 0 ]; then
			echo "$myname: can't edit ${inetconf}"
			errout 1
		fi
		rm ${inetconf}.tmp
		;;

	*)	echo "$myname: can't edit ${inetconf}"
		errout 1
		;;
	esac

	#
	# Make sure we remove the additions that were made to /etc/rpc
	#
	rpc=${PKG_INSTALL_ROOT}/etc/rpc
	awk '
	/metamedd/ {next}
	/metacld/ {next}
	{print}
	' < ${rpc} > ${rpc}.tmp

	cmp -s ${rpc} ${rpc}.tmp
	case $? in
	0)	rm ${rpc}.tmp
		;;

	1)	
		cat ${rpc}.tmp > ${rpc}
		if [ $? -ne 0 ]; then
			echo "$myname: can't edit ${rpc}"
			errout 1
		fi
		rm ${rpc}.tmp
		;;

	*)	echo "$myname: can't edit ${rpc}"
		errout 1
		;;
	esac

	#
	# Make sure inetd is aware of our changes
	#
	hupdaemon rpc.metacld
	hupdaemon rpc.metamedd
	hupdaemon inetd
fi	# if last instance

errout 0



