#!/bin/sh
#
#ident "@(#)postinstall   1.8     00/02/07 SMI"
#
# Copyright (c) 1998, 2000 by Sun Microsystems, Inc.
# All rights reserved
#

myname=`basename $0`
eval=0
CUR_INETDCONF=${PKG_INSTALL_ROOT}/etc/inet/inetd.conf
CUR_RPCFILE=${PKG_INSTALL_ROOT}/etc/rpc
TMP_INETDDIR=/tmp/inetd.conf.$$
TMP_RPCDIR=/tmp/rpc.$$
TMP_INETDFILE=/tmp/inetd.conf.$$.old
TMP_RPCFILE=/tmp/rpc.$$.old

# Give this list a few more releases after 4.1 and then start to
# remove the older ones. - JST
OLD_PATCHES="101231 101929 103926 102580 104172 104173 104275 106627"

#
# Cleanup temporary files
#
cleanup() {
	/usr/bin/rm -f ${TMP_INETDFILE}
	/usr/bin/rm -rf ${TMP_INETDDIR}
	/usr/bin/rm -f ${TMP_RPCFILE}
	/usr/bin/rm -rf ${TMP_RPCDIR}
}

#
# 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
}

#
# Clear out old DiskSuite patches
#
clear_old_patches() {
	if [ -d /var/sadm/patch ]; then
		cd /var/sadm/patch
	else
		return
	fi

	for patch in $OLD_PATCHES; do
		rm -rf ${patch}-??
	done
}

#
# set error part of exit value and exit
#
errout() {
	seterr $1
	cleanup
	exit $eval
}

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

#
# HUP a daemon
#
hupdaemon()
{
	/usr/bin/pgrep -x -f $1 > /dev/null 2>&1
	if [ $? = 0 ] ; then
	  /usr/bin/pkill -HUP -x -f $1 || echo "pkill -HUP -x -f $1 failed... continuing"
	fi
}

#
# catch common signals
#
trap 'echo "$myname: caught signal"; errout 3' 1 2 3

#
# Insert metacld and metamedd lines to inetd.conf one by one.
# It's always possible that this is an upgrade wherein rpc.metamedd
# is already present.
#
# Setup.
TITLES="metamedd metacld"

# Create temporary files for each of our entries.
# First the inetd entries...
cleanup

/usr/bin/mkdir ${TMP_INETDDIR}
if [ $? -ne 0 ]; then
	echo "$myname: can't create ${TMP_INETDDIR}"
	errout 1
fi

cat << THE_END > ${TMP_INETDDIR}/rpc.metamedd
# rpc.metamedd - DiskSuite mediator
100242/1	tli	rpc/tcp		wait	root	/usr/sbin/rpc.metamedd	rpc.metamedd
THE_END
if [ $? -ne 0 ]; then
	echo "$myname: can't create /tmp/${TMP_INETDDIR}/rpc.metamedd"
	errout 1
fi

cat << THE_END > ${TMP_INETDDIR}/rpc.metacld
# rpc.metacld - DiskSuite cluster control
100281/1	tli	rpc/tcp		wait	root	/usr/sbin/rpc.metacld	rpc.metacld
THE_END
if [ $? -ne 0 ]; then
	echo "$myname: can't create ${TMP_INETDDIR}/rpc.metacld"
	errout 1
fi

# ... and then the rpc entries.
/usr/bin/mkdir ${TMP_RPCDIR}
if [ $? -ne 0 ]; then
	echo "$myname: can't create ${TMP_RPCDIR}"
	errout 1
fi

cat << THE_END > ${TMP_RPCDIR}/metamedd
# rpc.metamedd - DiskSuite mediator
metamedd	100242	metamedd
THE_END
if [ $? -ne 0 ]; then
	echo "$myname: can't create ${TMP_RPCDIR}/rpc.metamedd"
	errout 1
fi

cat << THE_END > ${TMP_RPCDIR}/metacld
# rpc.metacld - DiskSuite cluster control
metacld		100281	metacld
THE_END
if [ $? -ne 0 ]; then
	echo "$myname: can't create ${TMP_RPCDIR}/rpc.metacld"
	errout 1
