#! /usr/bin/sh
#
# ident	"@(#)postremove	1.30	03/06/26 SMI"
#
# Copyright (c) 2000-2003 by Sun Microsystems, Inc.
# All rights reserved.
#

PATH="/usr/bin:/usr/sbin"
export PATH

iroot=${PKG_INSTALL_ROOT:-$BASEDIR}
#
# This is the failover daemon's UID configuration file.  This is the final
# location for the file.  The preinstall creates the file.
#
VALIDUIDSFILENAME=.fomd_uids.cf
VALIDUIDSDIR=${iroot}/etc/opt/SUNWSMS/config
VALIDUIDSFILE=${VALIDUIDSDIR}/${VALIDUIDSFILENAME}

SMSUSERLIST="
sms-codd sms-dca sms-dsmd sms-dxs sms-efe sms-efhd sms-elad sms-erd
sms-esmd sms-fomd sms-frad sms-osd sms-pcd sms-tmd"

SMSGROUPLIST="
platadmn platoper platsvc  dmnaadmn dmnarcfg dmnbadmn dmnbrcfg dmncadmn
dmncrcfg dmndadmn dmndrcfg dmneadmn dmnercfg dmnfadmn dmnfrcfg dmngadmn
dmngrcfg dmnhadmn dmnhrcfg dmniadmn dmnircfg dmnjadmn dmnjrcfg dmnkadmn
dmnkrcfg dmnladmn dmnlrcfg dmnmadmn dmnmrcfg dmnnadmn dmnnrcfg dmnoadmn
dmnorcfg dmnpadmn dmnprcfg dmnqadmn dmnqrcfg dmnradmn dmnrrcfg sms"

############################################################################
#
# instance_count
#
# This function determines the number of installed instances of the provided
# package.
#
# $1 - package name (defaults to this package)
#
############################################################################
instance_count()
{
	pkg=${1:-${PKG}}

	# request infomation on all package instances w/ the name $pkg and count
	# them using the wc command
	count=`pkginfo $pkg.* 2>/dev/null | egrep -v '^ERROR' | wc -l`

	return $count
}


############################################################################
#
# remove_user_ids
#
# This function removes the SMS daemon and server user ids
#
############################################################################
remove_user_ids()
{
	PASSWD=${iroot}/etc/passwd
	SHADOW=${iroot}/etc/shadow

	# Remove the UID configuration file
	rm -f ${VALIDUIDSFILE} >/dev/null 2>&1

	# Remove the sms daemon and server users from the passwd file.
	if [ -f ${PASSWD} ]
	then
		( for USER in $SMSUSERLIST; do echo "/^$USER:/d"; done
		echo 'w\nq' ) | ed -s ${PASSWD} >/dev/null
		true # force retval of this func to success
	fi

	# Remove the sms daemon and server passwords from the shadow file.
	if [ -f ${SHADOW} ]
	then
		( for USER in $SMSUSERLIST; do echo "/^$USER:/d"; done
		echo 'w\nq' ) | ed -s ${SHADOW} >/dev/null
		true # force retval of this func to success
	fi
}


############################################################################
#
# remove_group_ids
#
# This function removes the SMS daemon and server user ids
#
############################################################################
remove_group_ids()
{
	GROUPS=${iroot}/etc/group

	# Remove the sms groups from the passwd file.
	if [ -f ${GROUPS} ]
	then
		(for GROUP in $SMSGROUPLIST; do echo "/^$GROUP:/d"; done
		echo 'w\nq' )| ed -s ${GROUPS} >/dev/null
		true # force retval of this func to success
	fi
}


#############################################################
#
# undo_etc_system_changes_if_any()
# Remove the changes added by fix_etc_system(), above.
# That is, remove the "exclude:drv/ohci" line from /etc/system.
#
#############################################################
undo_etc_system_changes_if_any()
{
	if grep '^exclude:drv/ohci$' ${iroot}/etc/system >/dev/null 2>&1
	then
		rm -rf ${iroot}/etc/system.$$
		sed '/SUNWSMSr start$/,/SUNWSMSr end$/d' \
			<${iroot}/etc/system >${iroot}/etc/system.$$
		# Preserve /etc/system permission and ownership:
		cat ${iroot}/etc/system.$$ >${iroot}/etc/system
		rm -f ${iroot}/etc/system.$$
	fi
}



############################################################
# MAIN - execution starts here!
############################################################

# check if this package is the only instance (of this package)
# installed, if so then we have some cleanup to do
instance_count
if [ $? -eq 1 ]; then
	# cleanup the run control scripts and the main or spare flag files
	rm -f ${iroot}/etc/init.d/sms >/dev/null 2>&1;
	rm -f ${iroot}/etc/rc3.d/S99sms >/dev/null 2>&1;
	rm -f ${iroot}/etc/rc1.d/K20sms >/dev/null 2>&1;
	rm -f ${iroot}/etc/rc0.d/K20sms >/dev/null 2>&1;

	# remove the SMS daemon and server uids...
	# ... and the SMS daemon and server gids
	remove_user_ids && remove_group_ids

	# remove additions to /etc/system
	undo_etc_system_changes_if_any

	exit $?
fi

exit 0
