#!/bin/ksh

#
# Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
#
#pragma ident   "@(#)postbackout 1.1     12/10/19 SMI"
#

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

# define common locations and temp dirs
WEBCON_DIR=${ROOTDIR}/usr/share/webconsole
PRIV_BIN_DIR=${ROOTDIR}/usr/share/webconsole/private/bin
BIN_DIR=/usr/share/webconsole/bin
ETC_DIR=${ROOTDIR}/etc/webconsole/console/
TMP_DIR=${ETC_DIR}/.backoutpatch_tmp
RESTART_FLAG=${TMP_DIR}/restart
DEFAULT_DOMAIN_NAME=console
BUN_FILE=/var/svc/manifest/system/webconsole.xml

# 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}/patchrm_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}/patchrm_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.
postbackout()
{
	if [ -f /usr/lib/patch/patch_common_lib ]; then
  	  . /usr/lib/patch/patch_common_lib
  	  InitSafemode
	fi

	# Only execute commands if patching live system.
	if [ "${ROOTDIR}" = "" -o "${ROOTDIR}" = "/" -o "${ROOTDIR}" = "${SAFEMODE_ROOT}" ]; then

    	    # wcremove console domain
    	    ${PRIV_BIN_DIR}/wcremove -i ${DEFAULT_DOMAIN_NAME}

    	    # For s10 and up wcremove will place the console in 
    	    # maintenance mode, so clear before restart
    	    # SMF service bundle file must be installed for S10 up.
    	    # It will not be installed in the package otherwise.
    	    if [ -f ${BUN_FILE} ]; then
		svcadm clear svc:system/webconsole:console
    	    fi

    	    # determine console status, start if necessary,
    	    # set flag for restart after the patch is applied
    	    if [ -f ${RESTART_FLAG} ];then
		# start the server
		${BIN_DIR}/smcwebserver restart
    	    fi

	fi

	# remove temporary directory
	rm -rf ${TMP_DIR}

}

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

exit 0
