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


###########################################################
# 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: amsvcconfig [ -s <silent> ]" 
    exit 
fi

source_amutils

DIT_COMPLIANCE=false
DCTREE_SUFFIX=false
UM_ENABLED=true


#############################################################
###  Description - This script loads all the Identity Server 
###  Services Schema into the Directory Server.
#############################################################

##############################################################
#
# Tag swap of all the service Schema XML files and also the 
# AMConfig.properties file
#
config_tag_swap() {
    #
    # 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
    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 -1 ${CONFIG_DIR}/xml`

# 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="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"`
    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
       EDIT_FILES="${PKGDIR}/bin/amadmin ${CONFIG_DIR}/ums/amserveradmin ${CONFIG_DIR}/AMConfig.properties ${CONFIG_DIR}/serverconfig.xml ${PKGDIR}/bin/ampassword $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}/bin/amverifyarchive ${PKGDIR}/migration/61to62/install_61to62_upgrade.ldif ${PKGDIR}/migration/61to62/ds_remote_schema_61to62upgrade.ldif ${UPGRADE61to62_XML_FILES}"
    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
    
    escape_string_all $ADMINPASSWD
    ESCAPED_ADMINPASSWD=$x3

    # 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#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#$ESCAPED_ADMINPASSWD#g" \
       -e "s#PROXUUSERPASSWORD#$ESCAPED_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#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
}

##############################################################
#
# Function - Checks whether the IS Service schemas are loaded or not.
# Accordingly sets the value for the local variable IS_SERVICE_LOADED
#
is_service_loaded() {
    IS_SERVICE_LOADED=false
    if [ ! -f ${PKGDIR}/bin/ldapsearch ]; then
        eval $ECHO "`$gettext 'ldapsearch tool not found at ${PKGDIR}/bin/ldapsearch'`"
        eval $ECHO "`$gettext 'Exiting the install script'`"
        exit
    fi
    DAI_SERVICE=`${PKGDIR}/bin/ldapsearch -b "$ORG_ROOT_SUFFIX" -D "$DS_DIRMGRDN" -j "$FILE_DS_DIRMGRPASSWD" -h "$DS_HOST" -p "$DS_PORT" ou=DAI `
    if [ "$DAI_SERVICE" != "" ]; then
        $ECHO "Directory Server is already loaded with IS Service Schema. IS Service schema will not be loaded."
        IS_SERVICE_LOADED=true
    fi
    export IS_SERVICE_LOADED
}


##############################################################
#
# Load schema. Import services
#
load_schema() {

    if [ "$IS_SERVICE_LOADED" = "true" ]; then
        return
    fi
    
    eval $ECHO "`$gettext 'Loading service schema XML files ...'`"
    for file in $XML_FILES; do
	if [ "$DIT_COMPLIANCE" = "true" ]; then
	    if [ "$file" != "amEntrySpecific.xml" ] && [ "$file" != "amAdminConsole.xml" ]; then
        	SXML_FILES="$SXML_FILES ${CONFIG_DIR}/xml/$file"
	    fi
	else
	    if [ "$file" != "amEntrySpecificCompliant.xml" ] && [ "$file" != "amAdminConsoleCompliant.xml" ]; then
        	SXML_FILES="$SXML_FILES ${CONFIG_DIR}/xml/$file"
	    fi
	fi
    done
    
    $ADMIN_CLI --runasdn "$USER_NAMING_ATTR=amAdmin,ou=People,$ORG_BASE" -f "$FILE_ADMINPASSWD" -v --nolog --schema ${UMS_XML}
    
    if [ $? -ne 0 ]; then
        eval $ECHO "`$gettext 'Error occured while loading: ${UMS_XML}'`"
       	exit 1
    else
       	eval $ECHO "`$gettext 'Successfully loaded: ${UMS_XML}'`"
    fi
    
    $ADMIN_CLI --runasdn "$USER_NAMING_ATTR=amAdmin,ou=People,$ORG_BASE" -f "$FILE_ADMINPASSWD" -v --nolog --schema $SXML_FILES
        
    if [ $? -ne 0 ]; then
        eval $ECHO "`$gettext 'Error occured while loading: $file'`"
       	exit 1
    else
       	eval $ECHO "`$gettext 'Successfully loaded: $file'`"
    fi
    eval $ECHO "`$gettext 'Done loading service schema XML files.'`"
}


