#! /bin/sh -x
#
# ident	"@(#)preinstall	1.10	05/05/13 SMI"
#
# Copyright 2003-2004 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

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

CONSOLE_BASE=$BASEDIR/var/opt/webconsole

# 3.0 prefix for user defined options to login modules
OPTION_PREFIX="option_"

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

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

########################################################
# writeOptions
# write options to the JAAS_UPGRADE file
#
########################################################
writeOptions() {

    userOpts=""
    while [ -n "$1" ]; do
	p=`echo ${1} \
	    | sed -e "s@$WHITESPACE_KEY_PATTERN@ @g" -e 's@\"@@g'`
	if [ -n "${userOpts}" ]; then
	    userOpts="${userOpts}${OPTION_PREFIX}${p}\n"
	else
	    userOpts="${OPTION_PREFIX}${p}\n"
	fi
	shift
    done

    # strip the remaing white space
    strip `echo $userOpts \
	| sed "s@\"${WHITESPACE_KEY_PATTERN}@\" @g"`

}

#################################################
#
# Function to do to upgrade pre 2.1 login modules
# to the 3.0 console
#
# this code previously existed in
# SUNWmconr postinstall
#
#################################################
upgradePre21Modules() {

    # 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.


    # 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
    else
	# nothing to update
	return
    fi

    JAAS_UPGRADE=${CONSOLE_BASE}/.upgradeLoginModules
    mkdir -p `dirname $JAAS_UPGRADE` >/dev/null 2>&1

    # remove it if it exists
    rm -f ${JAAS_UPGRADE} >/dev/null 2>&1
    touch ${JAAS_UPGRADE}

    JAAS=${BASEDIR}/etc/opt/webconsole/consolelogin.conf

    if [ -f $JAAS ]; then
	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 "[ loginmodule - ${module} ]" >> $JAAS_UPGRADE
		echo "service=${service}"  >> $JAAS_UPGRADE
		echo "class=${module}"  >> $JAAS_UPGRADE
		echo "behavior=${behavior}" >> $JAAS_UPGRADE

		# options are in the form name="value of option"
		# we must place each on a line. Options should be
		# delimited by "$WHITESPACE_KEY_PATTERN
		options=`$ECHO $options \
		    | sed -e "s@ @$WHITESPACE_KEY_PATTERN@g"`

		writeOptions "$options"
		break
	    done

	done
    fi

} # upgradePre21Modules

# We must blow away Tomcat's work directory because there may be
# compiled JSP's which are not compatible with the new bits we are
# about to install.  One would think that installing new JSP's would
# trigger an automatic re-compile, but it doesn't appear so.
WORK_DIR=$CONSOLE_BASE/work
if [ -d $WORK_DIR ]; then
    rm -rf $WORK_DIR
fi

upgradePre21Modules

exit 0