fi

# Now do the actual idempotent insertions.
for title in $TITLES; do
	prog="rpc.${title}"

	# Create a file containing the current inetd.conf entry
	# for our rpc iff it exists.
	egrep ${prog} ${CUR_INETDCONF} > ${TMP_INETDFILE}
	case $? in
	0|1)	;;
	*)	echo "$myname: can't create ${TMP_INETDFILE}"
		errout 1
		;;
	esac

	# Check to see if the current entry is identical to the
	# one we want to insert.
	cmp -s ${TMP_INETDFILE} ${TMP_INETDDIR}/${prog}
	case $? in
	0)	;;	# It's already there.

	1)	egrep -v ${prog} ${CUR_INETDCONF} > ${CUR_INETDCONF}.tmp
		if [ $? -ne 0 ]; then
			echo "$myname: can't edit ${CUR_INETDCONF}"
			errout 1
		fi

		# ${CUR_INETDCONF}.tmp now contains all lines *except*
		# the old version of our rpc (if present). So we now insert
		# our new inetd entry.
		cat ${TMP_INETDDIR}/${prog} >> ${CUR_INETDCONF}.tmp
		if [ $? -ne 0 ]; then
			echo "$myname: can't edit ${CUR_INETDCONF}"
			errout 1
		fi

		# Move the entry into the actual file.
		mv ${CUR_INETDCONF}.tmp ${CUR_INETDCONF}
		if [ $? -ne 0 ]; then
			echo "$myname: can't edit ${CUR_INETDCONF}"
			errout 1
		fi
		# installf -f $PKGINST
		;;

	*)	echo "$myname: can't edit ${CUR_INETDCONF}"
		errout 1
		;;
	esac

	# We're done with the temporary file
	/usr/bin/rm ${TMP_INETDDIR}/${prog}

	#
	# add metacld and metamhd lines to rpc
	#
	egrep ${title} ${CUR_RPCFILE} > ${TMP_RPCFILE}
	case $? in
	0|1)	;;
	*)	echo "$myname: can't create ${TMP_RPCFILE}"
		errout 1
		;;
	esac

	# Check to see if the current entry is identical to the
	# one we want to insert.
	cmp -s ${TMP_RPCFILE} ${TMP_RPCDIR}/${title}
	case $? in
	0)	;;	# It's already there.

	1)	egrep -v ${title} ${CUR_RPCFILE} > ${CUR_RPCFILE}.tmp
		if [ $? -ne 0 ]; then
			echo "$myname: can't edit ${CUR_RPCFILE}"
			errout 1
		fi

		# ${CUR_RPCFILE}.tmp now contains all lines *except*
		# the old version of our rpc (if present). So we now insert
		# our new rpc entry.
		cat ${TMP_RPCDIR}/${title} >> ${CUR_RPCFILE}.tmp
		if [ $? -ne 0 ]; then
			echo "$myname: can't edit ${CUR_RPCFILE}"
			errout 1
		fi

		# Move the entry into the actual file.
		mv ${CUR_RPCFILE}.tmp ${CUR_RPCFILE}
		if [ $? -ne 0 ]; then
			echo "$myname: can't edit ${CUR_RPCFILE}"
			errout 1
		fi
		# installf -f $PKGINST
		;;

	*)	echo "$myname: can't edit ${CUR_RPCFILE}"
		errout 1
		;;
	esac

	# We're done with the temporary file
	/usr/bin/rm ${TMP_RPCDIR}/${title}

done

# We're done with the temporary directories
/usr/bin/rmdir ${TMP_INETDDIR}
/usr/bin/rmdir ${TMP_RPCDIR}

#
# Make sure that inetd is aware of the changes
#
hupdaemon rpc.metacld
hupdaemon rpc.metamedd
hupdaemon inetd

# Perform an upgrade
if [ "$IS_AN_UPGRADE" = "true" ]; then
	clear_old_patches
	sh $UPGR_SCRIPT &
	# Give the script a chance to queue onto the package database lock
	sleep 3
fi

rebout 0
