#!/bin/sh

# Copyright 08/02/00 Sun Microsystems, Inc. All Rights Reserved.
# @(#)postpatch	1.10 00/08/02 Sun Microsystems

AWK=/usr/bin/awk
NAWK=/usr/bin/nawk
ECHO=/usr/bin/echo
MV=/usr/bin/mv
CP=/usr/bin/cp
RM=/bin/rm
GREP=/usr/bin/grep
SED=/usr/bin/sed
HEAD=/usr/bin/head
CHMOD=/usr/bin/chmod
LS=/bin/ls
FIND=/usr/bin/find
gettext=/usr/bin/gettext
BASE_DIR=/
PROG_DIR=`pwd`

CONFIG_DIR=/etc/opt/SUNWam/config
IS_INSTALL_VARDIR=/var/opt
IS_INSTALL_ETCDIR=/etc/opt
PRODUCT_DIR=SUNWam

PATH=.:/bin:/usr/bin:/usr/sbin:/etc
export PATH

VERSION_FILE=.version

amfin=/tmp/amupg.in; rm -f $amfin; touch $amfin
export amfin

##############################################################
#
#

insert_line() 
{
  file=$1
  match=$2
  new=$3

  CP=/bin/cp
  if [ ! -f $file-orig-$$ ]; then
     if [ -f $file ]; then
        $CP $file $file-orig-$$
     fi
  fi

  sed -e "
/^${match}*/ {
i\\
$new
}" $file > $file-tmp
cp $file-tmp $file
rm -f $file-tmp $file-orig-$$
}

##############################################################
#
#

get_var()
{
bd=$BASEDIR/SUNWam/bin/.dsame63
#files="$bd/amadmin $bd/ampassword $bd/amverifyarchive"
 egrep -i "^AM_HOME" $bd/amadmin >> $amfin
 egrep -i "^JAVA_HOME" $bd/amadmin >> $amfin
 egrep -i "^USING_JDK14" $bd/amadmin >> $amfin
 egrep -i "^JSS_HOME" $bd/amadmin >> $amfin
 egrep -i "^NSS_HOME" $bd/amadmin >> $amfin
 egrep -i "^NSPR_HOME" $bd/amadmin >> $amfin
 egrep -i "^JSS_JAR_PATH" $bd/amadmin >> $amfin
 egrep -i "^JSS_SO_PATH" $bd/amadmin >> $amfin
 egrep -i "^NSPR_SO_PATH" $bd/amadmin >> $amfin
 egrep -i "^NSS_SO_PATH" $bd/amadmin >> $amfin

# no grep for ampassword, share from amadmin
#  insert some var def prepended password
# no grep for amverifyarchive, need insert_line 2 for DP_HOME,JAVA_HOME
#  then, tag swap for TAG_JSS_JAR_PATH,TAG_JSS_SO_PATH

}

##############################################################
#
#

gen_it()
{
pdir=`pwd`
bdir=`pkgparam SUNWamsdk BASEDIR`
cd $bdir/SUNWam/bin

. $amfin

# proc amadmin

cp amadmin.template amadmin+

sed -e "s#BASEDIR#$bdir#g" \
 -e "s#PRODUCT_DIR#SUNWam#g" \
 -e "s#JDK_PATH#$JAVA_HOME#g" \
 -e "s#IAS7INSTALL#$USING_JDK14#g" \
 -e "s#JSSHOME#$JSS_HOME#g" \
 -e "s#NSSHOME#$NSS_HOME#g" \
 -e "s#NSPRHOME#$NSPR_HOME#g" \
 -e "s#TAG_JSS_JAR_PATH#$JSS_JAR_PATH#g" \
 -e "s#TAG_JSS_SO_PATH#$JSS_SO_PATH#g" \
 -e "s#TAG_NSPR_SO_PATH#$NSPR_SO_PATH#g" \
 -e "s#TAG_NSS_SO_PATH#$NSS_SO_PATH#g" \
 -e "s#CONFIG_DIR#/etc/opt/SUNWam/config#g" \
 -e "s#AMCONFIG#AMConfig#g" \
 amadmin+ > amadmin
 rm -f amadmin+
 chmod +x amadmin

cp ampassword.template ampassword+
insert_line `pwd`/ampassword+ "DP_HOME=" "BASE_DIR=$bdir"
insert_line `pwd`/ampassword+ "DP_HOME=" "PRODUCT_DIR=SUNWam"
insert_line `pwd`/ampassword+ "DP_HOME=" "JDK_BASE_DIR=$JAVA_HOME"

sed -e "s#JSSHOME#$JSS_HOME#g" \
 -e "s#TAG_JSS_JAR_PATH#$JSS_JAR_PATH#g" \
 -e "s#TAG_JSS_SO_PATH#$JSS_SO_PATH#g" \
 -e "s#IS_INSTALL_ETCDIR#/etc/opt#g" \
 -e "s#IS_PRODNAME#SUNWam#g" \
 -e "s#AMCONFIG#AMConfig#g" \
 ampassword+ > ampassword
 rm -f ampassword+
 chmod +x ampassword

cp amverifyarchive.template amverifyarchive+
insert_line `pwd`/amverifyarchive+ "APPSERVER=" "BASE_DIR=$bdir"
insert_line `pwd`/amverifyarchive+ "APPSERVER=" "PRODUCT_DIR=SUNWam"

sed -e "s#TAG_JSS_JAR_PATH#$JSS_JAR_PATH#g" \
 -e "s#TAG_JSS_SO_PATH#$JSS_SO_PATH#g" \
 -e "s#IS_INSTALL_ETCDIR#/etc/opt#g" \
 -e "s#IS_PRODNAME#SUNWam#g" \
 -e "s#JDK_PATH#$JAVA_HOME#g" \
 -e "s#AMCONFIG#AMConfig#g" \
 amverifyarchive+ > amverifyarchive
 rm -f amverifyarchive+
 chmod +x amverifyarchive

cd $pdir
}


##############################################################
#
#

get_base_dir() {
  pkginfo -q SUNWamsdk
  if [ $? -eq 0 ]; then
    BASE_DIR=`pkginfo -r SUNWamsdk`
  else
    exit 
  fi

}

################################################################
#
# update the .version string
#
updateVersionString() {
    curr_dir=`pwd`
    cd /etc/opt/SUNWam/config

    if [ -f .version ]; then
        $MV .version .version-63 2>/dev/null
    else
        $ECHO "Sun One Identity Server 6.3" > .version-63
    fi
     
    $ECHO "Sun Java(tm) System Access Manager 6 2005Q1" > .version
    cd $curr_dir
}

##############################################################
#
# Remove unneeded space and tab in dn, then replace , with _
#

NormalizeDNandReplaceCommaWith_() {

    NMDN=`echo "$1" | awk '{

        dn = $0
        result = ""
        size = split(dn, rdns, ",")
        for(i=1; i<size+1; i++) {
            if (i>1) result = result  "_"
            rdn = rdns[i]
            size2 = split(rdn, attrs, "=")
            for(j=1; j<size2+1; j++) {
                if (j>1) result = result  "="
                str = attrs[j]
                strlen = length(str)
                start = 1
                end = strlen
                for (k=1; k<strlen+1; k++) {
                    ch = substr(str, k, 1);
                    if (ch != " " && ch != "\t") {
                        start = k
                        break
                    }
                }
                for(l=strlen; l>0; l--) {
                    ch = substr(str, l, 1);
                    if (ch != " " && ch != "\t") {
                        end = l
                        break
                    }
                }
                result = result substr(str, start, end-start+1)
            }
        }
        print result

    }'`

}


