#!/bin/sh
#
# Copyright 2001-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident	"@(#)postinstall	1.31	03/04/16 SMI"
#
# Update spmadmin user

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

SCGUIEVENTD=/var/cluster/spm/bin/scguieventd

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

	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
}

###########################################
#
# add entries to /etc/sysevent/config/SUNW,sysevent.conf
#
###########################################
update_etc_sysevent_conf()
{
	update_dir=${BASEDIR}/etc/sysevent/config
	update_file=${update_dir}/SUNW,sysevent.conf

	mkdir -p update_dir

	if [ -f ${update_file} ]
	then
	    # remove the old entries
	    remove_SUNWscvr_edits ${update_file}
	    echo "Removed old entries"
	fi

	echo Adding new entries to ${update_file}
	# add the new entries to the copy
cat << EOF >> ${update_file}
# ${STARTTAG}
- - SUNW - - - - ${SCGUIEVENTD} \$sequence \$subclass
# ${ENDTAG}
EOF

	if [ $? -ne 0 ]; then
		echo "${PROG}: unable to update ${update_file}"
		return 1
	fi

	if [ -x /usr/sbin/clinfo ]; then
		/usr/sbin/clinfo >/dev/null 2>&1
		if [ $? -eq 0 ]; then
			pkill -HUP syseventd
		fi
	fi

	return 0
}

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

PROG=SUNWscvr.postinstall

# Add the spm user profile
chroot ${PKG_INSTALL_ROOT:-/} /usr/sbin/usermod -P "SPM User" spmadmin

if [ $? -ne 0 ]
then
    echo "${PROG}: Failed to add the 'SPM User' RBAC profile to the spmadmin user"
fi

# Change the ownership of the files in /var/cluster/spm
chroot ${PKG_INSTALL_ROOT:-/} /usr/bin/chown -R spmadmin /var/cluster/spm

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

# Register SunPlex Manager with the Sun Management Center

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


# Register the applications
if [ -s ${SMREG} ]; then

    chroot ${PKG_INSTALL_ROOT:-/} ${SMREG} add \
	    -d ${CONSOLE_DIR}/SunPlexManager

    # If not jumpstarting, restart the server

    if [ "$PKG_INSTALL_ROOT" = "/" ] || [ "$PKG_INSTALL_ROOT" = "" ]
    then

	TERM=xterms; export TERM
	JAVA_HOME=/usr/java ; export JAVA_HOME
	${BIN_DIR}/smcwebserver stop
	${BIN_DIR}/smcwebserver start

    fi

fi

# Register the event handler
update_etc_sysevent_conf

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