# This script deletes the used backout data for a patch package
# and removes the deletes file entries.
#
# directory format options.
#
#pragma ident	"@(#)patch_postinstall	1.5	08/01/30 SMI"
#
# Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms. 
#

SED="/usr/bin/sed"
CP="/usr/bin/cp"
RM="/usr/bin/rm -f"
BASENAME="/usr/bin/basename"
DIRNAME="/usr/bin/dirname"

PATH=/usr/sadm/bin:$PATH
THIS_DIR=`dirname $0`
RMF=/usr/sbin/removef
PATCH_COMMON_LIB="/usr/lib/patch/patch_common_lib"
SAFEMODE_FAILED="Exiting! Patch deferred activation failed"
INST_DIR="$INST_DATADIR/$PKG/install"

if [ "$SAFEMODE_INSTALL" = "true" ] ; then
        if [ ! -s "$PATCH_COMMON_LIB" ]; then
                puttext "$SAFEMODE_FAILED"
                exit 1
        fi
        . $PATCH_COMMON_LIB
        InitSafemode || {
                puttext $SAFEMODE_FAILED
                exit 1
        }
fi


# Handle the pkg_* scripts in the undo package.
#	pkg_* scripts are patched CAS 
#       pkg_* scripts are copied to the destination directory
#       after stripping out the pkg_ from the file name.
#
PSPOOL_PKG="$PKG_INSTALL_ROOT/var/sadm/pkg/$PKGINST/save/pspool/$PKGINST"

if [ -d "$PSPOOL_PKG/install" ] ; then
	for script in $INST_DIR/*; do
		srcscript=`$BASENAME $script`
		targscript=`echo $srcscript | nawk '
			{ script=$0; }
			/pkg_/ {
				print "pspool";
				next;
			}
			{ print "dont_use" } '`
		if [ "$targscript" = "dont_use" ]; then
			continue
		fi
		if [ "$targscript" = "pspool" ]; then
			script=`echo $srcscript | $SED 's/pkg_//'`
			$CP $INST_DIR/$srcscript $PSPOOL_PKG/install/$script
		fi
	done
fi

Our_Deletes=$THIS_DIR/deletes

#
# Delete the used backout data
#
#
# If this is a safemode patch package and it has a deletes file in it
# handle the deletion of an object for safemode patching.
#
if [ -f $Our_Deletes ]; then
	PSPOOL_DIR="/var/sadm/pkg/$PKGINST/save/pspool/$PKGINST/install"
	cat $Our_Deletes | while read path; do
	Dir=`$DIRNAME $path`
	if [ "$Dir" = "$PSPOOL_DIR" ]; then
		path="${PKG_INSTALL_ROOT:-/}$path"
		if [ -f $path ]; then
			$RM $path
		fi
		continue
	fi

		if valpath -l $path; then
			Client_Path=`echo "$CLIENT_BASEDIR/$path" | sed "s|//|/|"`
		else	# It's an absolute path
			Client_Path=$path
		fi
		
		if [ "$SAFEMODE_INSTALL" = "true" ] ; then
			HandleSafemodeDeleteObject $PKGINST $Client_Path
                else
                        $RM `$RMF $PKGINST $Client_Path`
                fi
	done
	removef -f $PKGINST

	$RM $Our_Deletes
fi

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

exit 0
