#!/bin/sh
#
#ident "@(#)postinstall   1.3     96/06/24 SMI"
#
# Copyright 06/24/96 Sun Microsystems, Inc.  All Rights Reserved.
#

# postinstall
#
#	Do post install processing.
#	postinstall is called by pkgadd(1M) utility.
#
#	Copy the new postremove script directly into SUNWhagen package
#	to fix bugid 1238620.  We do this here (instead of in prototype
#	file) so that even if this patch is backed out, the old version
#	of SUNWhagen's postremove will not be restored.
#

cat >/var/sadm/pkg/SUNWhagen/install/postremove <<'EOF'
#!/bin/sh
#
#ident "@(#)postremove   1.1     96/02/22 SMI"
#
# Copyright 02/22/96 Sun Microsystems, Inc.  All Rights Reserved.
#

# postremove
#
#	Do post remove processing.
#	postremove is called by pkgrm(1M) utility.
#
#	Make the following modifications:
#
#	* Remove "sd_retry_on_reservation_conflict=0" from /kernel/drv/sd.conf.
#	* Remove "sd_retry_on_reservation_conflict=0" from /kernel/drv/ssd.conf.
#	* Remove /var/opt/SUNWhadf.
#
#	If an edit fails, print an error message and keep track of that
#	fact (increment $err) but continue processing.
#	Note: ed(1) returns exit code 2 if a search failed.
#	Exit with $err set to number of errors encountered.
#
#	Contains fix for bugid 1238620 (pkgrm(1M) of SUNWhagen can destroy
#	customer's files).

#
# increrr ()
#	increment number of errors encountered
#
increrr() {
	err=`expr $err + 1`
}


#
# Remove sd_retry_on_reservation_conflict=0 and SUNWhadf comment
# from /kernel/drv/sd.conf.
#
edit_sd_conf()
{
	if [ ! -w $PKG_INSTALL_ROOT/kernel/drv/sd.conf ]; then
		echo "$myname: can't edit $PKG_INSTALL_ROOT/kernel/drv/sd.conf"
		increrr
		return
	fi

	grep -s '^sd_retry_on_reservation_conflict' $PKG_INSTALL_ROOT/kernel/drv/sd.conf \
	    > /dev/null
	if [ $? -eq 0 ]; then
		ed -s $PKG_INSTALL_ROOT/kernel/drv/sd.conf <<end > /dev/null
g/SUNWhadf/d
/^sd_retry_on_reservation_conflict=0/d
.
w
q
end
		if [ $? -ne 0 ]; then
			echo "$myname: problem editing $PKG_INSTALL_ROOT/kernel/drv/sd.conf"
			increrr
		fi
	fi
}


#
# Remove sd_retry_on_reservation_conflict=0 and SUNWhadf comment
# from $PKG_INSTALL_ROOT/kernel/drv/ssd.conf.
#
edit_ssd_conf()
{
	DOTCONF=$PKG_INSTALL_ROOT/kernel/drv/ssd.conf

	if [ ! -w $DOTCONF ]; then
		echo "$myname: can't edit $DOTCONF"
		increrr
		return
	fi

	grep -s '^sd_retry_on_reservation_conflict' $DOTCONF > /dev/null
	if [ $? -eq 0 ]; then
		ed -s $DOTCONF <<end > /dev/null
g/SUNWhadf/d
/^sd_retry_on_reservation_conflict=0/d
.
w
q
end
		if [ $? -ne 0 ]; then
			echo "$myname: problem editing $DOTCONF"
			increrr
		fi
	fi
}


#
# remove /var/opt/SUNWhadf
#
remove_var()
{
	file=$PKG_INSTALL_ROOT/var/opt/SUNWhadf
	if [ ! -d $file ]; then
		return
	fi
	# Note--don't cross over into mounted filesystems
	find $file -mount -local -depth \
		-type d -exec rmdir '{}' ';' -o -exec rm -f '{}' ';'
	if [ $? -ne 0 ]; then
		echo "$myname: problem removing $PKG_INSTALL_ROOT/var/opt/SUNWhadf"
		## NOTE: failure of the above 'find' is not a fatal error (i.e.
		##  pkgrm should still succeed) so we don't call increrr here.
	fi
}


#
# main
#

PATH=/usr/bin
myname=`basename $0`
err=0

if [ ! -x /bin/grep  -o ! -x /bin/ed ]; then
	echo "$myname: missing /bin/grep or /bin/ed"
	exit 1
fi

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

edit_sd_conf
edit_ssd_conf
remove_var

echo "	
	NOTE: /etc/notrouter was added while installing SUNWhagen.
	It has not been removed as part of pkgrm SUNWhagen.
	Remove it by hand if you wish to enable this machine to
	become a router.
	"

echo "
	NOTE: /etc/nsswitch.conf may have been modified while
	installing SUNWhagen.  Modifications to this file resulting
	from the install of SUNWhagen have NOT been backed out as part
	of this pkgrm SUNWhagen.   One of the Solaris nsswitch.*
	template files found in the /etc directory may help you to back
	out modifications by hand.
	"

exit $err
EOF

result=$?
if [ $result -ne 0 ]; then
	echo 'A problem was encountered trying to patch the "postremove" script'
	echo ' (/var/sadm/pkg/SUNWhagen/install/postremove) \c'
	echo 'in the SUNWhagen package.'
	echo "Please correct the problem and re-install this patch."
	exit 1
else
	exit 0
fi
