# 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.8	08/08/13 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"

# Set LC_ALL to avoid risk of undefined behavior
LC_ALL=C
export LC_ALL

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"
	UNREGISTER_LIST=/var/run/.$$.unregister.paths.$$
	UNREGISTER_BATCH=/var/run/.$$.unregister.batch.$$
	/usr/bin/rm -f $UNREGISTER_LIST

	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
		echo "$Client_Path" >> $UNREGISTER_LIST
	done

	# We need to group the deletes entries for performance reasons. Only 100
	# entries are taken in a single batch, since we don't want to exceed the
	# number of arguments passed during removef program invocation, and
	# 100 seems to a good tradeoff.

	batch_start_line=1
	batch_end_line=100
	if [ -s "$UNREGISTER_LIST" ]; then
		while [ 1 = 1 ] ; do
			/usr/bin/sed -n "$batch_start_line,$batch_end_line p" $UNREGISTER_LIST > $UNREGISTER_BATCH
			[ -s $UNREGISTER_BATCH ] || break
			if [ "$SAFEMODE_INSTALL" = "true" ]; then
				# Handle deletion of an object for safemode patching
				HandleSafemodeDeleteObject $PKGINST "`/usr/bin/tr '\n' ' ' < $UNREGISTER_BATCH`" 
			else
				# Remove the file only if it's OK'd by removef
				/usr/sbin/removef $PKGINST `/usr/bin/tr '\n' ' ' < $UNREGISTER_BATCH` | while read path ; do
					/usr/bin/rm "$path"
				done
			fi
			batch_start_line=`/usr/bin/expr $batch_start_line + 100`
			batch_end_line=`/usr/bin/expr $batch_end_line + 100`
		done
		removef -f $PKGINST
	fi
	/usr/bin/rm -f $UNREGISTER_LIST $UNREGISTER_BATCH

	$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
