#!/bin/sh
#
# ident "@(#)postinstall	1.8	09/05/03 SMI"
#
# Copyright 2003-2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

OS=`uname -s`
if [ ! -n "${OS}" ]; then
    echo "Unable to determine operating system."
    exit 1
fi

if [ "${OS}" != "SunOS" -a "${OS}" != "Linux" -a "${OS}" != "HP-UX" -a "${OS}" != "AIX" ]; then
    echo "Not supported on detected OS \"${OS}\"."
    exit 1
fi

RELOCATE=""
if [ -n "${PKG_INSTALL_ROOT}" ]; then
    RELOCATE="-R ${PKG_INSTALL_ROOT}"
fi

CONSOLE_BASE=${BASEDIR}/var/opt/webconsole
CONSOLE_APPBASE=${BASEDIR}/var/opt/webconsole/webapps
INSTALL_DATA=${CONSOLE_BASE}/.install_data
CONSOLE_CONF=${BASEDIR}/etc/webconsole
CONSOLE_INSTANCE=console
CONFIG_PROPERTIES=config.properties
INSTANCE_DIR=${CONSOLE_CONF}/${CONSOLE_INSTANCE}
CONFIG_PROP=${INSTANCE_DIR}/${CONFIG_PROPERTIES}

# Extract 3rd-party login modules from JAAS configuration file and
# save them off in a seperate file in "registered_modules" format.
# This file will be merged with the authoritative module database
# when the server is restarted.

JAAS_UPGRADE=${CONSOLE_BASE}/.upgradeLoginModules
mkdir -p `dirname $JAAS_UPGRADE`

# 3rd-party modules can exist when upgrading from Lockhart 1.0 (in
# which case the config file is in /etc/sadm/...), or an upgrade from
# 2.x or later (in which case it's in /etc/opt/...).
if [ -f ${BASEDIR}/etc/sadm/webconsole/consolelogin.conf ]; then
    mv ${BASEDIR}/etc/sadm/webconsole/consolelogin.conf \
	${BASEDIR}/etc/opt/webconsole
fi
JAAS=${BASEDIR}/etc/opt/webconsole/consolelogin.conf

if [ -f $JAAS ]; then
    rm -f $JAAS_UPGRADE > /dev/null
    cat $JAAS | \
    while read line
    do
	# Skip everything until we find a comment line containing START
	echo $line | grep START > /dev/null
	if [ $? -ne 0 ]; then
	    continue
	fi

	# Found START of module configuration.  Determine if it's a module
	# we should upgrade.
	#
	while read line
	do
	    # Break loop if we detect END of module configuration.
	    echo $line | grep END > /dev/null
	    if [ $? -eq 0 ]; then
		break
	    fi

	    # Ignore all comments and blank lines.
	    firstChars=`echo $line | awk '{print substr($1, 1, 2)}'`
	    if [ ! -n "${firstChars}" -o "${firstChars}" = "//" ]; then
		continue
	    fi

	    # Ignore console-specific PamLoginModule and RbacRoleLoginModule.
	    echo $line | grep PamLoginModule > /dev/null
	    if [ $? -eq 0 ]; then
		break
	    fi
	    echo $line | grep RbacRoleLoginModule > /dev/null
	    if [ $? -eq 0 ]; then
		break
	    fi

	    # If we get here, we've detected a 3rd-party module.
	    # Extract the module configuration.  Remove trailing ';' from
	    # last config parameter, as this is not needed in  module
	    # registration database.

	    service=ConsoleLogin
	    module=`echo $line | awk '{print $1}'`
	    behavior=`echo $line | awk '{print $2}' | sed -e "s/;//"`

	    # Everything up to the next comment line containing END 
	    # are options.
	    options=""
	    while read line
	    do
		# Break loop if we detect END of module configuration.
		echo $line | grep END > /dev/null
		if [ $? -eq 0 ]; then
		    break
		fi

		# Skip blank lines
		if [ ! -n "${line}" ]; then
		    continue
		fi

		# Concatenate options, removing trailing ';', as this is not
		# needed in module registration database.
		if [ -n "${options}" ]; then
		    options="${options} ${line}"
		else
		    options=${line}
		fi
		options=`echo $options | sed -e "s/;//"`
	    done

	    echo "$service|$module|$behavior|$options" >> $JAAS_UPGRADE
	    break
	done

    done
fi


