#!/bin/sh
#
#
#ident "@(#)postinstall   1.2     99/09/26 SMI"
#
# Copyright (c) 1999 by Sun Microsystems, Inc.
#
#

myname=`basename $0`
#echo "begin with the $myname script"
eval=0
SCRIPT_MODIFIED="false"

OLD_PATCHES="101231 101929 103926 102580 104172 104173 104275"

#
# Cleanup temporary files
#
cleanup() {
	/usr/bin/rm -f /tmp/devlink.tab.old /tmp/devlink.tab.$$
	/usr/bin/rm -f /tmp/inetd.conf.old /tmp/inetd.conf.$$
	/usr/bin/rm -f /tmp/rpc.old /tmp/rpc.$$
}

#
# 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
	#echo "done with the $myname script, exiting with $eval"
	cleanup
	reb=`expr "$eval" / 10`
	if [ "$reb" -gt "0" ]; then
		touch ${PKG_INSTALL_ROOT}/reconfigure
	fi

	# 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


	exit $eval
}

#
# set reboot part of exit value and exit
#

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

oldhupdaemon()
{
	progname=$1

	#
	# kill daemon - this avoids a debug copy getting in the way.
	#
	all=`ps -ef | grep ${progname} | grep -v grep | awk '{ print $2 }'`
	if [ "$all" ]; then
		kill -HUP $all ||
			echo "kill -HUP $all (${progname}) failed... continuing"
	fi
}

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

#
# rpc.metad only on sparc
#
do_daemons=""
do_metad=""
do_metamhd=""
if [ -x ${PKG_INSTALL_ROOT}/usr/sbin/rpc.metad ]; then
	do_metad="yes"
	do_daemons="yes"
fi
if [ -x ${PKG_INSTALL_ROOT}/usr/sbin/rpc.metamhd ]; then
	do_metamhd="yes"
	do_daemons="yes"
fi

if [ "$do_daemons" ]; then

#
# add metad and metamhd lines to inetd.conf
#
cat /dev/null > /tmp/inetd.conf.$$
if [ "$do_metad" ]; then
	cat << THE_END >> /tmp/inetd.conf.$$
# rpc.metad
100229/1	tli	rpc/tcp		wait	root	/usr/sbin/rpc.metad	rpc.metad
THE_END
	if [ $? -ne 0 ]; then
		echo "$myname: can't create /tmp/inetd.conf.$$"
		errout 1
	fi
fi
if [ "$do_metamhd" ]; then
	cat << THE_END >> /tmp/inetd.conf.$$
# rpc.metamhd
100230/1	tli	rpc/tcp		wait	root	/usr/sbin/rpc.metamhd	rpc.metamhd
THE_END
	if [ $? -ne 0 ]; then
		echo "$myname: can't create /tmp/inetd.conf.$$"
		errout 1
	fi
fi

inetconf=${PKG_INSTALL_ROOT}/etc/inet/inetd.conf
egrep 'rpc.metad|rpc.metamhd' ${inetconf} > /tmp/inetd.conf.old
case $? in
0|1)	;;
*)	echo "$myname: can't create /tmp/inetd.conf.$$"
	errout 1
	;;
esac

cmp -s /tmp/inetd.conf.old /tmp/inetd.conf.$$
case $? in
0)	;;

1)	egrep -v 'rpc.metad|rpc.metamhd' ${inetconf} > ${inetconf}.tmp
	if [ $? -ne 0 ]; then
		echo "$myname: can't edit ${inetconf}"
		errout 1
	fi
	cat /tmp/inetd.conf.$$ >> ${inetconf}.tmp
	if [ $? -ne 0 ]; then
		echo "$myname: can't edit ${inetconf}"
		errout 1
	fi
	mv ${inetconf}.tmp ${inetconf}
	if [ $? -ne 0 ]; then
		echo "$myname: can't edit ${inetconf}"
		errout 1
	fi
	# installf -f $PKGINST
	;;

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

#
# add metad and metamhd lines to rpc
#
cat /dev/null > /tmp/rpc.$$
if [ "$do_metad" ]; then
	cat << THE_END >> /tmp/rpc.$$
# rpc.metad
metad		100229	metad
THE_END
	if [ $? -ne 0 ]; then
		echo "$myname: can't create /tmp/rpc.$$"
		errout 1
	fi
fi
if [ "$do_metamhd" ]; then
	cat << THE_END >> /tmp/rpc.$$
# rpc.metamhd
metamhd		100230	metamhd
THE_END
	if [ $? -ne 0 ]; then
		echo "$myname: can't create /tmp/rpc.$$"
		errout 1
	fi
fi

rpc=${PKG_INSTALL_ROOT}/etc/rpc
egrep 'metad|metamhd' ${rpc} > /tmp/rpc.old
case $? in
0|1)	;;
*)	echo "$myname: can't create /tmp/rpc.old"
	errout 1
	;;
esac

cmp -s /tmp/rpc.old /tmp/rpc.$$
case $? in
0)	;;

1)	egrep -v 'metad|metamhd' ${rpc} > ${rpc}.tmp
	if [ $? -ne 0 ]; then
		echo "$myname: can't edit ${rpc}"
		errout 1
	fi
	cat /tmp/rpc.$$ >> ${rpc}.tmp
	if [ $? -ne 0 ]; then
		echo "$myname: can't edit ${rpc}"
		errout 1
	fi
	mv ${rpc}.tmp ${rpc}
	if [ $? -ne 0 ]; then
		echo "$myname: can't edit ${rpc}"
		errout 1
	fi
	# installf -f $PKGINST
	;;

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

#
# Make sure that inetd is aware of the changes
#
hupdaemon rpc.metad
hupdaemon rpc.metamhd
hupdaemon inetd

#
# do_daemons
#
fi
errout 0
#

