#! /bin/sh
#
#ident	"@(#)postinstall	1.2	02/02/11 SMI"
#
# Copyright 2002 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

###########################################################################
#
# postinstall for SUNWscucm
#
#	* Update ${BASEDIR}/etc/inetd.conf for SUNWscucm daemons
#	* Update ${BASEDIR}/etc/rpc        for SUNWscucm daemons
#
###########################################################################

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

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

TMPFILES=

###########################################
#
# Cleanup any temporary files on exit/interrupt
#
###########################################
cleanup()
{
	exit_code=$1

	if [ "${TMPFILES}" ]; then
		rm -f ${TMPFILES}
	fi

	exit ${exit_code}
}

###########################################
#
# remove_SUNWscucm_edits <filename>
#
#	Backout all lined from the given ascii <filename>
#	indicated by the SUNWscucm comment tag lines.
#
###########################################
remove_SUNWscucm_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/inet/inetd.conf
#
###########################################
update_etc_inetd_conf()
{
	update_file=${BASEDIR}/etc/inet/inetd.conf

	# remove the old entries
	remove_SUNWscucm_edits ${update_file}

	# add the new entries to the copy
cat << EOF >> ${update_file}
# ${STARTTAG}
100532/1 	tli	rpc/circuit_v	wait root /usr/cluster/lib/ucmm/rpc.ucmmstate	rpc.ucmmstate
# ${ENDTAG}
EOF

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

	return 0
}

###########################################
#
# add entries to /etc/rpc
#
###########################################
update_etc_rpc()
{
	update_file=${BASEDIR}/etc/rpc

	# remove the old entries
	remove_SUNWscucm_edits ${update_file}
	
	# add the new entries to the copy
cat << EOF >> ${update_file}
# ${STARTTAG}
ucmmstate	100532
# ${ENDTAG}
EOF

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

	return 0
}

###########################################
#
# sig_hup_inetd - ask inetd to reread conf file
#	if we're not in a jumpstart environment
#
#	always return success regardless of
#	pkill's return code; don't want to abort
#	install just for inetd HUP failure
#
###########################################
sig_hup_inetd()
{

	# tweak inetd only if we're not jumpstarting
	if [ "$PKG_INSTALL_ROOT" = "/" ] || [ "$PKG_INSTALL_ROOT" = "" ]
	then
		/usr/bin/pkill -HUP -x -u 0 -P 1 inetd
		status=$?
		# require exact match on 'inetd', user root, ppid 1

		if [ $status -ne 0 ]
		then
			echo "${PROG}:  unable to restart inetd."
		fi
	fi
	return 0
}

###########################################
#
# main
#
###########################################
errs=0

# catch common signals
trap 'echo "${PROG}: caught signal";  cleanup 3' 1 2 3 15

update_etc_inetd_conf  	   || errs=`expr ${errs} + 1`
update_etc_rpc	  	   || errs=`expr ${errs} + 1`
sig_hup_inetd		   || errs=`expr ${errs} + 1`

if [ ${errs} -ne 0 ]; then
	cleanup 1
else
	cleanup 0
fi
