# This script deletes the used backout data for a patch package
# and removes the deletes file entries.
#
# directory format options.
#
#       @(#)patch_postinstall 1.3 98/07/29 SMI
#
# Copyright (c) 1995 by Sun Microsystems, Inc.
# All rights reserved
#

PATH=/usr/sadm/bin:$PATH
THIS_DIR=`dirname $0`

Our_Deletes=$THIS_DIR/deletes

CMSDEF_D=$BASEDIR/usr/platform/SUNW,Ultra-4FT/SUNWcms/etc/cmsdef.d
CMSDEF_CONF=$CMSDEF_D/cmsdef.conf
ETC_CMS=$BASEDIR/etc/SUNWcms
CONFIG_DIR=$ETC_CMS/.config
CONFIG_FILE=$CONFIG_DIR/configfile
SAV_CONFIG_FILE=$PKGSAV/configfile

#
# Delete the used backout data
#
if [ -f $Our_Deletes ]; then
	cat $Our_Deletes | while read path; do
		if valpath -l $path; then
			Client_Path=`echo "$CLIENT_BASEDIR/$path" | sed "s|//|/|"`
		else	# It's an absolute path
			Client_Path=$path
		fi
		rm `removef $PKGINST $Client_Path`

		# Having reomved the path from package manager knowledge, see if its a cmsdef
		CMSDEF="`expr $Client_Path : '\(.*cmsdef.d.*so\)$'`"
		[ "$CMSDEF" ] || continue
		# Yes, so remove it from the cmsdef.conf file
		sed "/$CMSDEF/d" $CMSDEF_CONF > $CMSDEF_CONF.tmp
		mv $CMSDEF_CONF.tmp $CMSDEF_CONF	
	done
	removef -f $PKGINST

	rm $Our_Deletes
fi

#
# Remove the deletes file, checkinstall and the postinstall
#
rm -r $PKGSAV/$ACTIVE_PATCH
rm -f $THIS_DIR/checkinstall $THIS_DIR/postinstall

# Reset the cmsdef configuration
find $CMSDEF_D -name '*.so' | sed 's,.*/,,; s,\.so,,' > $CMSDEF_CONF.tmp
mv $CMSDEF_CONF.tmp $CMSDEF_CONF

[ -f $SAV_CONFIG_FILE -o -f $SAV_CONFIG_FILE- ] && {
	# At least one of the old config files was saved ...
	for F in $SAV_CONFIG_FILE $SAV_CONFIG_FILE-; do
	[ -f $F ] && {
		# so restore it to its rightful place
		mv $F $CONFIG_DIR/`echo $F | sed 's,.*/,,'` || {
			echo "mv(1) failed for file: $F" >&2
			exit 1
		}
	}
	done
} || {
	rm -f $CONFIG_FILE* || error "postinstall: rm(1) failed"
}


exit 0
