#! /bin/sh
#
# Copyright (c) 2005 by Sun Microsystems, Inc.
# All rights reserved.
#
# "@(#)preinstall 1.1     05/05/06 SMI"
#

HPreverse(){
        pr -t -n $1 | sort -rn | cut -f2
}

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

if [ "${OS}" = "SunOS" ]; then
    ECHO="echo"
    REVERSE="tail -r"
    IMPL=solaris_impl
    IMPLX=solaris_implx
fi

if [ "${OS}" = "Linux" ]; then
    ECHO="/bin/echo -e"
    REVERSE="tac"
    IMPL=linux_impl
    IMPLX=linux_implx
fi

if [ "${OS}" = "HP-UX" ]; then
    ECHO="/usr/bin/echo"
    REVERSE=HPreverse
    IMPL=hpux_impl
    IMPLX=hpux_implx
fi

if [ "${OS}" = "AIX" ]; then
    ECHO="/usr/bin/echo"
    REVERSE="tail -r"
    IMPL=aix_impl
    IMPLX=aix_implx
fi

# For Linux, remove old directory leftover when upgrading from 2.x
# so the install can create a symlink for the same path.
if [ "${OS}" = "Linux" ]; then
    WEBCONSOLE_LIB_PATH=/usr/share/webconsole/lib
    if [ -d ${WEBCONSOLE_LIB_PATH} ]; then
        /bin/rm -rf ${WEBCONSOLE_LIB_PATH} > /dev/null 2>&1
    fi
fi

# Upgrade
# This preinstall script will help migrate existing plugins,
# jars, and 2.0 modules to 3.0. In short, earlier verions
# of registry databases that exist on the usr partition.
#

# whitespace character
TIMESTAMP=`/bin/date -u +"%Y"%m"%d"%H"%M"%S""`
WHITESPACE_KEY_PATTERN="_${TIMESTAMP}_"

# plugins
# 1.0 location	/usr/sadm/lib/webconsole/registered_plugins
#     format	context<TAB><SPACE>pluginLocation
# 2.0 location	/usr/share/webconsole/registered_plugins
#     format    context<TAB><SPACE>pluginLocation
PLUGIN10=/usr/sadm/lib/webconsole/registered_plugins
PLUGIN20=/usr/share/webconsole/registered_plugins

# jars
# 1.0 location	/usr/sadm/lib/webconsole/registered_jars
#     format    jar Location
# 2.0 location  /usr/share/webconsole/registered_jars
#     format    scope@jarName<SPACE><SPACE><SPACE>jarLocation
JAR10=/usr/sadm/lib/webconsole/registered_jars
JAR20=/usr/share/webconsole/registered_jars

# modules
# 1.0 - module database is on the root partition and is upgraded in
# preinstall of the root package SUNWmconr
# 2.0    location	/usr/share/webconsole/registered_modules
# format		serviceName|class|behavior|options
MODULE20=/usr/share/webconsole/registered_modules

# default database paths
PLUGIN_DB=""
JAR_DB=""
MODULE_DB=""

# We need to determine if we are upgrading from 1.0 or 2.0
# not all databases will exist, but we shouldn't have both
# on the system at the same time.
# If both sets of databases exist we will default to upgrade 2.0
if [ -f $PLUGIN10 -o -f $JAR10 ]; then
    PLUGIN_DB=$PLUGIN10
    JAR_DB=$JAR10
    MODULE_DB=""
    UPGRADE_SOURCE=1
fi
if [ -f $PLUGIN20 -o -f $JAR20 -o -f $MODULE20 ]; then
    PLUGIN_DB=$PLUGIN20
    JAR_DB=$JAR20
    MODULE_DB=$MODULE20
    UPGRADE_SOURCE=2
fi

# common upgrade destination
WEBCONSOLE_LIB_DIR=${BASEDIR}/usr/lib/webconsole
UPGRADE_RC=${WEBCONSOLE_LIB_DIR}/.upgrade

# make the lib directory if it is not there
if [ ! -d ${WEBCONSOLE_LIB_DIR} ]; then
    mkdir -p ${WEBCONSOLE_LIB_DIR}
fi

# remove the .upgrade file if it is there
if [ -f ${UPGRADE_RC} ]; then
    /bin/rm -f ${UPGRADE_RC} > /dev/null 2>&1
fi

