#!/bin/sh

# Copyright  2004 Sun Microsystems, Inc. All rights reserved. 
# 
# Sun Microsystems, Inc. has intellectual property rights relating to
# technology embodied in the product that is described in this document.
# In particular, and without limitation, these intellectual property rights
# may include one or more of the U.S. patents listed at
# http://www.sun.com/patents and one or more additional patents or pending
# patent applications in the U.S. and in other countries.
# 
# U.S. Government Rights - Commercial software. Government users are subject
# to the Sun Microsystems, Inc. standard license agreement and applicable
# provisions of the FAR and its supplements.
# 
# Use is subject to license terms. 
# 
# This distribution may include materials developed by third parties. Sun,
# Sun Microsystems, the Sun logo, Java and Sun[tm] ONE are trademarks or
# registered trademarks of Sun Microsystems, Inc. in the U.S. and other
# countries. 
# 
# Copyright  2004 Sun Microsystems, Inc. Tous droits rservs. Sun
# Microsystems, Inc. dtient les droits de proprit intellectuels relatifs
#  la technologie incorpore dans le produit qui est dcrit dans ce document.
# En particulier, et ce sans limitation, ces droits de proprit
# intellectuelle peuvent inclure un ou plus des brevets amricains lists
#  l'adresse http://www.sun.com/patents et un ou les brevets supplmentaires
# ou les applications de brevet en attente aux Etats - Unis et dans les
# autres pays.
# 
# L'utilisation est soumise aux termes du contrat de licence.
# 
# Cette distribution peut comprendre des composants dvelopps par des
# tierces parties.
# 
# Sun, Sun Microsystems, le logo Sun, Java et Sun[tm] ONE sont des marques
# de fabrique ou des marques dposes de Sun Microsystems, Inc. aux
# Etats-Unis et dans d'autres pays.


#All defines are listed here..
#General utils
platform="`uname`"
if [ ${platform} = "Linux" ]
then
   ID=/usr/bin/id
   ECHO=/bin/echo
   DATE=/bin/date
   SED=/bin/sed
   AWK=/bin/awk
   RM=/bin/rm
   CP=/bin/cp
   DIRNAME=/usr/bin/dirname
   CUT=/bin/cut
   GETTEXT=/bin/gettext
else
   ID=/usr/bin/id
   ECHO=/usr/bin/echo
   DATE=/usr/bin/date
   SED=/usr/bin/sed
   AWK=/usr/bin/awk
   RM=/usr/bin/rm
   DIRNAME=/usr/bin/dirname
   CUT=/usr/bin/cut
   CP=/usr/bin/cp
   GETTEXT=/usr/bin/gettext
fi


#Script Specific defines

. IS_INSTALL_ETCDIR/IS_PRODNAME/config/amProfile.conf

#Logfile location where the output is logged.
LOGFILE=/var/tmp/amsessionconfig.log

#i18n stuff
TEXTDOMAIN=amhasetup
TEXTDOMAINDIR=${IS_INSTALL}/locale
export TEXTDOMAIN
export TEXTDOMAINDIR



AM_MODIFYCONFIG_SUCCEED=0

AM_CONFIGPROPFILE_NOTFOUND=1
AM_CONFIGPROPFILE_CANNOTREAD=2

AM_INVALID_VALUE=3

AM_USAGE_ERROR=4

#Set input Variables 
IHOST=
IPORT=
IPROTOCOL=
IFLAG=


# Checks if the user entered ! in response to a question and prompts again
myread()
{
    while true
    do
        read NOBANG
        if [ "x$NOBANG" = "x!" ]; then
            ${ECHO} "`${GETTEXT} 'Please do not enter the character !'`"
            ${ECHO} "`${GETTEXT} 'Please enter the input again.'`"
            continue
        else
            eval $1=$NOBANG
            break
        fi
    done
}


#Check if all the IS configuration file is present and readable
CheckConfigFiles()
{
    # Check if the AMConfig.properties file exists and readable
    if [ ! -f "${AMCONFIGPROPFILE}" ]
    then
        ${ECHO} "`${GETTEXT} 'Access Manager configuration file does not exist. Please make sure it is present.'`"
	exit ${AM_CONFIGPROPFILE_NOTFOUND}
    fi
    if [ ! -r "${AMCONFIGPROPFILE}" ]
    then
        ${ECHO} "`${GETTEXT} 'Access Manager configuration file is not readable. Please rerun with read permissions.'`"
	exit ${AM_CONFIGPROPFILE_CANNOTREAD}
    fi
}

