#!/bin/sh
#
#ident "@(#)postremove   1.3     99/10/18 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
	  pkill -HUP -x -f $1 || echo "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. 
# If SUNWmdr is installed it will do the remove.

INSTALLED_PKGS=`pkginfo $PKG\\* | wc -l`
pkginfo -R ${PKG_INSTALL_ROOT:-/} -q SUNWmdr\*

if [ $INSTALLED_PKGS -eq 1 -a $? = 1 ]; then
	METASTAT=${PKG_INSTALL_ROOT}/usr/opt/SUNWmd/sbin/metastat
	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 we remove the additions that were make to /etc/system
	#
	system="${PKG_INSTALL_ROOT}/etc/system"
	tmp_file="/tmp/system.$$"
	sed -n '/Begin MDD/,/End MDD/ p' ${system} > ${tmp_file}
	if [ -s ${tmp_file} ]; then

		# remove them
		if [ "${system}" ]; then
			sed '/Begin MDD/,/End MDD/ d' ${system} > ${system}.tmp
			if [ $? -ne 0 ]; then
				echo "can't edit ${system}"
				errout 1
			fi
			mv ${system}.tmp ${system}
			if [ $? -ne 0 ]; then
				echo "$myname: can't edit ${system}"
				errout 1
			fi
		fi
	fi

	#
	# Make sure inetd is aware of our changes
	#
	hupdaemon rpc.metad
	hupdaemon rpc.metamhd
	hupdaemon inetd

	#
	# Remove all the /dev/md and /devices/pseudo that we created.
	#
	if [ -d /dev/md ]; then 
		echo "*** Removing metadevice /dev and /devices entries.\n"
		echo "\t(This may take a while.)"
		(cd /dev/md; /usr/bin/ls /dev/md 2>/dev/null | \
	    	/usr/bin/sed -n -e '/dsk/p' -e '/admin/p' -e '/shared/p' | \
	    	/usr/bin/xargs -l10 /usr/bin/rm -rf)
		x=`ls -l /dev/md | wc -l`

		if [ $x -eq 1 ]; then
			echo "removing /dev/md"
			rmdir /dev/md || echo "rmdir of /dev/md failed"
		fi
		(cd /devices/pseudo; /usr/bin/ls /devices/pseudo \
	    2>/dev/null | /usr/bin/sed -n -e 's;^\(md@.*\);\1;p' | /usr/bin/xargs -l10 /usr/bin/rm -f)
	fi
fi	# if INSTALLED_PKGS -eq 1

errout 0