###################################################################
#  Function - Set Path of
#  JSS, NSS, NSPR, JSS_JAR, JAVA_DIR, JAXP_JAR and LD_LIBRARY_PATH
#
set_path_for_config() {
        OSTYPE=`uname`
	if [ "$OSTYPE" = "Linux" ]
	then
		# These RPMs are not made relocatable on Linux.
		SUNWjaxp_BASE_DIR=/
		JSSHOME=/
		NSSHOME=/
		NSPRHOME=/
		REL_JSS_PATH=opt/sun/private/lib
		REL_JSS_JAR_PATH=opt/sun/private/share/lib
		REL_NSS_PATH=opt/sun/private/lib
		REL_NSPR_PATH=opt/sun/private/lib
	elif [ "$OSTYPE" = "SunOS" ]; then
		JSSHOME=`pkginfo -r SUNWjss` 
	
		NSSHOME=`pkginfo -r SUNWtls`
	
		NSPRHOME=`pkginfo -r SUNWpr`
	
		SUNWjaxp_BASE_DIR=`pkginfo -r SUNWjaxp 2>/dev/null`

		REL_JSS_PATH=usr/lib/mps/secv1
		REL_JSS_JAR_PATH=usr/share/lib/mps/secv1
		REL_NSS_PATH=usr/lib/mps/secv1
		REL_NSPR_PATH=usr/lib/mps/secv1
	fi

    if [ "$JSSHOME" = "/" ]; then
        JSS_PATH=/${REL_JSS_PATH}
        JSS_JAR_PATH=/${REL_JSS_JAR_PATH}
    else
        JSS_PATH=${JSSHOME}/${REL_JSS_PATH}
        JSS_JAR_PATH=${JSSHOME}/${REL_JSS_JAR_PATH}
    fi
    
    if [ "$NSSHOME" = "/" ]; then
        NSS_PATH=/${REL_NSS_PATH}
    else
        NSS_PATH=${NSSHOME}/${REL_NSS_PATH}
    fi
    
    if [ "$NSPRHOME" = "/" ]; then
        NSPR_PATH=/${REL_NSPR_PATH}
    else
        NSPR_PATH=${NSPRHOME}/${REL_NSPR_PATH}
    fi
    
    if [ "$SUNWjaxp_BASE_DIR" = "/" ]; then
		JAXP_JAR_PATH="/usr/share/lib"
    else
		JAXP_JAR_PATH="${SUNWjaxp_BASE_DIR}/usr/share/lib"
    fi
    
	# LD_LIBRARY_PATH
	if [ "$OSTYPE" = "Linux" ]
	then
		# Why is the native_threads dir needed in LD_LIBRARY_PATH on Linux? No idea. Seems like a JDK bug on Linux.
		LD_LIBRARY_PATH=$JSS_PATH:$NSS_PATH:$NSPR_PATH:${PKGDIR}/lib:${PKGDIR}/ldaplib/ldapsdk:$JAVA_HOME/jre/lib/i386/native_threads
	else
		LD_LIBRARY_PATH=$JSS_PATH:$NSS_PATH:$NSPR_PATH:${PKGDIR}/lib:${PKGDIR}/ldaplib/ldapsdk
	fi
        export LD_LIBRARY_PATH
	echo "LD_LIBRARY_PATH is --- $LD_LIBRARY_PATH"
    
	# CLASSPATH
	PKGDIR="$BASEDIR/SUNWam"
	CLASSPATH=${LOCALE_DIR}:${CONFIG_DIR}:${PKGDIR}/lib:${PKGDIR}/lib/am_services.jar:$JSS_JAR_PATH/jss3.jar:${PKGDIR}/lib/am_sdk.jar:$BASEDIR/SUNWam/locale
	export CLASSPATH
	echo "CLASSPATH is --- $CLASSPATH"
	
}
##################################################
# Function - Do tag swapping of the ldif files. ##
##################################################
ldif_config_tag_swap () {

    LDIF_LOCATION=${CONFIG_DIR}/ldif
    DIT_LDIF=${LDIF_LOCATION}/install.ldif
    EXISTING_DIT_LDIF=${LDIF_LOCATION}/installExisting.ldif
    CONFIG_LDIF=${LDIF_LOCATION}/ds_remote_schema.ldif
    SCHEMA2_CONFIG_LDIF=${LDIF_LOCATION}/sunone_schema2.ldif
    LDIF_UNINSTALL_FILE=${LDIF_LOCATION}/ds_remote_schema_uninstall.ldif

    CLIENT_SCHEMA_LDIF=${LDIF_LOCATION}/sunAMClient_schema.ldif
    CLIENT_DATA_LDIF=${LDIF_LOCATION}/sunAMClient_data.ldif

    NormalizeDNandReplaceCommaWith_ "$ROOT_SUFFIX"
    People_NM_ROOT_SUFFIX=People_${NMDN}
    
    NormalizeDNandReplaceCommaWith_ "$ORG_ROOT_SUFFIX"
    People_NM_ORG_ROOT_SUFFIX=People_${NMDN}
    
    RS_RDN=`$ECHO $ORG_ROOT_SUFFIX | awk ' BEGIN { FS="=" } { print $2 }' | awk ' BEGIN { FS="," } { print $1 }' `
    
    # CHECK - For existence of ${JAVA_HOME}/bin/java 
    if [ ! -f ${JAVA_HOME}/bin/java ]; then
        $ECHO "`$gettext 'Java not found at ${JAVA_HOME}/bin/java'`"
        return
    fi

##########################    LUKE
##################################################
# Function - Do tag swapping of the sample files.#
##################################################

tag_swap_samples()
{
    SAMPLEDIR=${PKGDIR}/samples
    SHARELIB=${PKGDIR}/lib
    EDIT_FILES="${SAMPLEDIR}/authentication/api/Cert/Makefile ${SAMPLEDIR}/authentication/api/LDAP/Makefile ${SAMPLEDIR}/authentication/api/jcdi/Makefile ${SAMPLEDIR}/authentication/spi/jcdi/Makefile ${SAMPLEDIR}/authentication/spi/postprocess/Makefile ${SAMPLEDIR}/authentication/spi/providers/Makefile ${SAMPLEDIR}/authentication/spi/purejaas/Makefile ${SAMPLEDIR}/sso/Makefile ${SAMPLEDIR}/sso/run ${SAMPLEDIR}/admin/cli/bulk-ops/schemaAddAttributeSchemaRequests.xml ${SAMPLEDIR}/admin/cli/bulk-ops/schemaAddSubSchemaRequests.xml ${SAMPLEDIR}/liberty/sample1/idp1/WEB-INF/web.xml ${SAMPLEDIR}/liberty/sample1/sp1/WEB-INF/web.xml ${SAMPLEDIR}/liberty/sample2/idp1/WEB-INF/web.xml ${SAMPLEDIR}/liberty/sample2/sp1/WEB-INF/web.xml ${SAMPLEDIR}/liberty/sample3/idp1/WEB-INF/web.xml ${SAMPLEDIR}/liberty/sample3/idp2/WEB-INF/web.xml ${SAMPLEDIR}/liberty/sample3/sp1/WEB-INF/web.xml ${SAMPLEDIR}/liberty/sample3/sp2/WEB-INF/web.xml ${SAMPLEDIR}/logging/RunSample ${SAMPLEDIR}/phase2/sis-ep/bin/install_dir.var ${SAMPLEDIR}/phase2/sis-ep/src/Makefile ${SAMPLEDIR}/phase2/sis-ep/xml/amLibertyEmployeeProfile.xml ${SAMPLEDIR}/policy/Makefile ${SAMPLEDIR}/saml/xmlsig/Makefile ${SAMPLEDIR}/um/Makefile ${SAMPLEDIR}/um/RunSamples ${SAMPLEDIR}/authentication/spi/genuid/Makefile ${SAMPLEDIR}/phase2/authnsvc/Makefile"
    for file in $EDIT_FILES; do
	cp -p $file $file+
	 sed -e "s#JAVADIR#${JAVA_HOME}#g" \
            -e "s#IS_INSTALL_BASEDIR#${BASEDIR}#g" \
            -e "s#BASEDIR#${BASEDIR}#g" \
            -e "s#SHARELIB#${SHARELIB}#g" \
            -e "s#JSSJARPATH#${JSS_JAR_PATH}#g" \
            -e "s#JSSPATH#${JSS_SO_PATH}#g" \
            -e "s#IS_INSTALL_ETCDIR#${IS_INSTALL_ETCDIR}#g" \
            -e "s#IS_INSTALL_VARDIR#${IS_INSTALL_VARDIR}#g" \
            -e "s#IS_PRODNAME#${PRODUCT_DIR}#g" \
            -e "s#TAG_JSS_SO_PATH#${JSS_SO_PATH}#g" \
            -e "s#TAG_JSS_JAR_PATH#${JSS_JAR_PATH}#g" \
            -e "s#TAG_NSS_SO_PATH#${NSS_PATH}#g" \
            -e "s#TAG_NSPR_SO_PATH#${NSPR_PATH}#g" \
	$file+ > $file
	 rm -f $file+
    done
}
####################

#    ENCLDAPUSERPASSWD=`${JAVA_HOME}/bin/java -classpath ${CLASSPATH} com.iplanet.services.util.internal.TextCrypt -f ${FILE_AMLDAPUSERPASSWD}`
    
      eval $ECHO "`$gettext 'Starting the tag swapping of the install.ldif and installExisting.ldif'`"
      
      eval $ECHO "`$gettext 'ROOT_SUFFIX is ${ROOT_SUFFIX}'`"
      eval $ECHO "`$gettext 'People_NM_ROOT_SUFFIX is ${People_NM_ROOT_SUFFIX}'`"
      eval $ECHO "`$gettext 'SERVER_HOST ${SERVER_HOST}'`"
      eval $ECHO "`$gettext 'DIRECTORY_SERVER ${DS_HOST}'`"
      eval $ECHO "`$gettext 'DIRECTORY_PORT ${DS_PORT}'`"
      eval $ECHO "`$gettext 'USER_NAMING_ATTR ${USER_NAMING_ATTR}'`"
      eval $ECHO "`$gettext 'ORG_NAMING_ATTR ${ORG_NAMING_ATTR}'`"
      eval $ECHO "`$gettext 'CONSOLE_DEPLOY_URI ${CONSOLE_DEPLOY_URI}'`"
      eval $ECHO "`$gettext 'ORG_OBJECT_CLASS ${ORG_OBJECT_CLASS}'`"
      eval $ECHO "`$gettext 'RS_RDN ${RS_RDN}'`"
      eval $ECHO "`$gettext 'USER_OBJECT_CLASS ${USER_OBJECT_CLASS}'`"
    
    
    EDIT_FILES="${EXISTING_DIT_LDIF} ${DIT_LDIF} ${CLIENT_DATA_LDIF}"
    for file in $EDIT_FILES; do
        $CP $file $file+
        sed -e "s#\BASE_DIR#${BASEDIR}#g" \
            -e "s#\People_NM_ROOT_SUFFIX#${People_NM_ROOT_SUFFIX}#g" \
            -e "s#\People_NM_ORG_ROOT_SUFFIX#${People_NM_ORG_ROOT_SUFFIX}#g" \
            -e "s#\ORG_ROOT_SUFFIX#${ORG_ROOT_SUFFIX}#g" \
            -e "s#\ROOT_SUFFIX#${ROOT_SUFFIX}#g" \
            -e "s#\SERVER_HOST#${SERVER_HOST}#g" \
            -e "s#\DIRECTORY_SERVER#${DS_HOST}#g" \
            -e "s#\DIRECTORY_PORT#${DS_PORT}#g" \
            -e "s#\ADMINPASSWD#${ADMINPASSWD}#g" \
            -e "s#\AMLDAPUSERPASSWD#${AMLDAPUSERPASSWD}#g" \
            -e "s#\ENCLDAPUSERPASSWD#${ENCLDAPUSERPASSWD}#g" \
            -e "s#\USER_NAMING_ATTR#${USER_NAMING_ATTR}#g" \
            -e "s#\ORG_NAMING_ATTR#${ORG_NAMING_ATTR}#g" \
            -e "s#\CONSOLE_DEPLOY_URI#${CONSOLE_DEPLOY_URI}#g" \
            -e "s#\ORG_OBJECT_CLASS#${ORG_OBJECT_CLASS}#g" \
            -e "s#\RS_RDN#${RS_RDN}#g" \
            -e "s#\USER_OBJECT_CLASS#${USER_OBJECT_CLASS}#g" \
        $file+ > $file
        rm -f $file+
    done
}