#################################################
#
# Function to do whatever is necessary to upgrade
# existing pre 2.1 apps to this version of the
# console.
# 1.0 format context<TAB><SPACE>pluginLocation
# 2.0 format context<TAB><SPACE>pluginLocation
#
# $1 = list of plugin name/directory pairs
#
#################################################
upgradePlugins() {

     while [ -n "$*" ]
     do
	name=$1
	shift
	dir=$1
	shift

	# Log command such that the registration can be deferred 
	# until next server start.  But skip the console and tags
	# "apps" which will be registered as part of install.
	#
	rawName=`$ECHO ${name} | awk -F_ '{print $1}'`
	if [ "${rawName}" != "com.sun.web.console" -a \
	     "${rawName}" != "com.sun.web.ui" ]; then
	   $ECHO "[ webapp - ${dir} ]" >> ${UPGRADE_RC}
	   $ECHO "context=${name}"  >> ${UPGRADE_RC}
	   $ECHO "location=${dir}"  >> ${UPGRADE_RC}
	   $ECHO "" >> ${UPGRADE_RC}
	fi
    done

} # upgradePlugins


#################################################
#
# Function to do whatever is necessary to upgrade
# existing pre 2.1 library jars to this version of
# the console.
#
# 1.0 format  jarLocation
# 2.0 format  scope@jarName<SPACE><SPACE><SPACE>jarLocation
#
# $1 = list of registered jar/directory pairs
#
#################################################
upgradeJars() {

    while [ -n "$*" ]
    do
	if [ ${UPGRADE_SOURCE} -eq 2 ]; then
	    scope=`$ECHO $1 | awk -F'@' '{print $1}'`
	    name=`$ECHO $1 | awk -F'@' '{print $2}'`
	    shift
	    dir=$1
	    shift
	else
	    name=`basename $1`
	    dir=$1
	    shift
	fi

	# write a record to the upgrade rc file, but
	# skip the console jars which will be registered
	# as part of install.
	if [ "${name}" != "com_sun_management_services_api.jar" -a \
	     "${name}" != "com_sun_management_services_impl.jar" -a \
	     "${name}" != "com_sun_management_cc.jar" -a \
	     "${name}" != "com_sun_management_webcommon.jar" -a \
	     "${name}" != "com_iplanet_jato_jato.jar" -a \
	     "${name}" != "com_sun_management_$IMPL.jar" -a \
	     "${name}" != "com_sun_management_$IMPLX.jar" ]; then
	    $ECHO "[ jar - ${dir} ]" >> ${UPGRADE_RC}
	    $ECHO "location=${dir}"  >> ${UPGRADE_RC}
	    $ECHO "scope=${scope}"  >> ${UPGRADE_RC}
	    $ECHO "" >> ${UPGRADE_RC}
	fi

    done

} # upgradeJars


########################################################
# strip
# function to strip a string of WHITESPACE_KEY_PATTERN
# and write to the UPGRADE_RC file
#
########################################################
strip() {
    while [ -n "$1" ]; do
	a=`echo $1 | sed -e "s@$WHITESPACE_KEY_PATTERN@ @g"`
	echo $a >> ${UPGRADE_RC}
	shift
	done
}

#################################################
#
# Function to do whatever is necessary to upgrade
# existing pre 2.1 login modules to this version of
# the console.
#
# 1.0 modules are  processed by root pkg SUNWmconr
# 2.0 format serviceName|class|behavior|options
#
# $1 = list of registered modules 
#
#################################################
upgradeModules() {

    while [ -n "$*" ]
    do
	line="$1"
	shift

	# 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

	# break up the line on the | field separator
	name=`$ECHO $line | awk -F'|' '{print $1}'`
	class=`$ECHO $line | awk -F'|' '{print $2}'`
	behavior=`$ECHO $line | awk -F'|' '{print $3}'`
	options=`$ECHO $line | awk -F'|' '{print $4}'`

	$ECHO "[ loginmodule - ${class} ]" >> ${UPGRADE_RC}
	$ECHO "service=${name}"  >> ${UPGRADE_RC}
	$ECHO "class=${class}"  >> ${UPGRADE_RC}
	$ECHO "behavior=${behavior}"  >> ${UPGRADE_RC}
	# options are in the form name="value of option"
	# we must place each on a line. Options should be
	# delimited by "$WHITESPACE_KEY_PATTERN
	strip `echo $options | sed "s@\"${WHITESPACE_KEY_PATTERN}@\" @g"`
	echo ""  >> ${UPGRADE_RC}
    done

} # upgradeModules

# process plugins
if [ -f "$PLUGIN_DB" ]; then
    upgradePlugins `cat $PLUGIN_DB | grep -v "^#"`
fi
# process jars
if [ -f "$JAR_DB" ]; then
    upgradeJars `cat $JAR_DB | grep -v "^#"`
fi

# process modules
# options to modules can have spaces these need to
# be filled with a known pattern. When written to
# disk this pattern must be removed
if [ -f "$MODULE_DB" ]; then
    upgradeModules `cat $MODULE_DB | grep -v "^#"\
	| sed -e "s@ @$WHITESPACE_KEY_PATTERN@g"`
fi

exit 0
