#!/bin/sh
#
#ident "@(#)swcupdateconfig	1.2 06/02/13 SMI"
# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
#

###############
## Constants ##
###############

# Solaris console configuration directory
SUN_OS_CONSOLE_CONF=/etc/webconsole

# Linux console configuration directory
LINUX_OS_CONSOLE_CONF=/etc/opt/webconsole

# HP-UX console configuration directory
HPUX_OS_CONSOLE_CONF=/etc/opt/webconsole

# Solaris path to javahelp 
JAVAHELP_PATH_SOLARIS=jdk/packages/javax.help-2.0

# Linux path to javahelp
JAVAHELP_PATH_LINUX=java/packages/javax.help-2.0/javahelp

# HP-UX path to javahelp
JAVAHELP_PATH_HPUX=share/lib/javahelpruntime/javahelp

# config properties file name
CONFIG_PROPS_FILE=config.properties

# console instance name
CONSOLE_INSTANCE=console

# lockhart config property names
JAVA_HOME_PROP=java_home
JAVAHELP_HOME_PROP=javahelp_home
JATO_HOME_PROP=jato_home

# JES statefile property names
JES_JAVA_HOME_PROP=JDK_LOCATION
JES_JATO_HOME_PROP=WEBSERVICES_BASE
JES_JAVAHELP_HOME_PROP=SUNWJAVAHELP_BASEDIR


# path to jato
JATO_PATH=usr/share/lib/jato

# path to jato
JATO_PATH_HPUX=share/lib

# Default OS names
SUN_OS="SunOS"
LINUX="Linux"
HPUX="HP-UX"

# update Comment
COMMENT="# This file has been updated by JES Install"

# temporary properties file
TMP_CONFIG_FILE=tmp.properties

#
# getStateFile
# get the statefile form the commandline
getStateFile() {
    # make sure we have a statefile cmdline arg
    if [ ! -n $1 ]; then
	exit 0
    fi

    STATE_FILE=$1
}

#
# getOS
# use uname -a to determine the OS
# this will be used later to determine the location of
# the config.properties file
#
getOS() {
    # read the OS
    OS=`uname -s`

    # default to SunOS
    if [ ! -n "${OS}" ]; then
	OS=${SUN_OS}
    fi
}


#
# buildOSDependentPaths
# build paths that are specific to the OS we are installing on
# These include the base paths to the config.properties file
# and the location of javahelp.
#
buildOSDependentPaths() {

    # initialize path to config.properties file
    CONFIG_PROPS_PATH=""

    # based on OS
    if [ -n ${OS} ]; then
	if [ ${OS} = ${SUN_OS} ]; then
	    # build base path to the config.properties file
	    CONFIG_PROPS_PATH=${SUN_OS_CONSOLE_CONF}/${CONSOLE_INSTANCE}
	    # build base path for javahelp
	    JAVAHELP_PATH=${JAVAHELP_PATH_SOLARIS}	
	fi
	if [ ${OS} = ${LINUX} ]; then
	    # build base path to the config.properties file
	    CONFIG_PROPS_PATH=${LINUX_OS_CONSOLE_CONF}/${CONSOLE_INSTANCE}
	    # build base path for javahelp
	    JAVAHELP_PATH=${JAVAHELP_PATH_LINUX}
	fi
	if [ ${OS} = ${HPUX} ]; then
	    # build base path to the config.properties file
	    CONFIG_PROPS_PATH=${HPUX_OS_CONSOLE_CONF}/${CONSOLE_INSTANCE}
	    # build base path for javahelp
	    JAVAHELP_PATH=${JAVAHELP_PATH_HPUX}
	    # build base path for jato
	    JATO_PATH=${JATO_PATH_HPUX}
	fi
    fi
}

#
# prelimChecks
# make preliminary checks
#
prelimChecks() {

    #
    # failure of any check exits with 0
    # and the script will not proceed
    #

    # make sure statefile exists
    if [ ! -f ${STATE_FILE} ]; then
	exit 0
    fi

    # make sure config.properties exists
    if [ ! -f ${CONFIG_PROPS_PATH}/${CONFIG_PROPS_FILE} ]; then
	exit 0
    fi

    # make sure we can write to the config.properties
    echo "${COMMENT}" >> ${CONFIG_PROPS_PATH}/${CONFIG_PROPS_FILE}
    if [ $? -ne 0 ]; then
	exit 0
    fi   
}

#
# getJESProp
# read the value of the passed in prop from
# the JES statefile that was passed in on the
# command line
#
getJESProp() {

    # if $1 is empty return
    if [ ! -n $1 ]; then
	return 0
    fi

    # save parameter to a more meaningful variable
    PROP_NAME=$1

    # search for the proper
    line=`grep ${PROP_NAME} ${STATE_FILE}`
    if [ $? -eq 0 ]; then
	value=`echo ${line} | awk -F= '{print $2}' | sed 's@ @@g'`
    fi

    echo ${value}
}

#
# updateConfigProps
# update the given property in the config.properties file
# with the given value.
#
updateConfigProps() {

    # place params in meaningful names
    name=$1
    value=$2

    # make sure we don't have empties
    if [ ! -n ${name} ]; then
	return
    fi

    if [ ! -n ${value} ]; then
	return
    fi

    # remove double slashes from value
    value=`echo ${value} | sed 's@//@/@g'`

    # Build new name value pair
    newNVPair="${name}=${value}"

    # dump the contents of config.properties to a temp file
    # excluding the property we are updating in the process
    grep -v ${name} \
	${CONFIG_PROPS_PATH}/${CONFIG_PROPS_FILE} \
	> ${CONFIG_PROPS_PATH}/${TMP_CONFIG_FILE}
        
    # append the new name=value pair to the end of the temp file
    echo ${newNVPair} >> ${CONFIG_PROPS_PATH}/${TMP_CONFIG_FILE}

    # overwrite config.properties
    mv ${CONFIG_PROPS_PATH}/${TMP_CONFIG_FILE} \
	${CONFIG_PROPS_PATH}/${CONFIG_PROPS_FILE}
}

##########
## MAIN ##
##########

#
# get statefile
#
getStateFile $1

#
# determine the OS
#
getOS

#
# build a paths that are specific to our target OS
#
buildOSDependentPaths

#
# make preliminary checks
#
prelimChecks

#
# get properties we need from the JES statefile
#

# JAVA_HOME
CONFIG_JAVA_HOME=`getJESProp ${JES_JAVA_HOME_PROP}`

# JAVA_HELP_HOME
CONFIG_JAVAHELP_HOME=`getJESProp ${JES_JAVAHELP_HOME_PROP}`

# JATO_HOME
CONFIG_JATO_HOME=`getJESProp ${JES_JATO_HOME_PROP}`

#
# update the config.properties file
#

# java home
updateConfigProps  ${JAVA_HOME_PROP} ${CONFIG_JAVA_HOME}

# javahelp home
updateConfigProps ${JAVAHELP_HOME_PROP} \
    ${CONFIG_JAVAHELP_HOME}/${JAVAHELP_PATH}

# jato home
updateConfigProps ${JATO_HOME_PROP} \
    ${CONFIG_JATO_HOME}/${JATO_PATH}