##############################################################
# Tag swap of all the service Schema XML files and also the 
# AMConfig.properties file
##############################################################
xml_config_tag_swap() {

curr_dir=`pwd`
    #
    # determine fc urls necessary.  if port is default port, then add another 
    # rule that does not include the port
    #
    FCURLS="<Val>$SERVER_PROTOCOL://$SERVER_HOST:$SERVER_PORT</Val>"
    if [ "$SERVER_PORT" = "80" ] && [ "$SERVER_PROTOCOL" = "http" ]; then
    	FCURLS="$FCURLS<Val>$SERVER_PROTOCOL://$SERVER_HOST</Val>"
    elif [ "$SERVER_PORT" = "443" ] && [ "$SERVER_PROTOCOL" = "https" ]; then
    	FCURLS="$FCURLS<Val>$SERVER_PROTOCOL://$SERVER_HOST</Val>"
    fi
    
    GROUP_TYPE="dynamic"
    # CHECK - For existence of ${JAVA_HOME}/bin/java 
    if [ ! -f ${JAVA_HOME}/bin/java ]; then
        eval $ECHO "`$gettext 'Java not found at ${JAVA_HOME}/bin/java'`"
        return
    fi
    ORG_BASE=$ORG_ROOT_SUFFIX
    SERV_LIST=$SERVER_PROTOCOL://$SERVER_HOST:$SERVER_PORT
    server_url=$SERVER_PROTOCOL://$SERVER_HOST:$SERVER_PORT
    WSVR_INSTANCE="https-${SERVER_HOST}"
    PROFILE_HOST=$SERVER_HOST
    PROFILE_PORT=$SERVER_PORT
    session_url=%protocol://%host:%port
    profileservice_url=$SERVER_PROTOCOL://$PROFILE_HOST:$PROFILE_PORT
    loggingservice_url=$SERVER_PROTOCOL://$PROFILE_HOST:$PROFILE_PORT
    serverservice_url=$SERVER_PROTOCOL://$SERVER_HOST:$SERVER_PORT
    notification_url=$SERVER_PROTOCOL://$SERVER_HOST:$SERVER_PORT$SERVER_DEPLOY_URI/notificationservice
##### LUKE
# Need to find the value from the original file
###########################
##    SAMLSITEID=`${JAVA_HOME}/bin/java -classpath ${CLASSPATH}:${PKGDIR}/lib/xercesImpl.jar:${PKGDIR}/lib/sax.jar:${PKGDIR}/lib/dom.jar com.sun.identity.saml.common.SAMLSiteID ${SERV_LIST}`
##    ENCADMINPASSWD=`${JAVA_HOME}/bin/java -classpath ${CLASSPATH} com.iplanet.services.util.internal.TextCrypt -f ${FILE_ADMINPASSWD}`
##    ENCLDAPUSERPASSWD=`${JAVA_HOME}/bin/java -classpath ${CLASSPATH} com.iplanet.services.util.internal.TextCrypt -f ${FILE_AMLDAPUSERPASSWD}`
    XML_FILES=`/bin/ls -l ${CONFIG_DIR}/xml | grep '^-' | awk '{print $9}'`

# Begin PORTALMA. This is a generic change which allows request files to be put 
#  in CONFIG_DIR/request directory. These data files will be imported using 
#  amadmin --data. 
    REQUEST_DIR=${CONFIG_DIR}/request
    if [ -d $REQUEST_DIR ]; then
	    REQUEST_FILES=`/bin/ls -1 ${REQUEST_DIR}`
	    for file in $REQUEST_FILES; do
		ABS_REQUEST_FILES="$ABS_REQUEST_FILES ${REQUEST_DIR}/$file"
	    done
    else
    	    REQUEST_FILES=""
	    ABS_REQUEST_FILES=""
    fi

# End PORTALMA

    UPGRADE61to62_XML_FILES=`/bin/ls -1 ${UPGRADE61to62_XML_DIR}/*.xml`
    
    if [ "$DIRECTORY_MODE" = "1" ]; then
        UMS_XML=${CONFIG_DIR}/ums/ums.xml
    else
        UMS_XML=${CONFIG_DIR}/ums/umsExisting.xml
    fi
    DEFAULTORG=$ORG_ROOT_SUFFIX
    NormalizeDNandReplaceCommaWith_ "$ROOT_SUFFIX"
    NM_ROOT_SUFFIX="$NMDN"
    NormalizeDNandReplaceCommaWith_ "$DEFAULTORG"
    NM_INST_ORGANIZATION="$NMDN"
    NM_ORG_ROOT_SUFFIX="$NMDN"
    VERSION="Sun Java(tm) System Access Manager 6 2005Q1"
    NORMALIZED_RS=`echo $ROOT_SUFFIX | sed -e "s#\&#\&amp;#g" -e "s#'#\&apos;#g" -e "s#\"#\&quot;#g" -e "s#<#\&lt;#g" -e "s#>#\&gt;#g"`
    NORMALIZED_DEFAULTORG=`echo $DEFAULTORG | sed -e "s#\&#\&amp;#g" -e "s#'#\&apos;#g" -e "s#\"#\&quot;#g" -e "s#<#\&lt;#g" -e "s#>#\&gt;#g"`
    RS_RDN=`echo $ORG_ROOT_SUFFIX | awk 'BEGIN { FS="=" } { print $2 }' | awk 'BEGIN { FS="," } { print $1 }' `

    if [ -z "$XML_ENCODING"  ];then 
    	TANS=`${JDK_BASE_DIR}/bin/java -classpath ${CLASSPATH} com.iplanet.am.util.getEncoding -http | awk ' { print $4 }' `
    	if [ "$TANS" != "" ]; then
    		XML_ENCODING=$TANS
    	else
    		XML_ENCODING="UTF-8"
    	fi
    	eval $ECHO "`$gettext 'XML Encoding is not set.found automatically to $XML_ENCODING'`"
    fi
    
    
    cd ${CONFIG_DIR}/xml
    if [ "$DEPLOY_LEVEL" != "31" ]; then
       if [ -f ${CONFIG_DIR}/AMConfig.properties ]; then
	    ## backup AMConfig.properties
	    $MV ${CONFIG_DIR}/AMConfig.properties ${CONFIG_DIR}/.AMConfig.properties.backup
       fi
       $CP ${CONFIG_DIR}/AMConfig.properties.template ${CONFIG_DIR}/AMConfig.properties
       chown $NEW_OWNER:$NEW_GROUP ${CONFIG_DIR}/AMConfig.properties
       chmod 400 ${CONFIG_DIR}/AMConfig.properties

       if [ -d ${PKGDIR}/upgrade ]; then
	    UPGRADE_FILE=`/bin/ls -1 $PKGDIR/upgrade/scripts/*.xml \
			  $PKGDIR/upgrade/services/*/*/*/*.*`
       fi

       # process amadmin,ampassword,amverifyarchive CLI specially
       get_var

       gen_it

       EDIT_FILES="${CONFIG_DIR}/ums/amserveradmin ${CONFIG_DIR}/AMConfig.properties ${CONFIG_DIR}/serverconfig.xml $XML_FILES $ABS_REQUEST_FILES ${CONFIG_DIR}/ums/ums.xml ${CONFIG_DIR}/ums/umsExisting.xml ${PKGDIR}/bin/amserver  ${PKGDIR}/bin/am2bak ${PKGDIR}/bin/bak2am ${PKGDIR}/migration/61to62/install_61to62_upgrade.ldif ${PKGDIR}/migration/61to62/ds_remote_schema_61to62upgrade.ldif ${UPGRADE61to62_XML_FILES} $UPGRADE_FILE"
    else
      EDIT_FILES="${CONFIG_DIR}/AMConfig.properties ${PKGDIR}/bin/ampassword ${CONFIG_DIR}/serverconfig.xml"
    fi
    
    # $PACKAGEDIR/servers/$SERVER_HOST
    if [ "$SERVER_HOST" != "" ] && [ -d ${WS61_HOME}/https-$SERVER_HOST ]
    then
      WEBSERVER_HOST=$SERVER_HOST
    else
      WEBSERVER_HOST=$CONSOLE_HOST
    fi

    if [ "$OSTYPE" = "Linux" ]; then
    	PAM_SERVICE_NAME=system-auth
    else
    	PAM_SERVICE_NAME=login
    fi
   
    # Seding start 
    for file in $EDIT_FILES; do
     if [ -f $file ];then
      $CP $file $file+
      sed -e "s#PLATDIR#$BASEDIR#g" \
       -e "s#\${DPRO_BASEDIR}#${BASEDIR}#g" \
       -e "s#\${BASE_DIR}#${BASEDIR}#g" \
       -e "s#\${JDK_BASE_DIR}#${JAVA_HOME}#g" \
       -e "s#\${PRODUCT_DIR}#${PRODUCT_DIR}#g" \
       -e "s#NORMALIZED_RS#$NORMALIZED_RS#g" \
       -e "s#NORMALIZED_ORGBASE#$NORMALIZED_DEFAULTORG#g" \
       -e "s#PKGDIR#${PKGDIR}#g" \
       -e "s#TEMP_DIR_PREFIX#${TEMP_DIR_PREFIX}#g" \
       -e "s#DEBUG_DIR_PREFIX#${DEBUG_DIR_PREFIX}#g" \
       -e "s#CONFIG_DIR#${CONFIG_DIR}#g" \
       -e "s#JDK_PATH#${JAVA_HOME}#g" \
       -e "s#WEBSERVER_HOST#$WEBSERVER_HOST#g" \
       -e "s#SERVER_URL#$server_url#g" \
       -e "s#SERVER_HOST#$SERVER_HOST#g" \
       -e "s#SERVER_PORT#$SERVER_PORT#g" \
       -e "s#CONSOLE_HOST#$CONSOLE_HOST#g" \
       -e "s#CONSOLE_PORT#$CONSOLE_PORT#g" \
       -e "s#SERVER_PROTO#$SERVER_PROTOCOL#g" \
       -e "s#CONSOLE_PROTO#$CONSOLE_PROTOCOL#g" \
       -e "s#SERVER_DEPLOY_URI#$SERVER_DEPLOY_URI#g" \
       -e "s#CONSOLE_DEPLOY_URI#$CONSOLE_DEPLOY_URI#g" \
       -e "s#PASSWORD_DEPLOY_URI#$PASSWORD_DEPLOY_URI#g" \
       -e "s#COMMON_DEPLOY_URI#$COMMON_DEPLOY_URI#g" \
       -e "s#CONSOLE_REMOTE#$CONSOLE_REMOTE#g" \
       -e "s#BASEDIR#$BASEDIR#g" \
       -e "s#HOST_NAME#$HOST_NAME#g" \
       -e "s#FULLHOSTNAME#$SERVER_HOST#g" \
       -e "s#PRODUCT_DIR#$PRODUCT_DIR#g" \
       -e "s#DIT_COMPLIANCE#$DIT_COMPLIANCE#g" \
       -e "s#NM_ROOT_SUFFIX#$NM_ROOT_SUFFIX#g" \
       -e "s#NM_ORG_ROOT_SUFFIX#$NM_ORG_ROOT_SUFFIX#g" \
       -e "s#ORG_ROOT_SUFFIX#$ORG_ROOT_SUFFIX#g" \
       -e "s#ROOT_SUFFIX#$ROOT_SUFFIX#g" \
       -e "s#RS_RDN#$RS_RDN#g" \
       -e "s#ORG_BASE#$ORG_BASE#g" \
       -e "s#DCTREE_SUFFIX#$DCTREE_SUFFIX#g" \
       -e "s#GROUP_TYPE#$GROUP_TYPE#g" \
       -e "s#NM_INST_ORGANIZATION#$NM_INST_ORGANIZATION#g" \
       -e "s#INST_ORGANIZATION#$DEFAULTORG#g" \
       -e "s#\${DIRECTORY_PORT}#${DS_PORT}#g" \
       -e "s#\${DIRECTORY_SERVER}#${DS_HOST}#g" \
       -e "s#DIRECTORY_PORT#${DS_PORT}#g" \
       -e "s#DIRECTORY_SERVER#${DS_HOST}#g" \
       -e "s#SIMPLE_HOST#$DS_HOST#g" \
       -e "s#ENCLDAPUSERPASSWD#$ENCLDAPUSERPASSWD#g" \
       -e "s#AMLDAPUSERPASSWD#$LDAPUSERPASSWD#g" \
       -e "s#ENCADMINPASSWD#$ENCADMINPASSWD#g" \
       -e "s#ADMINPASSWD#$ADMINPASSWD#g" \
       -e "s#PROXUUSERPASSWORD#$ADMINPASSWD#g" \
       -e "s#INST_SERV_LIST#$SERV_LIST#g" \
       -e "s#INST_COOKIE_DOMAIN_LIST#$COOKIE_DOMAIN_LIST#g" \
       -e "s#SAML_SITEID#$SAMLSITEID#g" \
       -e "s#SERVER_URL#$serverservice_url#g" \
       -e "s#PROFILE_URL#$profileservice_url#g" \
       -e "s#SESSION_URL#$session_url#g" \
       -e "s#LOGGING_URL#$loggingservice_url#g" \
       -e "s#NOTIFICATION_URL#$notification_url#g" \
       -e "s#PROFILE_HOST#$SERVER_HOST#g" \
       -e "s#PROFILE_PORT#$SERVER_PORT#g" \
       -e "s#DPRO_VERSION#$VERSION#g" \
       -e "s#CURRENT_PLATFORM_LOCALE#$PLATFORM_LOCALE#g" \
       -e "s#AVAILABLE_LOCALES#$AVAILABLE_LOCALES#g" \
       -e "s#XML_ENCODING#$XML_ENCODING#g" \
       -e "s#PLATFORM_LOCALE#$PLATFORM_LOCALE#g" \
       -e "s#UM_ENABLED#$UM_ENABLED#g" \
       -e "s#ORG_OBJECT_CLASS#$ORG_OBJECT_CLASS#g" \
       -e "s#ORG_NAMING_ATTR#$ORG_NAMING_ATTR#g" \
       -e "s#USER_NAMING_ATTR#$USER_NAMING_ATTR#g" \
       -e "s#USER_OBJECT_CLASS#$USER_OBJECT_CLASS#g" \
       -e "s#PRIMARY_NAMING_URL#$profileservice_url$SERVER_DEPLOY_URI/namingservice#g" \
       -e "s#PRIMARY_LOGIN_URL#$profileservice_url$SERVER_DEPLOY_URI/UI/Login#g" \
       -e "s#SHARED_SECRET#$ENCLDAPUSERPASSWD#g" \
       -e "s#FAILOVER_NAMING_URL##g" \
       -e "s#FAILOVER_LOGIN_URL##g" \
       -e "s#WEB_CONTAINER_TAG#$WEB_CONTAINER#g" \
       -e "s#NEW_OWNER#$NEW_OWNER#g" \
       -e "s#NEW_GROUP#$NEW_GROUP#g" \
       -e "s#JSSHOME#$JSSHOME#g" \
       -e "s#NSSHOME#$NSSHOME#g" \
       -e "s#WS_BASE_DIR#$WS61_HOME#g" \
       -e "s#WEBSVR_INSTANCE#$WS61_HOME#g" \
       -e "s#WSVR_INSTANCE#$WSVR_INSTANCE#g" \
       -e "s#NSPRHOME#$NSPRHOME#g" \
       -e "s#JAXPHOME#$JAXP_JAR_PATH#g" \
       -e "s#JAXP_LIB_DIR#$JAXP_JAR_PATH#g" \
       -e "s#JAVA_COMPONENT_PACKAGES#$JAVA_COMPONENT_PACKAGES#g" \
       -e "s#PAM_SERVICE_NAME#$PAM_SERVICE_NAME#g" \
       -e "s#IS_INSTALL_ETCDIR#$IS_INSTALL_ETCDIR#g" \
       -e "s#IS_INSTALL_VARDIR#$IS_INSTALL_VARDIR#g" \
       -e "s#IS_PRODNAME#$PRODUCT_DIR#g" \
       -e "s#DEBUG_SUBDIR#debug#g" \
       -e "s#TAG_JSS_SO_PATH#${JSS_PATH}#g" \
       -e "s#TAG_JSS_JAR_PATH#${JSS_JAR_PATH}#g" \
       -e "s#TAG_NSS_SO_PATH#${NSS_PATH}#g" \
       -e "s#TAG_NSPR_SO_PATH#${NSPR_PATH}#g" \
       -e "s#ENCLDAPUSERPASSWD#$ENCLDAPUSERPASSWD#g" \
       -e "s#WEBAPPSDIR#$WEBAPPSDIR#g" \
       -e "s#AM_ENC_PWD#$AM_ENC_PWD#g" \
       -e "s#COOKIE_ENCODE#$COOKIE_ENCODE#g" \
       -e "s#LB_COOKIE#$LB_COOKIE#g" \
       -e "s#^com.sun.identity.session.property.doNotTrimList=#com.sun.identity.session.property.doNotTrimList=$DONOTTRIMLIST#g" \
       -e "s#COOKIE_CHECK#$COOKIE_CHECK#g" \
       -e "s#CONTAINER_CERTDB_DIR#$CONTAINER_CERTDB_DIR#g" \
       -e "s#CONTAINER_CERTDB_PREFIX#$CONTAINER_CERTDB_PREFIX#g" \
       $file+ > $file
      rm -f $file+
     fi
    done

cd $curr_dir
}

