# 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.10	10/03/05 SMI"
#
# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms. 
#

# Definitions for override safety mechanism
PATCH_OVERRIDE_LIB="${PKG_INSTALL_ROOT}/usr/lib/patch/patch_override_lib"

# Check if we messaging is ready. Returns 0 on success, 1 on failure
# On successful run it leaves MSG_FILENAME variable defined
# 
# Usage:
#
# check_patch_script_messaging
#
check_patch_script_messaging()
{
	TMP_DIRNAME="/var/run"
	MSG_FILENAME="${TMP_DIRNAME}/patchrm_msg_file_${ACTIVE_PATCH}"

	if [ -w "${MSG_FILENAME}" ] ; then
		unset TMP_DIRNAME
		return 0
	else
		unset TMP_DIRNAME
		unset MSG_FILENAME
		return 1
	fi
}

# This function prepares a message to be printed to stdout towards the end
# of postpatch execution. If the message file is not ready, it at least tries
# immediate output to stdout which is currently know to work only in patch
# level scripts.
#
# Usage:
#
# print_patch_message "text_of_message_to_be_printed_out"
#
print_patch_message()
{
	if check_patch_script_messaging; then
		echo "$*" >> "${MSG_FILENAME}"
	else
		echo "$*"
	fi

	return 0
}

# script in a fucntion so that it can be overridden if needed.
patch_postinstall()
{
	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
}

# load the override lib if it exists
if [ -f "${PATCH_OVERRIDE_LIB}" -a -r "${PATCH_OVERRIDE_LIB}" ] ; then
   . "${PATCH_OVERRIDE_LIB}"
fi

# execute the script function
patch_postinstall "$@"

exit 0