ModifyConfigFile()
{
    WriteToLog " "
    WriteToLog "\t `${GETTEXT} 'Modifying file:'` ${AMCONFIGPROPFILE}"
    ${CP} $AMCONFIGPROPFILE $AMCONFIGPROPFILE.tmp
    sed "s/^com.iplanet.am.server.protocol=.*$/com.iplanet.am.server.protocol=${IPROTOCOL}/
        s/^com.iplanet.am.server.host=.*$/com.iplanet.am.server.host=${IHOST}/
        s/^com.iplanet.am.server.port=.*$/com.iplanet.am.server.port=${IPORT}/
        s/^com.iplanet.am.console.protocol=.*$/com.iplanet.am.console.protocol=${IPROTOCOL}/
        s/^com.iplanet.am.console.host=.*$/com.iplanet.am.console.host=${IHOST}/
        s/^com.iplanet.am.console.port=.*$/com.iplanet.am.console.port=${IPORT}/
        s/^com.iplanet.am.profile.host=.*$/com.iplanet.am.profile.host=${IHOST}/
        s/^com.iplanet.am.profile.port=.*$/com.iplanet.am.profile.port=${IPORT}/
        s/^com.iplanet.am.naming.url=.*$/com.iplanet.am.naming.url=${IPROTOCOL}:\/\/${IHOST}:${IPORT}\/amserver\/namingservice/

    " $AMCONFIGPROPFILE.tmp > $AMCONFIGPROPFILE
    if [ $?=0 ]; then
        ${RM} $AMCONFIGPROPFILE.tmp
        WriteToLog "\t `${GETTEXT} 'Succeeded...'`"
    else
        ${CP} $AMCONFIGPROPFILE.tmp $AMCONFIGPROPFILE
        WriteToLog "\t `${GETTEXT} 'Failed...'`"
    fi
}


UpdateConfigResourceFile()
{
    if [ "x${IFLAG}" = "x" ] 
    then 
        WriteToLog " "
        response= 

        WriteToLog " "
        WriteToLog "\t `${GETTEXT} 'Running with the following Settings.'`"
        WriteToLog "\t -------------------------------------------------"
        WriteToLog "\t `${GETTEXT} 'Host Protocol: '`  ${IPROTOCOL}"
        WriteToLog "\t `${GETTEXT} 'Host Port    : '`  ${IPORT}"
        WriteToLog "\t `${GETTEXT} 'Server Host  : '`  ${IHOST}"
        WriteToLog "\t -------------------------------------------------"
        WriteToLog " "

        ${ECHO} "`${GETTEXT} 'Please confirm if following configuration values are correct.'`"
        ${ECHO} "`${GETTEXT} 'Valid values [yY/nN] :'` \c"
        myread response
        case $response in
            [yY]) break;;
            *)${ECHO} "`${GETTEXT} 'Aborting update process....'`"
            exit ${AM_INVALID_VALUE};;
        esac
    fi
    ModifyConfigFile 
}

#Usage message function
UsageMessage()
{
    ${ECHO} "`${GETTEXT} 'Usage : amsessionconfig [--help] port protocol host'`"
}


CheckInputValues()
{
    CheckUser
    #Check if the user entered the help option
    if [ "${1}" = "--help" ]
    then
        UsageMessage
        exit ${AM_USAGE_ERROR}
    fi

    #Check if the port is entered
    if [ "x$1" = "x" ]
    then
       UsageMessage
       exit ${AM_USAGE_ERROR}
    else 
       IPORT=$1
    fi

    #Check if the protocol is entered
    if [ "x$2" = "x" ]
    then
       UsageMessage
       exit ${AM_USAGE_ERROR}
    else 
       IPROTOCOL=$2
    fi

    #Check if the host is entered
    if [ "x$3" = "x" ]
    then
       UsageMessage
       exit ${AM_USAGE_ERROR}
    else 
       IHOST=$3
    fi

    #Check if running standalone
    if [ "x$4" != "x" ]
    then
       if [ "x$4" != "xmInstance" ]
       then
          AMCONFIGPROPFILE=${IS_CONFIG}/AMConfig.properties
       else
          AMCONFIGFILE=$5
          AMCONFIGPROPFILE=${IS_CONFIG}/${AMCONFIGFILE}
       fi
       IFLAG=$4
    fi
}

WriteToLog()
{
    Message=$1
    ${ECHO} $1
    ${ECHO} $1 >> ${LOGFILE}
}

#Check if the user running this script is root user. If not bail out
CheckUser()
{
    if [ `${ID} | ${AWK} '{print $1}'` != "uid=0(root)" ]
    then
        ${ECHO} "`${GETTEXT} 'This program can only be run by a root user.'`"
	exit ${AMCLSETUP_INVALID_USER}
    fi
}

#---- Script Starts here ----------
CheckInputValues $1 $2 $3 $4 $5
CheckConfigFiles
UpdateConfigResourceFile 
exit ${AM_MODIFYCONFIG_SUCCEED}