############################################################
###  This function swaps the tag required by this script. ##
############################################################
sdk_config_tag_swap() {

curr_dir=`pwd`

    RS_RDN=`$ECHO $ORG_ROOT_SUFFIX | awk ' BEGIN { FS="=" } { print $2 }' | awk ' BEGIN { FS="," } { print $1 }' `
          
    # CHECK - For existence of ${JAVA_HOME}/bin/java 
    if [ ! -f ${JAVA_HOME}/bin/java ]; then
        eval $ECHO "`$gettext 'Java not found at ${JAVA_HOME}/bin/java'`" $PROCESS_LOG
        eval $ECHO "`$gettext 'Java not found at ${JAVA_HOME}/bin/java'`"
        return
    fi

    # use SDK client host instead of server host for notification config
    CLIENT_HOST=""
    CLIENT_PORT=""
    CLIENT_CONTAINER_TAG="WEB_CONTAINER"
    
    if [ "$WEB_CONTAINER" = "WS6" ];then                # webserver 6.1
      CLIENT_HOST=$WS61_HOST
      CLIENT_PORT=$WS61_PORT
      CLIENT_CONTAINER_TAG="SUN_WEBSERVER"
    elif [ "$WEB_CONTAINER" = "AS7" ];then      # AS 7.0
      CLIENT_HOST=$AS70_HOST
      CLIENT_PORT=$AS70_PORT
      CLIENT_CONTAINER_TAG="IAS7.0"
    elif [ "$WEB_CONTAINER" = "WL6" ];then      # weblogic 6.1
      CLIENT_HOST=$WL61_HOST
      CLIENT_PORT=$WL61_PORT
      CLIENT_CONTAINER_TAG="BEA6.1"
    elif [ "$WEB_CONTAINER" = "WL8" ];then      # weblogic 8.1
      CLIENT_HOST=$WL8_HOST
      CLIENT_PORT=$WL8_PORT
      CLIENT_CONTAINER_TAG="BEA8.1"
    elif [ "$WEB_CONTAINER" = "WAS5" ];then     # websphere 5.1
      CLIENT_HOST=$WAS51_HOST
      CLIENT_PORT=$WAS51_PORT
      CLIENT_CONTAINER_TAG="IBM5.1"
    fi
    
    ORG_BASE=$ORG_ROOT_SUFFIX
    PROFILE_HOST=$SERVER_HOST
    PROFILE_PORT=$SERVER_PORT
    profileservice_url=$SERVER_PROTOCOL://$PROFILE_HOST:$PROFILE_PORT
    serverservice_url=$SERVER_PROTOCOL://$SERVER_HOST:$SERVER_PORT
    
    if [ "$CLIENT_HOST" = "" ] || [ "$CLIENT_PORT" = "" ]; then
        notification_url="NOTIFICATION_URL"
    else
        notification_url=$SERVER_PROTOCOL://$CLIENT_HOST:$CLIENT_PORT/notificationservice
    fi
            
    ENCADMINPASSWD=`${JAVA_HOME}/bin/java -classpath ${CLASSPATH} com.iplanet.services.util.internal.TextCrypt -f ${FILE_ADMINPASSWD}`
    ENCLDAPUSERPASSWD=`${JAVA_HOME}/bin/java -classpath ${CLASSPATH} com.iplanet.services.util.internal.TextCrypt -f ${FILE_AMLDAPUSERPASSWD}`
    DEFAULTORG=$ORG_ROOT_SUFFIX
    VERSION="Sun Java(tm) System Access Manager 6 2005Q1"
    NORMALIZED_RS=`echo $ROOT_SUFFIX | sed -e "s#\&#\&amp;#g" -e "s#'#\&apos;#g" -e "s#\"#\&quot;#g" -e "s#<#\&lt;#g" -e "s#>#\&gt;#g"`
    NORMALIZED_DEFAULTORG=`echo $DEFAULTORG | sed -e "s#\&#\&amp;#g" -e "s#'#\&apos;#g" -e "s#\"#\&quot;#g" -e "s#<#\&lt;#g" -e "s#>#\&gt;#g"`
    
    if [ -z "$XML_ENCODING"  ];then 
    	TANS=`${JDK_BASE_DIR}/bin/java -classpath ${CLASSPATH} com.iplanet.am.util.getEncoding -http | awk ' { print $4 }' `
    	if [ "$TANS" != "" ]; then
    		XML_ENCODING=$TANS
    	else
    		XML_ENCODING="UTF-8"
    	fi
    	eval $ECHO "`$gettext 'XML Encoding is not set.found automatically to $XML_ENCODING'`" $PROCESS_LOG
    fi
    
    get_var

    gen_it
    EDIT_FILES="${CONFIG_DIR}/AMConfig.properties ${PKGDIR}/bin/ampassword ${CONFIG_DIR}/serverconfig.xml ${PKGDIR}/bin/amadmin"
    
    # Seding start 
    for file in $EDIT_FILES; do
      $CP $file $file+
      sed -e "s#PLATDIR#$BASEDIR#g" \
       -e "s#\${DPRO_BASEDIR}#${BASEDIR}#g" \
       -e "s#\${BASE_DIR}#${BASEDIR}#g" \
       -e "s#\${JDK_BASE_DIR}#${JAVA_HOME}#g" \
       -e "s#\${PRODUCT_DIR}#${PRODUCT_DIR}#g" \
       -e "s#ORG_ROOT_SUFFIX#$ORG_ROOT_SUFFIX#g" \
       -e "s#NORMALIZED_RS#$NORMALIZED_RS#g" \
       -e "s#NORMALIZED_ORGBASE#$NORMALIZED_DEFAULTORG#g" \
       -e "s#PKGDIR#${PKGDIR}#g" \
       -e "s#CONFIG_DIR#${CONFIG_DIR}#g" \
       -e "s#JDK_PATH#${JAVA_HOME}#g" \
       -e "s#SERVER_HOST#$SERVER_HOST#g" \
       -e "s#SERVER_PORT#$SERVER_PORT#g" \
       -e "s#CONSOLE_HOST#$CONSOLE_HOST#g" \
       -e "s#CONSOLE_PORT#$CONSOLE_PORT#g" \
       -e "s#SERVER_PROTO#$SERVER_PROTOCOL#g" \
       -e "s#CONSOLE_PROTO#$CONSOLE_PROTOCOL#g" \
       -e "s#SERVER_DEPLOY_URI#$SERVER_DEPLOY_URI#g" \
       -e "s#CONSOLE_DEPLOY_URI#$CONSOLE_DEPLOY_URI#g" \
       -e "s#CONSOLE_REMOTE#$CONSOLE_REMOTE#g" \
       -e "s#PASSWORD_DEPLOY_URI#$PASSWORD_DEPLOY_URI#g" \
       -e "s#BASEDIR#$BASEDIR#g" \
       -e "s#PRODUCT_DIR#$PRODUCT_DIR#g" \
       -e "s#ORG_BASE#$ORG_BASE#g" \
       -e "s#INST_ORGANIZATION#$DEFAULTORG#g" \
       -e "s#\${DIRECTORY_PORT}#${DS_PORT}#g" \
       -e "s#\${DIRECTORY_SERVER}#${DS_HOST}#g" \
       -e "s#DIRECTORY_PORT#${DS_PORT}#g" \
       -e "s#DIRECTORY_SERVER#${DS_HOST}#g" \
       -e "s#ENCLDAPUSERPASSWD#$ENCLDAPUSERPASSWD#g" \
       -e "s#ENCADMINPASSWD#$ENCADMINPASSWD#g" \
       -e "s#SERVER_URL#$serverservice_url#g" \
       -e "s#PROFILE_URL#$profileservice_url#g" \
       -e "s#NOTIFICATION_URL#$notification_url#g" \
       -e "s#PROFILE_HOST#$SERVER_HOST#g" \
       -e "s#PROFILE_PORT#$SERVER_PORT#g" \
       -e "s#DPRO_VERSION#$VERSION#g" \
       -e "s#ROOT_SUFFIX#$ROOT_SUFFIX#g" \
       -e "s#CURRENT_PLATFORM_LOCALE#$PLATFORM_LOCALE#g" \
       -e "s#JSSHOME#$JSSHOME#g" \
       -e "s#NSSHOME#$NSSHOME#g" \
       -e "s#USER_NAMING_ATTR#$USER_NAMING_ATTR#g" \
       -e "s#NSPRHOME#$NSPRHOME#g" \
       -e "s#XML_ENCODING#$XML_ENCODING#g" \
       -e "s#WEB_CONTAINER#$CLIENT_CONTAINER_TAG#g" \
       -e "s#IS_INSTALL_ETCDIR#${IS_INSTALL_ETCDIR}#g" \
       -e "s#IS_INSTALL_VARDIR#${IS_INSTALL_VARDIR}#g" \
       -e "s#IS_PRODNAME#${PRODUCT_DIR}#g" \
       -e "s#TAG_JSS_SO_PATH#${JSS_PATH}#g" \
       -e "s#TAG_JSS_JAR_PATH#${JSS_JAR_PATH}#g" \
       -e "s#TAG_NSS_SO_PATH#${NSS_PATH}#g" \
       -e "s#TAG_NSPR_SO_PATH#${NSPR_PATH}#g" \
       $file+ > $file
        rm -f $file+
    done
    
    if [ ${WEB_CONTAINER:-none} = "WL6" -o ${WEB_CONTAINER:-none} = "WL8" -o ${WEB_CONTAINER:-none} = "WAS5" ];then
     for file in $EDIT_FILES; do
      $CP $file $file+
      sed -e "s#COOKIE_ENCODE#true#g" \
       $file+ > $file
        rm -f $file+
     done
    fi

    # Set FQDN for remote console installation
    if [ "$CONSOLE_REMOTE" = "true" ] && [ "$CONSOLE_HOST" != "" ]; then
	amconfigfile=${CONFIG_DIR}/AMConfig.properties
	$CP $amconfigfile $amconfigfile+
	sed -e "s;#com.sun.identity.server.fqdnMap\[<invalid-name>\]=<valid-name>;com.sun.identity.server.fqdnMap[$CONSOLE_HOST]=$CONSOLE_HOST;g" \
	$amconfigfile+ > $amconfigfile
	rm -f $amconfigfile+
    fi

    # Enable the polling mode if on SDK client box
    if [ ${DEPLOY_LEVEL} -eq 3 -o ${DEPLOY_LEVEL} -eq 4 ]; then
	$CP ${CONFIG_DIR}/AMConfig.properties ${CONFIG_DIR}/AMConfig.properties+
        sed -e "s#com.iplanet.am.session.client.polling.enable=false#com.iplanet.am.session.client.polling.enable=true#g" \
	${CONFIG_DIR}/AMConfig.properties+ > ${CONFIG_DIR}/AMConfig.properties
	rm -f ${CONFIG_DIR}/AMConfig.properties+
    fi

       
    MAKE_FILE_SSO=`$LS -1 ${PKGDIR}/samples/sso/Makefile`
    cd ${PKGDIR}/samples/sso
    for file in $MAKE_FILE_SSO; do
      cp -p $file $file+
      sed -e "s#JAVADIR#${JAVA_HOME}#g" \
          -e "s#BASEDIR#${BASEDIR}#g" \
      $file+ > $file
      rm -f $file+
    done
    
    RUN_FILE_SSO=`$LS -1 ${PKGDIR}/samples/sso/run`
    cd ${PKGDIR}/samples/sso
    for file in $RUN_FILE_SSO; do
      cp -p $file $file+
      sed -e "s#BASEDIR#${BASEDIR}#g" \
          -e "s#JSSJARPATH#${JSS_JAR_PATH}#g" \
          -e "s#JSSPATH#${JSS_SO_PATH}#g" \
          -e "s#JAVADIR#${JAVA_HOME}#g" \
      $file+ > $file
      rm -f $file+
    done
cd $curr_dir

}

