#!/bin/sh
#
#ident "@(#)preremove   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
}

#
# set reboot part of exit value and exit
#
rebout() {
	setreb $1
	errout 0
}

#
# exit for reconfiguration reboot
#
reconfigout() {
	echo "Please reconfiguration reboot the system"
	touch ${PKG_INSTALL_ROOT}/reconfigure
	rebout 10
}

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

if [ "$BEING_UPGRADED" != "true" ]; then
	# if SUNWmd pkg does not exist use commands from sbin.
	pkginfo -R ${PKG_INSTALL_ROOT:-/} -q SUNWmd\* 
	if [ $? = 1 ] ; then
		METADB=${PKG_INSTALL_ROOT}/usr/sbin/metadb
	else
		METADB=${PKG_INSTALL_ROOT}/usr/opt/SUNWmd/sbin/metadb
	fi	
	METADEV=${PKG_INSTALL_ROOT}/dev/md/admin

	#
	# check for any configured metadevices
	#
	if [ -t 0 ]; then
		if [ -x "$METADB" -a -c "$METADEV" ]; then
			if [ "`$METADB`" ]; then
				echo "Metadevice state database contains one or more replicas."
				echo "All replicas must be removed prior to the removal of this package."
				errout 1
			fi
		fi
	fi
fi
#
errout 0
