#!/bin/ksh

#
# Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
#
#pragma ident	"@(#)postpatch	1.2	17/04/04 SMI"
#

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

# define common locations and temp dirs
PRIV_BIN_DIR=${ROOTDIR}/usr/share/webconsole/private/bin
DEFAULT_DOMAIN_NAME=console
BUN_FILE=/var/svc/manifest/system/webconsole.xml
BIN_DIR=/usr/share/webconsole/bin
ETC_DIR=${ROOTDIR}/etc/webconsole/console/
WEBCONSOLE_TMP_DIR=${ROOTDIR}/var/webconsole/tmp
TMP_DIR=${ETC_DIR}/.patch_tmp
RESTART_FLAG=${TMP_DIR}/restart
WEBCON_PRIV_DIR=${ROOTDIR}/usr/share/webconsole/private
ZFS_APP_DIR=${ROOTDIR}/usr/share/webconsole/webapps/zfs
#CONSOLE_WEBAPP_DIR=${ROOTDIR}/usr/share/webconsole/webapps/console
#NEW_WEB_UI_DIR=${ROOTDIR}/usr/share/webconsole/private/import/com_sun_web_ui
#OLD_WEB_UI_DIR=${ROOTDIR}/usr/share/webconsole/webapps/console/com_sun_web_ui

# 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()
{
# 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
	# restart the server
	${BIN_DIR}/smcwebserver restart
	echo "The webconsole has been restarted"
    fi

    # redeploy ZFS application
    if [ -d "${ZFS_APP_DIR}" ]; then
        zfs_status=`wcadmin list -a | grep zfs`
        if [ -z "${zfs_status}" ]; then
            wcadmin deploy -a console -x zfs ${ZFS_APP_DIR}
        fi
    fi

fi

# set the sticky bit on the var/webconsole/tmp
chmod 1777 ${WEBCONSOLE_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
postpatch "$@"
print_patch_script_messaging

exit 0