misc_pkgadd()
{
pkgname=$1
path=$2
basedir=$3

# buidup the admin & response file
AFILE=/tmp/am63.upg
cat <<EOF > $AFILE
#ident  "@(#)default    1.4     92/12/23 SMI"   /* SVr4.0  1.5.2.1      */ 
mail=
instance=unique
partial=nocheck
runlevel=nocheck
idepend=nocheck
rdepend=nocheck
space=nocheck
setuid=nocheck
conflict=nocheck
action=nocheck
basedir=${basedir:-default}
EOF

RFILE=/tmp/am63.rfile
cat <<EOF > $RFILE
y
y
y
EOF

pkgadd -n -a $AFILE -d $path -r $RFILE -R / $pkgname
return $?
}

modifySessionServiceSchema() {
	cd /etc/opt/SUNWam/config/xml
	$GREP " encoding=" amSession.xml > /var/opt/SUNWam/modifySession.xml
	$ECHO  " <!DOCTYPE Requests
       PUBLIC \"-//iPlanet//Sun Java System Access Manager 2005Q1 Admin CLI DTD//EN\"
       \"jar://com/iplanet/am/admin/cli/amAdmin.dtd\"
        >" >> /var/opt/SUNWam/modifySession.xml
	$ECHO  "<!--  MODIFY REQUESTS -->
      	<Requests>
     	<SchemaRequests serviceName=\"iPlanetAMSessionService\"
           SchemaType=\"global\" SubSchema=\"Site\">
       		<ModifyDefaultValues>
         	  <AttributeValuePair>
                     <Attribute name=\"iplanet-am-session-store-cpl-max-wait-time\"/>
                      <Value>5000</Value>
         	  </AttributeValuePair>
       		</ModifyDefaultValues>
      	</SchemaRequests>
      	</Requests>" >> /var/opt/SUNWam/modifySession.xml

	$ECHO "        As the fix of bug 6290949, the value of service schema attribute
        iplanet-am-session-store-cpl-max-wait-time of iPlanetAMSessionService
        needs to be modified by amadmin tool. 
        The input xml file modifySession.xml is prepared in /var/opt/SUNWam, 
        please invoke following command after the patch is installed
        $BASEDIR/SUNWam/bin/amadmin -u amadmin -w <password> -t 
        /var/opt/SUNWam/modifySession.xml "
}

