#!/bin/sh
#
#ident "@(#)swcstop	1.3 06/08/23 SMI"
# Copyright (c) 2006 Sun Microsystems, Inc.
# All Rights Reserved.

# Console stop server script.
# Called by JES installer and setup script before removing packages.

# Checks if console server is running and stops it (all platforms).
# For Solaris 10 and up, stops server only in global zone.
# If console SMF service is registered, retrieve enabled state and save it.

# Fixed constants for 3.x console
CONSOLE_CONF=/etc/webconsole
CONSOLE_INST=console
SERVICE_NAME=system/webconsole
ENABLE_PROP=general/enabled

# Command to use to stop the console for all versions.
# Note: May be an issue for relocated consoles in JES 6!
SMCX=/usr/sbin/smcwebserver

# SMF service instance fmri
FMRI=${SERVICE_NAME}:${CONSOLE_INST}

# Total wait time for SMF service to stop (seconds)
WAIT_TIME=60

################################################################
# Wait for the console SMF service to stop.
#

waitOnSMF() {

    t1=0
    while [ $t1 -lt $WAIT_TIME ]; do
	temp=`/usr/bin/svcs -H ${FMRI}`
	if [ $? -ne 0 ]; then
	    break
	fi
	state=`echo "${temp}" | cut -f1 -d" "`
	if [ "${state}" = "disabled" -o "${state}" = "DISABLED" ]; then
	    break
	fi
	if [ "${state}" = "maintenance" -o "${state}" = "MAINTENANCE" ]; then
	    break
	fi
	if [ "${state}" = "uninitialized" -o "${state}" = "UNINITIALIZED" ]; then
	    break
	fi
	sleep 3
	t1=`(expr $t1 + 3)`
    done

}

################################################################
# Main
#
# If Solaris 10 and later, save SMF enabled state.
# Some versions of Solaris 10 do not have the 3.x console bundled,
# but these cases fall through because no console SMF service is defined.
# Set the current server SMF running state for later testing (STATE).
# Save indicator that the service must be disabled later (DISABLE).

STATE=""
DISABLE=""
OS=`uname -s`
if [ "${OS}" = "SunOS" ]; then
    VS=`uname -r`
    if [ "${VS}" != "5.8" -a "${VS}" != "5.9" ]; then
	# Solaris 10 and up, SMF is available.
	temp=`/usr/bin/svcs -H ${FMRI}`
	if [ $? -eq 0 ]; then
	    # Got a registered console service, so 3.x or later.
	    # Save running state and always force a disable after stop.
	    STATE=`echo "${temp}" | cut -f1 -d" "`
	    DISABLE="true"
	    value=""
	    temp=`/usr/bin/svcprop -C -p ${ENABLE_PROP} ${FMRI}`
	    if [ "${temp}" = "true" -o "${temp}" = "TRUE" ]; then
		value="yes"
	    fi
	    svcfile=${CONSOLE_CONF}/${CONSOLE_INST}/service.properties
	    # If the ENABLE property exists in the service file,
	    # must go through the logic to reset it to yes or remove it.
	    temp=""
	    if [ -f ${svcfile} ]; then
		temp=`grep -i "^ENABLE=" ${svcfile}`
	    fi
	    if [ -n "${temp}" -o "${value}" = "yes" ]; then
		tmpfile=${CONSOLE_CONF}/${CONSOLE_INST}/service.tmp
		rm -f ${tmpfile} > /dev/null 2>&1
		if [ -f ${svcfile} ]; then
		    sed -e "/^ENABLE=/d" ${svcfile} > ${tmpfile}
		else
		    echo "#Console shared services properties" > ${tmpfile}
		fi
		if [ "${value}" = "yes" ]; then
		    echo "ENABLE=yes" >> ${tmpfile}
		fi
        	rm -f ${svcfile} > /dev/null 2>&1
		mv ${tmpfile} ${svcfile}
		chmod 644 ${svcfile}
	    fi
	fi
    fi
fi

# If console server is running, stop its execution.
# This is done on all Unix platforms.
# If a Solaris SMF service, force to disabled state.
# Must special case 3.0 and 3.0.1 due to Solaris SMF issues.
if [ -x $SMCX ]; then
    temp=`${SMCX} status -p | grep -i "running=" | cut -f2 -d"="`
    vers=`${SMCX} --version | cut -f2 -d" "`
    if [ "${temp}" = "yes" ]; then
	# Console server is running.
	if [ "${vers}" = "3.0" -a "${STATE}" = "online" ]; then
	    # Version 3.0 started as an SMF service.
	    # Must be stopped via SMF.  Wait for service to stop.
	    /usr/sbin/svcadm disable ${FMRI}
	    waitOnSMF
	else
	    # Stop via smcwebserver command
	    ${SMCX} stop
	    if [ $? -ne 0 ]; then
		echo "Error stopping Sun Java Web Console server" 2>&1
	    fi
	fi
	# If version 3.0.1 using SMF, must wait for the service to stop.
	if [ "${vers}" = "3.0.1" -a "${DISABLE}" = true ]; then
	    waitOnSMF
	fi
    fi
    if [ "${DISABLE}" = "true" ]; then
	# What a bit after disabling to give SMF time to finish.
	# Only resetting the enabled property since service is stopped.
	/usr/sbin/svcadm disable ${FMRI}
	sleep 2
    fi
fi

exit 0