if [ "${OS}" = "SunOS" ]; then

    # Purge 1.0 files which are obsolete.  These would be /var/sadm/webconsole
    # (for which nothing was registered with the pkg database) and 
    # /etc/sadm/webconsole (for which there are pkg registrations).
    # Purge both of these, but migrate over keystore to the new location and
    # delete the JAAS config file, neither of which are registered.
    # We extract the paths from the pkg database, and then reverse-sort
    # them so that we remove files 1st before the directories that
    # contain them.
    #
    if [ -d ${BASEDIR}/var/sadm/webconsole ]; then
	rm -rf ${BASEDIR}/var/sadm/webconsole
    fi
    rm -f ${JAAS} >/dev/null
    if [ -f ${BASEDIR}/etc/sadm/webconsole/keystore ]; then
	mv ${BASEDIR}/etc/sadm/webconsole/keystore /etc/opt/webconsole
    fi
    fileslist=/tmp/${PKGINST}.$$
    rm -f ${fileslist}
    env LANG=C LC_ALL=C pkgchk -l ${RELOCATE} ${PKGINST} \
	| grep -i pathname | grep "etc/sadm" \
	| awk '{print $2}' | sort -r > ${fileslist}

    if [ -s ${fileslist} ]; then
	# Lock pkg database to remove each entry from the DB, and
	# then delete the file.  Note that we pump a file list into
	# a single call to removef, which is ALOT faster than calling
	# removef individually for each file.
	#
	removef ${RELOCATE} ${PKGINST} `cat ${fileslist}` | \
	    while read pathname
	    do
		rm -rf $pathname
	    done
	removef ${RELOCATE} -f ${PKGINST}
    fi
    rm -f ${fileslist}
fi

if [ "${OS}" = "Linux" ]; then

    # The layout of /etc/init.d, /etc/rc.d, and /etc/rc<N>.d differs amongst
    # the various Linux distributions in that which directories are real files
    # vs symbolic links varies.  So we must use the public interface for
    # setting runlevels for system services.
    #
    /sbin/chkconfig --add webconsole > /dev/null

    # On Redhat Linux, the convention is that the runlevels are set in a 
    # comment in the service file.  See chkconfig(8) for details.
    # But on SuSE Linux, it is necessary to specifically set the runlevels.
    # Soooo, we extract the levels from the file and set'em.
    #
    service=/etc/init.d/webconsole
    if [ -f /etc/SuSE-release ]; then
	if [ -f $service ]; then
	    config=`grep "^# chkconfig:" $service` 2>&1
	    if [ $? -eq 0 ]; then
		levels=`echo $config | awk '{print $ 3}'`
		if [ "$levels" != "-" ]; then
		    /sbin/chkconfig `basename $service` $levels > /dev/null
		fi
	    fi
	fi
    fi
fi

##########################################################
#
# Replace a specified property name in config.properties
# with a specified value.
#
# $1 = the property name
# $2 = the property value
#
##########################################################
replace_config_prop() {

    propName=$1
    propValue=$2

    CONFIG_PROP_TMP=/tmp/${CONFIG_PROPERTIES}.$$
    rm -f $CONFIG_PROP_TMP

    if [ -n "$propName" ]; then
	cat $CONFIG_PROP | \
	while read line
	do
	    # Ignore empty lines or lines starting with a comment character.
	    firstChar=`echo $line | awk '{print substr($1, 1, 1)}'`
	    if [ ! -n "${firstChar}" -o "${firstChar}" = "#" ]; then
		echo $line >> $CONFIG_PROP_TMP
		continue
	    fi

	    # Extract the property name
	    name=`echo $line | awk -F= '{print $1}'`

	    # Found it, so replace with new value.
	    if [ "${name}" = "$propName" ]; then
		echo "$propName=$propValue" >> $CONFIG_PROP_TMP
		continue
	    fi

	    # Just keep it.
	    echo $line >> $CONFIG_PROP_TMP
	done
	mv $CONFIG_PROP_TMP $CONFIG_PROP
	rm -f $CONFIG_PROP_TMP
    fi
}