modifyAMAdminConsoleServiceSchema() {

	cd /etc/opt/SUNWam/config/xml
	encode=`$GREP " encoding=" amAdminConsole.xml`
	$ECHO  $encode '
<!--
    Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved
    Use is subject to license terms.
-->

<!DOCTYPE Requests
    PUBLIC "-//iPlanet//Sun Java System Access Manager 2005Q1 Admin CLI DTD//EN"    "jar://com/iplanet/am/admin/cli/amAdmin.dtd" >

<!--  MODIFY REQUESTS -->

<Requests>

<SchemaRequests serviceName="iPlanetAMAdminConsoleService"
    SchemaType="Global" >
    <AddAttributeSchema fileName="/var/opt/SUNWam/newConsoleServiceAttribute.xml" />

</SchemaRequests>

</Requests> ' > /var/opt/SUNWam/addConsoleServiceAttributeRequest.xml

   $ECHO $encode '
<!--
    Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved
    Use is subject to license terms.
-->

<!DOCTYPE Requests
    PUBLIC "-//iPlanet//Sun Java System Access Manager 2005Q1 Admin CLI DTD//EN"    "jar://com/sun/identity/sm/sms.dtd" >


<Requests>

        <AttributeSchema name="iplanet-am-admin-console-show-group-base"
            type="single"
            syntax="boolean"
            i18nKey="g100a">
            <DefaultValues>
                <Value>false</Value>
            </DefaultValues>
        </AttributeSchema>

</Requests> ' > /var/opt/SUNWam/newConsoleServiceAttribute.xml



	$ECHO "
	Fix for bug #6388761, the value of attribute
	iplanet-am-admin-console-show-group-base  of 
	iPlanetAMAdminConsoleService needs to be modified by
	amadmin tool. The input xml file 
	addConsoleServiceAttributeRequest.xml is prepared in 
	/var/opt/SUNWam please invoke following command after
	the patch is installed 
	$BASEDIR/SUNWam/bin/amadmin -u amadmin -w <password> -t 
	/var/opt/SUNWam/addConsoleServiceAttributeRequest.xml
        "
}

