#!/bin/ksh
# Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms. 
#
# ident	"@(#)postpatch	1.2	10/03/05 SMI"
#

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

# This function prints out content of the patch messaging file to stdout
#
# Usage:
#
# print_patch_script_messaging
#
print_patch_script_messaging()
{
	TMP_DIRNAME="/var/run"
	MSG_FILENAME="${TMP_DIRNAME}/patchadd_msg_file_${PatchNum}"

	if [ -r "${MSG_FILENAME}" -a -s "${MSG_FILENAME}" ]; then
		/usr/bin/cat "${MSG_FILENAME}"
	fi

	/usr/bin/rm -f "${MSG_FILENAME}"

	return 0
}

# 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}/patchadd_msg_file_${PatchNum}"

	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 function so that it can be overridden if needed.
postpatch()
{
	:
}

# 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
postpatch "$@"
print_patch_script_messaging

exit 0
