#!/bin/ksh

#
# Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
#
#pragma ident	"@(#)prepatch	1.1	12/07/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
BIN_DIR=/usr/share/webconsole/bin
PRIV_BIN_DIR=${ROOTDIR}/usr/share/webconsole/private/bin
ETC_DIR=${ROOTDIR}/etc/webconsole/console
TMP_DIR=${ETC_DIR}/.patch_tmp
RESTART_FLAG=${TMP_DIR}/restart
DEFAULT_DOMAIN_NAME=console
BUN_FILE=/var/svc/manifest/system/webconsole.xml

# Clean up from a potential previous failure
rm -rf ${TMP_DIR}

# Create the new temp directory
mkdir ${TMP_DIR}

# 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 [ "${ROOTDIR}" = "" -o "${ROOTDIR}" = "/" -o "${ROOTDIR}" = "${SAFEMODE_ROOT}" ]; then

    # determine console status, stop if necessary,
    # set flag for restart after the patch is applied
    if [ -x ${BIN_DIR}/smcwebserver ];then
	console_status=`${BIN_DIR}/smcwebserver status -p | awk -F= '{print $2}'`
	if [ "${console_status}" = "yes" ];then
	    ${BIN_DIR}/smcwebserver stop
	    echo "The webconsole has been stopped"
	    touch ${RESTART_FLAG}
	fi
    fi

    # 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
	/usr/sbin/svcadm clear svc:system/webconsole:console
    fi

fi
}

# 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 $?
