#! /bin/sh
#
# Copyright 1996-1999,2001-2003 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#ident  "@(#)postremove 1.9 03/08/14 SMI"
#

###########################################################################
#
# postremove for SUNWscr
#
#	* Remove our changes to /etc/devlink.tab
#	* Remove our changes to /kernel/drv/sd.conf
#	* Remove our changes to /kernel/drv/ssd.conf
#	* Remove our changes to /etc/system
#
###########################################################################

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

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

TMPFILES=/etc/rcS.d/S68did_update_vfstab
CLPRIVNET_TMPFILE=/tmp/rem_drv_output.$$

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

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

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

	exit ${exit_code}
}

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

	# If the file does not exist, return without error
	if [ ! -f "${update_file}" ]; then
		return 0
	fi

	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
}

###########################################
#
# remove clprivnet driver 
#
# This function is executed here rather than in preremove to support
# patch removal as well as package removal.
#
###########################################
remove_clprivnet_driver()
{

#
# When postremove is used with patch removal, if the clprivnet driver  
# still exists (the previous version) then rem_drv should not be done.
#
if [ -f "${BASEDIR}/kernel/drv/clprivnet" ]; then
	return 0
fi

#
# The rem_drv command may output to stderr but continue on successfully.
# Stderr output is displayed here only in the event of an error exit.
# 
/usr/sbin/rem_drv  clprivnet 2> "${CLPRIVNET_TMPFILE}"

# report any errors and print output collected from stderr
if [ $? -ne 0 ]; then
	echo "${PROG}: rem_drv of clprivnet driver failed"
	if [ -f "${CLPRIVNET_TMPFILE}" ]; then
		echo "rem_drv stderr output:"
		cat "${CLPRIVNET_TMPFILE}"
	fi
	return 1
fi

return 0
}

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

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

remove_clprivnet_driver	|| errs=`expr ${errs} + 1`
remove_SUNWscr_edits /etc/devlink.tab	|| errs=`expr ${errs} + 1`
remove_SUNWscr_edits /kernel/drv/sd.conf	|| errs=`expr ${errs} + 1`
remove_SUNWscr_edits /kernel/drv/ssd.conf	|| errs=`expr ${errs} + 1`
remove_SUNWscr_edits /etc/system	|| errs=`expr ${errs} + 1`
remove_SUNWscr_edits /etc/iu.ap		|| errs=`expr ${errs} + 1`

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