#!/bin/ksh
# Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
#
# ident	"@(#)prepatch	1.2	15/01/06 SMI"
#

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

# This function prepares patch messaging infrastructure.
# If message files already exists, it is first printed and then wiped.
# On success it returns 0, on failure it returns 1.
#
# Usage:
#
# prep_patch_script_messaging
#
prep_patch_script_messaging()
{
	TMP_DIRNAME="/var/run"
	MSG_FILENAME="${TMP_DIRNAME}/patchadd_msg_file_${PatchNum}"
	TMP_OWNER="nobody"

	if [ -r "${MSG_FILENAME}" -a -s "${MSG_FILENAME}" ] ; then
		echo
		echo "NOTE: A possible previous instance of patchadd message file file encountered."
		echo "      Please review it."
		echo "----- Old message file content: begin -----"
		/usr/bin/cat "${MSG_FILENAME}"
		echo "-----  Old message file content: end  -----"
		echo
	fi

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

	: > "${MSG_FILENAME}" && \
	/usr/bin/chown "${TMP_OWNER}" "${MSG_FILENAME}" && \
	/usr/bin/chmod 0666 "${MSG_FILENAME}"

	if [ ! -w "${MSG_FILENAME}" ] ; then
		echo "ERROR: Failed to create patch scripts message file."
		return 1
	fi
}

# 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.
prepatch()
{
	if [ `/sbin/zonename` != "global" ] ; then
		return 0
	fi

	SVCCFG_REPOSITORY="${ROOTDIR}/etc/svc/repository.db" \
	    /usr/sbin/svccfg -s svc:/system/sysidtool:net listprop | \
	    grep "filesystem_local.*dependency" > /dev/null 2>&1 && {
		cat <<-EOF
		This patch cannot be applied without rebooting the target boot
		environment to import a prerequisite SMF service. This is a one
		time requirement.

		NOTE: Patch ${PatchNum} ONLY provides supporting functionality
		for zone root filesystems on iSCSI backed storage :

		- Systems deploying zones of this configuration will first
		  require this patch to have been applied. Please reboot the
		  target boot environment in order to apply this patch.

		- All other systems do NOT require the functionality provided by
		  this patch so may safely disregard that the patch did not
		  apply in the current patching session. There is also NO
		  immediate requirement to reboot and reapply this patch.

		Further details are in Alert
		https://support.oracle.com/rs?type=doc&id=1937985.1
		EOF
		exit 1
	}

	return 0
}

# 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
prep_patch_script_messaging || exit $?
prepatch "$@"

exit $?