addAttribAuthServiceSchema()
{
$ECHO '<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved
    Use is subject to license terms.
-->


<!DOCTYPE Requests
    PUBLIC "-//iPlanet//Sun Java System Access Manager 2005Q1 Admin CLI DTD//EN"
    "jar://com/sun/identity/sm/sms.dtd"
>

<Requests>

<AttributeSchema name="iplanet-am-auth-callback-plugins"
                     type="list"
                     syntax="string"
                     i18nKey="a142">
                 </AttributeSchema>
</Requests>
' > /var/opt/SUNWam/schemaAddAttributeAuthService.xml

$ECHO '<?xml version="1.0" encoding="ISO-8859-1"?>
<!--
    Copyright (c) 2004 Sun Microsystems, Inc. All rights reserved
    Use is subject to license terms.
-->


<!DOCTYPE Requests
    PUBLIC "-//iPlanet//Sun Java System Access Manager 2005Q1 Admin CLI DTD//EN"    "jar://com/iplanet/am/admin/cli/amAdmin.dtd"
>

<!--  MODIFY REQUESTS -->

<Requests>
<SchemaRequests serviceName="iPlanetAMAuthService"
                SchemaType="Organization" >
   <AddAttributeSchema fileName="/var/opt/SUNWam/schemaAddAttributeAuthService.xml" />

</SchemaRequests>
</Requests>

' > /var/opt/SUNWam/updateAuthService.xml

        $ECHO "        As the fix of bug 6222704, a new attribute
        iplanet-am-auth-callback-plugins has to be added to the 
        iPlanetAMAuthService using the  amadmin tool.
        The input xml file updateAuthService.xml is prepared in 
        /var/opt/SUNWam, please invoke following command after the patch 
        is installed $BASEDIR/SUNWam/bin/amadmin -u amadmin -w <password> -t
        /var/opt/SUNWam/updateAuthService.xml "

}
############
## main
###########