##############################################################
#
# Load schema. Import services
#
remove_schema() {

    SERVICE_NAMES="iPlanetAMAdminConsoleService \
            iPlanetAMAgentService \
            iPlanetAMAuthService \
            iPlanetAMAuthAnonymousService \
            iPlanetAMAuthCertService \
            iPlanetAMAuthConfiguration \
            iPlanetAMAuthHTTPBasicService \
            iPlanetAMAuthLDAPService \
            iPlanetAMAuthMembershipService \
            iPlanetAMAuthNTService \
            iPlanetAMAuthRadiusService \
            iPlanetAMAuthSafeWordService \
            iPlanetAMAuthSecurIDService \
            iPlanetAMAuthUnixService \
            iPlanetAMAuthWindowsDesktopSSOService \
            iPlanetAMAuthenticationDomainConfigService \
            SunAMClientData \
            sunIdentityServerDiscoveryService \
            iPlanetAMClientDetection \
            iPlanetAMEntrySpecificService \
            iPlanetG11NSettings \
            sunIdentityServerLibertyPPService \
            iPlanetAMLoggingService \
            iPlanetAMNamingService \
            iPlanetAMPasswordResetService \
            iPlanetAMPlatformService \
            iPlanetAMPolicyService \
            iPlanetAMPolicyConfigService \
            iPlanetAMProviderConfigService \
            iPlanetAMSAMLService \
            sunIdentityServerSOAPBinding \
            iPlanetAMSessionService \
            iPlanetAMUserService \
            iPlanetAMWebAgentService \
            sunIdentityLocaleService \
            DAI"
    
    ORG_BASE=$ORG_ROOT_SUFFIX
    
    ans="n" 
    for service_name in $SERVICE_NAMES; do
        $ECHO
        eval $ECHO "`$gettext '**********Going to remove the service :- $service_name **********'`"
        $ADMIN_CLI --runasdn "$USER_NAMING_ATTR=amAdmin,ou=People,$ORG_BASE" --password "$ADMINPASSWD" -v --nolog -r $service_name
	if [ $? -ne 0 ]; then
                eval $ECHO "`$gettext 'Error occured while removing the service:- $service_name'`"
                if [ "$ans" != y ]; then
                    $ECHO
                    $ECHO "`$gettext 'want to continue on such errors y/[n] ?'`"
                    read ans
                    if [ "$ans" != "y" ]; then
                        exit 1
                    fi
                fi
	else
		eval $ECHO "`$gettext '**********Successfully removed the service :- $service_name **********'`"
                
	fi
    done
}

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

##############################################################
#
# Modify services. 
#
modify_services(){
        
        if [ "$IS_SERVICE_LOADED" = "true" ]; then
            return
        fi

	if [ "$ABS_REQUEST_FILES" != "" ]; then
		eval $ECHO "`$gettext 'Modifying services using request XML files ...'`"

		$ADMIN_CLI --runasdn "$USER_NAMING_ATTR=amAdmin,ou=People,$ORG_BASE" -f "$FILE_ADMINPASSWD" -v --nolog --data ${ABS_REQUEST_FILES}
	
		if [ $? -ne 0 ]; then
			eval $ECHO "`$gettext 'Error occured while loading: $ABS_REQUEST_FILES'`"
			exit 1
		else
			eval $ECHO "`$gettext 'Successfully loaded: $ABS_REQUEST_FILES'`"
		fi
		eval $ECHO "`$gettext 'Done modifying services using request XML files.'`"
	fi
	eval $ECHO "`$gettext 'No request XML files found.'`"

}

# End PORTALMA


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

OPTION=$3

if [ "$OPTION" = "remove" ]; then
    remove_schema
else
    config_tag_swap
    if [ "$OPTION" = "load" ]; then
        # Load services irrespective of the DIRECTORY_MODE
        load_schema

        # 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.
        # Modify Services
        modify_services
        # End PORTALMA
    else    
        # Load services based on the DIRECTORY_MODE
        if [ "$DIRECTORY_MODE" = "1" ] || [ "$DIRECTORY_MODE" = "2" ]; then
            load_schema

            # 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.
            # Modify Services
            modify_services
            # End PORTALMA
        fi
    fi
fi

delete_password_files
