#!/bin/sh
#
# Copyright 2001-2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)preremove	1.19	02/08/02 SMI"
#
# Remove the spmadmin user and the messages file from the system.

##############################################################################
#
# Main
#
##############################################################################

PKGNAME=SUNWscvr
STARTTAG="Start of lines added by ${PKGNAME}"
ENDTAG="End   of lines added by ${PKGNAME}"
PROG=${PKGNAME}.preremove

# Remove the spm user
/usr/sbin/userdel spmadmin

###########################################
#
# remove_SUNWscvw_edits <filename>
#
#	Backout all lined from the given ascii <filename>
#	indicated by the SUNWscvw comment tag lines.
#
###########################################
remove_SUNWscvw_edits()
{
	update_file=$1

	# If the file does not exist, return without error
	if [ ! -f "${update_file}" ]; then
		return 0
	fi

	while :
	do
		grep "${STARTTAG}" ${update_file} >/dev/null 2>&1
		case $? in
		2)	# error reading file
			echo "${PROG}:  error reading ${update_file}"
			return 1
			;;

		0)	# grep found the string, so get rid of the entries
			ed -s ${update_file} << EOF >/dev/null 2>&1
/${STARTTAG}/,/${ENDTAG}/d
w
q
EOF
			if [ $? -ne 0 ]; then
				echo "${PROG}: problem updating ${update_file}"
				return 1
			fi
			;;

		*)
			break
			;;
		esac
	done

	return 0
}

###########################################
#
# main
#
###########################################

if [ $? -ne 0 ]
then
    echo "${PROG}: Failed to remove the spmadmin user from the system"
fi

# Remove the /var/cluster/spm/messages file
rm -f /var/cluster/spm/messages

if [ $? -ne 0 ]
then
    echo "${PROG}: Failed to remove /var/cluster/spm/messages"
fi

# Unregister the SunPlex Manager

CONSOLE_DIR=${PKG_INSTALL_ROOT}/usr/sadm/lib/webconsole
BIN_DIR=${PKG_INSTALL_ROOT}/usr/sadm/bin
SMREG=${BIN_DIR}/smreg
export BASEDIR

if [ -s ${SMREG} ]; then

    ${SMREG} remove \
	-d ${CONSOLE_DIR}/SunPlexManager

fi

# Remove sysevent registration
remove_SUNWscvw_edits /etc/sysevent/config/SUNW,sysevent.conf
/usr/bin/pkill -HUP syseventd

# exit 0 so that the pkgrm will always succeed
exit 0