# For bundled Solaris, while a default configuration is already installed, we
# must provide a way for other installers to customize the configuration
# during installation.  While there are several ways to this in the normal
# case, when installing into a local zone we can only do this thru the
# pkg environment.  And so we allow certain environment variables to
# override the default configuration.
#
if [ "${OS}" = "SunOS" ]; then

    #####  THIS SECTION WOULD ONLY APPLY IF A HIGHER LEVEL       #####
    #####  INSTALLER KNOWS THE NAMES OF CONFIGURATION PROPERTIES #####

    # We do NOT allow adding new properties to the configuration, but
    # only replacement of values for properties that are already defined.
    # So we first get a list of the properties defined in config.properties.
    #
    propNamesFile=/tmp/propNames.$$
    rm -f $propNamesFile
    cat $CONFIG_PROP | \
    while read line
    do
	# Ignore empty lines or lines starting with a comment character.
	firstChar=`echo $line | awk '{print substr($1, 1, 1)}'`
	if [ ! -n "${firstChar}" -o "${firstChar}" = "#" ]; then
	    continue
	fi

	# Extract the property name and append it to the names file
	name=`echo $line | awk -F= '{print $1}'`
	echo "$name \c" >> $propNamesFile
    done

    # Then loop thru the names, checking if it is defined in the environment.
    # If it is, get the value and replace the existing value in 
    # config.properties.
    #
    # Note that variables in the pkg environment MUST begin with an
    # upper-case letter, otherwise they will not show up in the env.
    # So we look for names prefixed by "SWC_" in the environment to
    # get their values, but update the name sans the prefix in
    # config.properties.
    #
    for name in `cat $propNamesFile`; do
	eval 'test -n "$'SWC_${name}'"'
	if [ $? -eq 0 ]; then
	    value=`eval 'echo "$'SWC_${name}'"'`
	    replace_config_prop $name $value
	fi
    done

    rm -f $propNamesFile


    #####  THIS SECTION ONLY APPLIES WHEN BEING INSTALLED BY  #####
    #####  THE JES INSTALLER.  SPECIFIC ENVIRONMENT VARIABLES #####
    #####  ARE MAPPED TO SPECIFIC PROPERTY NAMES.             #####

    # JES uses JDK_LOCATION to specify the java_home property
    if [ -n "${JDK_LOCATION}" ]; then
	replace_config_prop java_home $JDK_LOCATION
    fi

    # JES uses WEBSERVICES_BASE to specify a *basedir* under which
    # Jato can be found.  So we append the path to the Jato directory.
    if [ -n "${WEBSERVICES_BASE}" ]; then
	replace_config_prop jato_home $WEBSERVICES_BASE/usr/share/lib/jato
    fi

    # JES uses SUNWJAVAHELP_BASEDIR to specify a *basedir* under which
    # Javahelp can be found.  So we append the path to the Javahelp directory.
    if [ -n "${SUNWJAVAHELP_BASEDIR}" ]; then
	replace_config_prop javahelp_home \
		$SUNWJAVAHELP_BASEDIR/jdk/packages/javax.help-2.0
    fi

    #### THIS SECTION APPLIES FOR BUNBLED, UNBUNDLED, AND JES  ####
    #### INSTALLATIONS.  SET THE SMF SERVICE ENABLED STATE     ####
    #### APPROPRIATELY.  IF BUNDLED, SET TO ENABLED, OTHERWISE ####
    #### CHECK SAVED STATE IN SITE ADMIN PROPERTIES FILE.      ####
    #### IF ENABLED, SET THE ENABLE ON NEXT BOOT STATE SO THAT ####
    #### SERVICE DOES NOT START IMMEDIATELY FROM THIS PACKAGE. ####
    #### UNBUNBLED INSTALLERS MUST SET SWC_unbundled_install   ####
    #### PROPERTY TO TRUE IN THE RESPONSE FILE.                ####

    # SMF service bundle file must be installed for S10 up.
    # It will not be installed in the package otherwise.
    bun_file=${BASEDIR}/var/svc/manifest/system/webconsole.xml
    if [ -f ${bun_file} ]; then
	fmri="system/webconsole:console"
	if [ -n "${SWC_unbundled_install}" ]; then

	    # Unbundled install checks saved state in case of upgrade.
	    # If was enabled, reset service for enabled on next reboot
	    # (so service does not start immediately).  Service must be
	    # manually started after an unbundled install.

	    state=""
	    svcfile=${BASEDIR}/etc/webconsole/console/service.properties
	    if [ ! -f ${svcfile} ]; then
		svcfile=${BASEDIR}/etc/opt/webconsole/webconsole
	    fi
	    if [ -f ${svcfile} ]; then
		state=`grep -i "ENABLE=" ${svcfile} | cut -f2 -d"="`
	    fi
	    if [ "${state}" = "yes" -o "${state}" = "YES" -o "${state}" = "Yes" ]; then
		# We depend on the class action script actually registering
		# the service with SMF so following commands will work.
		/usr/sbin/svcadm disable -t ${fmri}
		/usr/sbin/svccfg -s ${fmri} setprop general/enabled = true
	    fi

	else

	    # Bundled install always sets for enabled state.
	    # We use a contract private interface to enable the service
	    # on initial boot after a bundled install.  This code
	    # should be moved into an SMF profile some day.
	    # Note we do delayed enable only for bundled, live upgrade,
	    # and diskless installs; not for vanilla package install.

	    if [ "${PKG_INSTALL_ROOT}" != "" -a "${PKG_INSTALL_ROOT}" != "/" ]; then
		up_file=${PKG_INSTALL_ROOT}/var/svc/profile/upgrade
		if [ ! -f ${up_file} ]; then
		    touch ${up_file}
		fi
		echo "/usr/sbin/svcadm enable ${fmri}" >> ${up_file}
	    fi

	fi
    fi


    # Create pkg registration entry for config.properties
    installf -c none ${RELOCATE} ${PKGINST} ${CONFIG_PROP} f 444 root bin
    installf -f ${RELOCATE} ${PKGINST}

fi

exit 0