. /tmp/.amsilent
set_path_for_config

ORG_ROOT_SUFFIX=$ROOT_SUFFIX
DEFAULTORG=$ORG_ROOT_SUFFIX
ORG_BASE=$ORG_ROOT_SUFFIX
COOKIE_DOMAIN_LIST=$COOKIE_DOMAIN
AVAILABLE_LOCALES="<Value>en_US</Value>"
UM_ENABLED=true

#####################
## WS6 => s1ws 6.1 ##
#####################
WS61_INSTANCE=https-$SERVER_HOST
WS61_PROTOCOL=$SERVER_PROTOCOL
WS61_HOST=$SERVER_HOST
WS61_PORT=$SERVER_PORT


#####################
## WS6 => s1ws 6.1 ##
#####################
#AS70_HOME=/opt/SUNWappserver7  get the value from prepatch
AS70_PROTOCOL=$SERVER_PROTOCOL
AS70_HOST=$SERVER_HOST
AS70_PORT=$SERVER_PORT
AS70_INSTANCE=server1
AS70_DOMAIN=domain1
AS70_INSTANCE_DIR=/var/opt/SUNWappserver7/domains/${AS70_DOMAIN:-domain1}/${AS70_INSTANCE:-server1}
AS70_DOCS_DIR=/var/opt/SUNWappserver7/domains/${AS70_DOMAIN:-domain1}/${AS70_INSTANCE:-server1}/docroot

#################################

UPGRADE61to62_XML_DIR=${BASEDIR}/${PRODUCT_DIR}/migration/61to62/61to62
PKGDIR=${BASEDIR}/${PRODUCT_DIR}

pkginfo -q SUNWamsvc
if [ $? = 0 ];then
   ldif_config_tag_swap
   xml_config_tag_swap
   tag_swap_samples

   # add new sfodb pkg now
#   echo "pkgadd SUNWamsfodb from $PROG_DIR/fullbits ...." 
#   misc_pkgadd SUNWamsfodb "$PROG_DIR/fullbits" 
#   misc_pkgadd SUNWbdb "$PROG_DIR/fullbits" 
#   misc_pkgadd SUNWbdbj "$PROG_DIR/fullbits" 
else
   sdk_config_tag_swap
fi
#misc_pkgadd SUNWamclnt "$PROG_DIR/fullbits" "${BASEDIR}"

cp $CONFIG_DIR/.serverconfig.xml.6.3 $CONFIG_DIR/serverconfig.xml

updateVersionString

#$RM /tmp/.amsilent

if [ "WEB_CONTAINER"="$WEB_CONTAINER" ]; then
        echo ""
        echo "Warning: WEB_CONTAINER related settings may not be set correct"
        echo "Set correct values in /tmp/.amsilent file before you run amconfig"
        echo ""
fi
modifySessionServiceSchema
modifyAMAdminConsoleServiceSchema
addAttribAuthServiceSchema

exit 0
