#!/bin/sh
#
#
#ident "@(#)postremove   1.2     99/09/26 SMI"
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
#
#

#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() {
	/usr/bin/pgrep -x -f $1 > /dev/null 2>&1
	if [ $? = 0 ] ; then
	  /usr/bin/pkill -HUP -x -f $1 || \
		echo "/usr/bin/pkill -HUP -x -f $1 failed... continuing"
	fi
}

oldhupdaemon()
{
	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
# Last one out turns off the lights.

INSTALLED_PKGS=`pkginfo $PKG\\* | wc -l`
# if SUNWmd pkg does not exist use commands from sbin.

if [ $INSTALLED_PKGS -eq 1 ]; then
	pkginfo -R ${PKG_INSTALL_ROOT:-/} -q SUNWmd\* 
	if [ $? = 1 ] ; then
		METADB=${PKG_INSTALL_ROOT}/usr/sbin/metadb
	else
		METASTAT=${PKG_INSTALL_ROOT}/usr/opt/SUNWmd/sbin/metastat
	fi
	METADEV=${PKG_INSTALL_ROOT}/dev/md/admin

	devltab=${PKG_INSTALL_ROOT}/etc/devlink.tab
	grep -v name=md\;minor3 ${devltab} > /tmp/SUNWmd1
	case $? in
	0)	;;
	*)	echo "$myname: can't edit ${devltab}"
		errout 1
		;;
	esac

	grep -v name=md\;minor1 /tmp/SUNWmd1 > /tmp/SUNWmd2
	case $? in
		0)	;;
	*)	echo "$myname: can't edit ${devltab}"
		errout 1
		;;
	esac

	cp /tmp/SUNWmd2 ${devltab}
	case $? in
	0)	;;
	*)	echo "$myname: can't edit ${devltab}"
		errout 1
		;;
	esac
	rm /tmp/SUNWmd1 /tmp/SUNWmd2

	#
	# Make sure we remove the additions that were made to /etc/inetd.conf
	#
	inetconf=${PKG_INSTALL_ROOT}/etc/inet/inetd.conf
	awk '
	/[. 	]rpc.metad$/ {next}
	/[. 	]rpc.metamhd$/ {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 make to /etc/rpc
	#
	rpc=${PKG_INSTALL_ROOT}/etc/rpc
	awk '
	/[. 	]metad$/ {next}
	/[. 	]metamhd$/ {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.metad
	hupdaemon rpc.metamhd
	hupdaemon inetd

fi	# if INSTALLED_PKGS -eq 1

errout 0




