#!/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 réservés. Sun
# Microsystems, Inc. détient les droits de propriété intellectuels relatifs
# à la technologie incorporée dans le produit qui est décrit dans ce document.
# En particulier, et ce sans limitation, ces droits de propriété
# intellectuelle peuvent inclure un ou plus des brevets américains listés
# à l'adresse http://www.sun.com/patents et un ou les brevets supplémentaires
# 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 développés par des
# tierces parties.
# 
# Sun, Sun Microsystems, le logo Sun, Java et Sun[tm] ONE sont des marques
# de fabrique ou des marques déposées de Sun Microsystems, Inc. aux
# Etats-Unis et dans d'autres pays.


# script variables

DIT_COMPLIANCE=false
DCTREE_SUFFIX=false
UM_ENABLED=true

###########################################################
# Finds amutils and sources it. This function is identical
# in all scripts. Any changes made here need to made in
# other scripts too.
source_amutils()
{
	# Try to guess the location of amutils
	if [ "$BASEDIR" != "" -a "$PRODUCT_DIR" != "" -a -f $BASEDIR/$PRODUCT_DIR/bin/amutils ]; then
		AMUTILS=$BASEDIR/$PRODUCT_DIR/bin/amutils
	elif [ "$BASEDIR" != "" -a "$PRODUCT_DIR" != "" -a -f $BASEDIR/$PRODUCT_DIR/share/bin/amutils ]; then
		AMUTILS=$BASEDIR/$PRODUCT_DIR/share/bin/amutils
	elif [ -f ./amutils ]; then
		AMUTILS=./amutils
	elif [ -f `dirname $0`/amutils ]; then
		AMUTILS=`dirname $0`/amutils
	elif [ -f /opt/SUNWam/bin/amutils ]; then
		AMUTILS=/opt/SUNWam/bin/amutils
	elif [ -f /opt/SUNWam/share/bin/amutils ]; then
		AMUTILS=/opt/SUNWam/share/bin/amutils ]
	elif [ -f /opt/sun/identity/bin/amutils ]; then
		AMUTILS=/opt/sun/identity/bin/amutils
	elif [ -f /opt/sun/identity/share/bin/amutils ]; then
		AMUTILS=/opt/sun/identity/share/bin/amutils ]
	else
		echo "$0: FATAL: Could not find amutils. Exiting."
		exit 1
	fi

	# Source it!
	echo "$0: Sourcing $AMUTILS"
	. $AMUTILS
}

if [ ${#} -eq 0 ]; then
	INSTALL_FILE_NOT_REQUIRED=true
	source_amutils
	do_interactive
	set -- -s $IS_SILENT_INSTALL_FILE
	INSTALL_FILE_NOT_REQUIRED=false
elif [ $1 = "-s" ]; then 
    if [ ! -f $2 ]; then 
        echo Silent file not found: $2 
        exit 
    fi 
    IS_SILENT_INSTALL_FILE=$2 
    export IS_SILENT_INSTALL_FILE 
else 
    echo "Usage: amsdkconfig [ -s <silent> ]" 
    exit 
fi

source_amutils

#############################################
###  Description - This script only configure 
###  the SDK of the Identity Server 
#############################################

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

    # User input required for SDK Only Install - CHECK with Orion Installer - ?
    # JAVA_HOME
    # BASEDIR
    # PRODUCT_DIR
    # SERVER_HOST
    # SERVER_PORT
    # SERVER_PROTOCOL
    # SERVER_DEPLOY_URI
    # CONSOLE_DEPLOY_URI
    # PASSWORD_DEPLOY_URI
    # ADMINPASSWD
    # AMLDAPUSERPASSWD
    # CONFIG_DIR
    # ROOT_SUFFIX
    # ORG_BASE
    # USER_NAMING_ATTR 
    # DS_PORT
    # DS_HOST
    # VERSION
    # PLATFORM_LOCALE
    # TEMP_DIR_PREFIX
    # DEBUG_DIR_PREFIX

    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:-none}" = "WS6" ];then                # webserver 6.1
      CLIENT_HOST=$WS61_HOST
      CLIENT_PORT=$WS61_PORT
      CLIENT_CONTAINER_TAG="WEB_CONTAINER"
    elif [ "{$WEB_CONTAINER:-none}" = "AS7" ];then      # AS 7.0
      CLIENT_HOST=$AS70_HOST
      CLIENT_PORT=$AS70_PORT
      CLIENT_CONTAINER_TAG="IAS7.0"
    elif [ "${WEB_CONTAINER:-none}" = "WL6" ];then      # weblogic 6.1
      CLIENT_HOST=$WL61_HOST
      CLIENT_PORT=$WL61_PORT
      CLIENT_CONTAINER_TAG="BEA6.1"
    elif [ "${WEB_CONTAINER:-none}" = "WL8" ];then      # weblogic 8.1
      CLIENT_HOST=$WL8_HOST
      CLIENT_PORT=$WL8_PORT
      CLIENT_CONTAINER_TAG="BEA8.1"
    elif [ "${WEB_CONTAINER:-none}" = "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="6.2"
    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
    
    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

       ## Tags not used in SDK Only install Case. Just leave it here commented incase. 
       ## we need it later. #
       # -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#AMLDAPUSERPASSWD#$LDAPUSERPASSWD#g" \
       # -e "s#SESSION_URL#$session_url#g" \
       # -e "s#LOGGING_URL#$loggingservice_url#g" \
       # -e "s#AVAILABLE_LOCALES#$AVAILABLE_LOCALES#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#DCTREE_SUFFIX#$DCTREE_SUFFIX#g" \
       # -e "s#GROUP_TYPE#$GROUP_TYPE#g" \
       # -e "s#NM_INST_ORGANIZATION#$NM_INST_ORGANIZATION#g" \
       # -e "s#WEBSERVER_HOST#$WEBSERVER_HOST#g" \
       # -e "s#SERVER_URL#$server_url#g" \ 
       # -e "s#CONSOLE_REMOTE#$CONSOLE_REMOTE#g" \ 
       # -e "s#HOST_NAME#$HOST_NAME#g" \ 
       # -e "s#FULLHOSTNAME#$SERVER_HOST#g" \
       # -e "s#DIT_COMPLIANCE#$DIT_COMPLIANCE#g" \
       # -e "s#NM_ROOT_SUFFIX#$NM_ROOT_SUFFIX#g" \
       # -e "s#RS_RDN#$RS_RDN#g" \
       # -e "s#SIMPLE_HOST#$DS_HOST#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#WS_BASE_DIR#$WS61_HOME#g" \
       # -e "s#WEBSVR_INSTANCE#$WS61_HOME#g" \
       # -e "s#WSVR_INSTANCE#$WSVR_INSTANCE#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" \

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


##############################################################
#
#           MAIN - STARTING MAIN ROUTINE
#

# check that user is root
# This function is defined in common.lib library
check_root_user

create_password_files

# Set required path for this script
set_path_for_config

# tag swap
config_tag_swap 

delete_password_files
