#!/bin/ksh

###############################################
# Sourcing macros
###############################################
DIRNAME=/usr/bin/dirname
. `$DIRNAME $0`/../lib/psmacros

BELL_CHAR='\a'

NLP_STATE_FILE="$PS_CONFIG_DIR/NLPConfig.properties"

IS_PKGINFO_FILE="/var/sadm/pkg/SUNWamsvc/pkginfo"
IS_SDK_PKGINFO_FILE="/var/sadm/pkg/SUNWamsdk/pkginfo"
IS_CONFIG_FILE="$IDSAME_CONFIG_DIR/config/AMConfig.properties"

JSS_NSS_NSPR_LIBPATH=$JSS_NSS_NSPR_LIB_DIR
JSS_JAR=$JSS_JAR_LIB_DIR/jss4.jar

################################################################
# Get configuration from specified file
################################################################

GrabConfig() {

    GRABCONFIG_FILE=$1
    GRABCONFIG_KEY=$2
    GRABCONFIG_SEPARATOR=$3

    ANSWER=`$GREP "^$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR" $GRABCONFIG_FILE | $UNIQ | $SED -e "s/$GRABCONFIG_KEY$GRABCONFIG_SEPARATOR//"`

}

################################################################
# Generate cert DB and self-signed cert
################################################################

GenerateCert() {

    $MKDIR -p $CERT_DIR
    $CHMOD 755 $CERT_DIR

    #To workaround JDK JNI bug on Linux
    if [ "$OSTYPE" = "Linux" ]; then
        EXTRA_LIBS="$JDK_DIR/jre/lib/i386/native_threads"
    else
        EXTRA_LIBS=""
    fi

    if [ -z "${LD_LIBRARY_PATH}" ]; then
        LD_LIBRARY_PATH="$JSS_NSS_NSPR_LIBPATH:$LIB_DIR/solaris/sparc:$EXTRA_LIBS"
    else
        LD_LIBRARY_PATH="$JSS_NSS_NSPR_LIBPATH:$LIB_DIR/solaris/sparc:$EXTRA_LIBS:${LD_LIBRARY_PATH}"
    fi

    IS_HOME=$IS_BASEDIR/$IDSAME_PRODUCT_DIR
    JCE_CLASSPATH="$IS_HOME/lib/jce1_2_1.jar:$IS_HOME/lib/local_policy.jar:$IS_HOME/lib/US_export_policy.jar:$IS_HOME/lib/sunjce_provider.jar"
    CLASSPATH=$LIB_DIR/certadmin.jar:$JSS_JAR:$JCE_CLASSPATH:$NLP_BASEDIR/$PS_PRODUCT_DIR/locale

    export LD_LIBRARY_PATH
    export CLASSPATH

    if [ ! -f "$CERT_DIR/libnssckbi.so" ]; then
    $LN -s $JSS_NSS_NSPR_LIBPATH/libnssckbi.so $CERT_DIR/libnssckbi.so
    fi

    $JDK_DIR/bin/java com.sun.portal.cli.cert.CertUtil -option=createselfsigncert "$CERT_DIR" "${CERT_DB_PASSWORD}" "en_US" "$CERT_INFO" "" "$NICKNAME" "6"

    if [ ! -f $KEY3_DB_FILE ]; then
        $CHMOD 644 $KEY3_DB_FILE
        $CHMOD 644 $CERT7_DB_FILE
        $CHMOD 644 $SECMOD_DB_FILE
    fi

}

################################################################
# Exit if non root user is executing this script
################################################################

CheckUser() {

    if [ `$ID | $AWK '{print $1}'` != "uid=0(root)" ]; then
        $ECHO "You must be root user. $BELL_CHAR"
        exit 1
    fi

}

################################################################
# Exit if NLPConfig state file in not present
################################################################

CheckStateFile() {

    if [ ! -f $NLP_STATE_FILE ]; then
        $ECHO "Error: $NLP_STATE_FILE does not exist. $BELL_CHAR"
        exit 1
    fi

}

################################################################
# Get the Gateway profile name from NLPConfig state file.
################################################################
GetGatewayProfileName() {

    GrabConfig $NLP_STATE_FILE "NLP_GATEWAY_PROFILE" "="
    if [ "$ANSWER" != "" ]; then
        NLP_GATEWAY_PROFILE=$ANSWER
    else
        $ECHO "Error: Cannot determine NLP_GATEWAY_PROFILE. $BELL_CHAR"
        exit 1
    fi

}

################################################################
# Copy the NLPConfig state file to NLPConfig-<profilename>
################################################################

CopyStateFile() {

    NEW_NLP_STATE_FILE="$PS_CONFIG_DIR/NLPConfig-$NLP_GATEWAY_PROFILE.properties"
    $CP $NLP_STATE_FILE $NEW_NLP_STATE_FILE

}

################################################################
# Update the state file pointer to point to current state file.
################################################################

UpdateStateFilePointer() {
	
    NEW_NLP_STATE_FILE="$PS_CONFIG_DIR/NLPConfig-$NLP_GATEWAY_PROFILE.properties"
    NLP_STATE_FILE=$NEW_NLP_STATE_FILE

}

################################################################
# Read all the configuration informations
################################################################
GetNLPConfigInfo() {
    
    # JDK base directory
    GrabConfig "$IS_CONFIG_FILE" "com.iplanet.am.jdk.path" "="
    if [ "$ANSWER" != "" ]; then
        JDK_DIR=$ANSWER
    else
        $ECHO "Error: Cannot determine JDK_DIR. $BELL_CHAR"
        exit 1
    fi

    # Netlet proxy base directory
    GrabConfig $NLP_STATE_FILE "NLP_BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
        NLP_BASEDIR=$ANSWER
    else
        $ECHO "Error: Cannot determine NLP_BASEDIR. $BELL_CHAR"
        exit 1
    fi

    # Netlet proxy protocol
    GrabConfig $NLP_STATE_FILE "NLP_PROTOCOL" "="
    if [ "$ANSWER" != "" ]; then
        NLP_PROTOCOL=$ANSWER
    else
        $ECHO "Error: Cannot determine NLP_PROTOCOL. $BELL_CHAR"
        exit 1
    fi

    # Netlet proxy host
    GrabConfig $NLP_STATE_FILE "NLP_HOST" "="
    if [ "$ANSWER" != "" ]; then
        NLP_HOST=$ANSWER
    else
        $ECHO "Error: Cannot determine NLP_HOST. $BELL_CHAR"
        exit 1
    fi

    # Netlet proxy port
    GrabConfig $NLP_STATE_FILE "NLP_PORT" "="
    if [ "$ANSWER" != "" ]; then
        NLP_PORT=$ANSWER
    else
        $ECHO "Error: Cannot determine NLP_PORT. $BELL_CHAR"
        exit 1
    fi

    # Netlet proxy IP address
    GrabConfig $NLP_STATE_FILE "NLP_IP" "="
    if [ "$ANSWER" != "" ]; then
        NLP_IP=$ANSWER
    else
        $ECHO "Error: Cannot determine NLP_IP. $BELL_CHAR"
        exit 1
    fi

    # Create self signed certficate ?
    GrabConfig $NLP_STATE_FILE "SELF_SIGNED_CERT" "="
    if [ "$ANSWER" != "" ]; then
        SELF_SIGNED_CERT=$ANSWER
    else
        $ECHO "Error: Cannot determine SELF_SIGNED_CERT. $BELL_CHAR"
        exit 1
    fi
    
    # Certificate information
    if [ "$SELF_SIGNED_CERT" = "y" ]; then
        GrabConfig $NLP_STATE_FILE "CERT_INFO" "="
        if [ "$ANSWER" != "" ]; then
            CERT_INFO=$ANSWER
        else
            $ECHO "Error: Cannot determine CERT_INFO. $BELL_CHAR"
            exit 1
        fi
    fi

    # Start netlet proxy after installation ?
    GrabConfig $NLP_STATE_FILE "START_NETLETPROXY" "="
    if [ "$ANSWER" != "" ]; then
        START_NETLETPROXY=$ANSWER
    else
        $ECHO "Error: Cannot determine START_NETLETPROXY. $BELL_CHAR"
        exit 1
    fi

    # Portal server protocol
    GrabConfig $NLP_STATE_FILE "SERVER_PROTOCOL" "="
    if [ "$ANSWER" != "" ]; then
        SERVER_PROTOCOL=$ANSWER
    else
        $ECHO "Error: Cannot determine SERVER_PROTOCOL. $BELL_CHAR"
        exit 1
    fi

    # Portal server host
    GrabConfig $NLP_STATE_FILE "SERVER_HOST" "="
    if [ "$ANSWER" != "" ]; then
        SERVER_HOST=$ANSWER
    else
        $ECHO "Error: Cannot determine SERVER_HOST. $BELL_CHAR"
        exit 1
    fi

    # Portal server port
    GrabConfig $NLP_STATE_FILE "SERVER_PORT" "="
    if [ "$ANSWER" != "" ]; then
        SERVER_PORT=$ANSWER
    else
        $ECHO "Error: Cannot determine SERVER_PORT. $BELL_CHAR"
        exit 1
    fi

    # Portal server load balancer URL.
    GrabConfig $NLP_STATE_FILE "LOAD_BALANCER_URL" "="
    if [ "$ANSWER" != "" ]; then
        PS_LB_URL=$ANSWER
    else
        $ECHO "Error: Cannot determine LOAD_BALANCER_URL. $BELL_CHAR"
        exit 1
    fi
   
    # Derive PS_PROTOCOL, PS_HOST, PS_PORT, PS_URL and DEPLOY_URI from PS_LB_URL.
    PS_PROTOCOL=`echo $PS_LB_URL | awk '{ print substr($0, 0, index($0,":")-1) }'`

    TMP_STR=`echo $PS_LB_URL | awk '{ print substr($0, index($0,"/")+2) }'`
    DEPLOY_URI=`echo $TMP_STR | awk '{ print substr($0, index($0,"/")) }'`
      
    TMP_STR=`echo $TMP_STR | awk '{ print substr($0, 0, index($0,"/")-1) }'`
    PS_HOST=`echo $TMP_STR | awk '{ print substr($0, 0, index($0,":")-1) }'`
    
    if [ "$PS_HOST" == "" ]; then
      PS_HOST=$TMP_STR
      PS_PORT="80"
    else
      PS_PORT=`echo $TMP_STR | awk '{ print substr($0, index($0,":")+1) }'`
    fi

    PS_URL="$PS_PROTOCOL://$PS_HOST:$PS_PORT"

    # Identity server/SDK base directory
    GrabConfig $NLP_STATE_FILE "IDSAME_BASEDIR" "="
    if [ "$ANSWER" != "" ]; then
        IS_BASEDIR=$ANSWER
    else
        $ECHO "Error: Cannot determine IDSAME_BASEDIR. $BELL_CHAR"
        exit 1
    fi

    # Create IS instance ?
    GrabConfig $NLP_STATE_FILE "CREATE_IS_INSTANCE" "="
    if [ "$ANSWER" != "" ]; then
        CREATE_IS_INSTANCE=$ANSWER
    else
        $ECHO "Error: Cannot determine CREATE_IS_INSTANCE. $BELL_CHAR"
        exit 1
    fi

}

################################################################
# Grab the existing Identity information.
# Uses pkginfo file.
################################################################
GetExistingISInfo() {

    if [ -f $IS_SDK_PKGINFO_FILE ]; then
        PKG_FILE=$IS_SDK_PKGINFO_FILE
    else
        PKG_FILE=$IS_PKGINFO_FILE
    fi

    # Get the version number.
    GrabConfig $PKG_FILE "VERSION" "="
    DPRO_VERSION=$ANSWER

    # Get the platform locale information.
    GrabConfig $PKG_FILE "PLATFORM_LOCALE" "="
    PLATFORM_LOCALE=$ANSWER
  
    # Get the user naming attribute.
    GrabConfig $PKG_FILE "USER_NAMING_ATTR" "="
    USER_NAMING_ATTR=$ANSWER

}

################################################################
# Grab the existing Identity information for Linux.
# Uses the AMConfig.properties file.
################################################################

GetExistingISInfoForLinux() {
    if [ "$OSTYPE" = "Linux" ]; then
        # Get the version number.
        GrabConfig $IS_CONFIG_FILE "com.iplanet.am.version" "="
        DPRO_VERSION=$ANSWER

        # Get the platform locale information.
        GrabConfig $IS_CONFIG_FILE "com.iplanet.am.locale" "="
        PLATFORM_LOCALE=$ANSWER

        # Get the user naming attribute.
        GrabConfig $IS_CONFIG_FILE "com.sun.identity.authentication.super.user" "="
        USER_NAMING_ATTR=`$ECHO $ANSWER | $CUT -f1 -d =`
    fi
}

################################################################
# Grab the new IS info from NLP state file
################################################################
GetNewISInfo() {

    # Get the organization DN
    GrabConfig $NLP_STATE_FILE "IDSAME_ORG_DN" "="
    if [ "$ANSWER" != "" ]; then
        ORG_DN=$ANSWER
    else
        $ECHO "Error: Cannot determine IDSAME_ORG_DN. $BELL_CHAR"
        exit 1
    fi

    # Get the Identity server deploy URI
    GrabConfig $NLP_STATE_FILE "IDSAME_AMSERVER" "="
    if [ "$ANSWER" != "" ]; then
        IS_DEPLOY_URI=$ANSWER
    else
        $ECHO "Error: Cannot determine IDSAME_AMSERVER. $BELL_CHAR"
        exit 1
    fi

    # Get the Identity server password encryption key
    GrabConfig $NLP_STATE_FILE "IDSAME_PASSWORD_KEY" "="
    if [ "$ANSWER" != "" ]; then
        IS_PASSWORD_KEY=$ANSWER
    else
        $ECHO "Error: Cannot determine IDSAME_PASSWORD_KEY. $BELL_CHAR"
        exit 1
    fi

    # Create the notification URL.
    NOTIFICATION_URL=$SERVER_PROTOCOL://$SERVER_HOST:$SERVER_PORT/$IS_DEPLOY_URI/notificationservice

}

################################################################
# Initializes some of the required variables
################################################################

InitializeOther() {

    BIN_DIR=$NLP_BASEDIR/$PS_PRODUCT_DIR/bin
    LIB_DIR=$NLP_BASEDIR/$PS_PRODUCT_DIR/lib
    PLATFORM_CONF=$PS_CONFIG_DIR/platform.conf.$NLP_GATEWAY_PROFILE

    CERT_DIR=$PS_CONFIG_DIR/cert/$NLP_GATEWAY_PROFILE
    KEY3_DB_FILE="$CERT_DIR/key3.db"
    CERT7_DB_FILE="$CERT_DIR/cert7.db"
    SECMOD_DB_FILE="$CERT_DIR/secmod.db"
    NICKNAME="server-cert"

    SERVER_URL=$SERVER_PROTOCOL://$SERVER_HOST:$SERVER_PORT
    GW_VIRTUAL_HOST="$NLP_HOST $NLP_IP"
    SSL_CONNECTION="true"
}

################################################################
# Replace the tokens in specified files
################################################################
ReplaceTokens() {

    FILES=$*
    for FILE in $FILES; do
	if [ "$OSTYPE" = "SunOS" ]; then
	  $INSTALLF "SUNWpsnlp" $FILE
	fi
        $CP -p $FILE $FILE-tmp
        $SED -e "s#\${BASE_DIR}#$NLP_BASEDIR#g" \
             -e "s#\${IDSAME_BASE_DIR}#$IS_BASEDIR#g" \
             -e "s#\${PRODUCT_DIR}#$PS_PRODUCT_DIR#g" \
             -e "s#\${JAVA_BASE_DIR}#$JDK_DIR#g" \
             -e "s#SERVER_PROTO#$PS_PROTOCOL#g" \
             -e "s#SERVER_HOST#$PS_HOST#g" \
             -e "s#SERVER_PORT#$PS_PORT#g" \
             -e "s#SERVER_URL#$PS_URL#g" \
             -e "s#GW_HOST#$NLP_HOST#g" \
             -e "s#GATEWAY_IPADDR#$NLP_IP#g" \
             -e "s#GW_PORT#$NLP_PORT#g" \
             -e "s#GW_PROTOCOL#$NLP_PROTOCOL#g" \
             -e "s#GW_VIRTUAL_HOST#$NLP_VIRTUAL_HOST#g" \
             -e "s#DEPLOY_URI#$DEPLOY_URI#g" \
             -e "s#SSL_CONNECTION#$SSL_CONNECTION#g" \
             -e "s#CERT_DB_DIR#$CERT_DIR#g" \
             -e "s#WEBSERVER_HOST#$SERVER_HOST#g" \
        $FILE-tmp > $FILE
        $RM -f $FILE-tmp
    done
    if [ "$OSTYPE" = "SunOS" ]; then
      $INSTALLF -f "SUNWpsnlp"
    fi

}

################################################################
# Write the logging user password to PLATFORM_CONF file
################################################################

CreateLogUserPassword() {

    $MKDIR -p $CERT_DIR
    $CHMOD 755 $CERT_DIR

    #To workaround JDK JNI bug on Linux
    if [ "$OSTYPE" = "Linux" ]; then
        EXTRA_LIBS="$JDK_DIR/jre/lib/i386/native_threads"
    else
        EXTRA_LIBS=""
    fi

    if [ -z "${LD_LIBRARY_PATH}" ]; then
        LD_LIBRARY_PATH="$JSS_NSS_NSPR_LIBPATH:$LIB_DIR/solaris/sparc:$EXTRA_LIBS"
    else
        LD_LIBRARY_PATH="$JSS_NSS_NSPR_LIBPATH:$LIB_DIR/solaris/sparc:$EXTRA_LIBS:${LD_LIBRARY_PATH}"
    fi

    IS_HOME=$IS_BASEDIR/$IDSAME_PRODUCT_DIR
    JCE_CLASSPATH="$IS_HOME/lib/jce1_2_1.jar:$IS_HOME/lib/local_policy.jar:$IS_HOME/lib/US_export_policy.jar:$IS_HOME/lib/sunjce_provider.jar"
    CLASSPATH=$LIB_DIR/certadmin.jar:$JSS_JAR:$JCE_CLASSPATH:$NLP_BASEDIR/$PS_PRODUCT_DIR/locale

    export LD_LIBRARY_PATH
    export CLASSPATH

    $CP $JSS_NSS_NSPR_LIBPATH/libnssckbi.so $CERT_DIR

    $JDK_DIR/bin/java com.sun.portal.cli.cert.CertUtil -option=createloguserpassword "$PLATFORM_CONF" "$CERT_DIR" "${SRA_LOG_USER_PASSWORD}" 

}

################################################################
# Append Netlet proxy informations to PLATFORM_CONF file
################################################################

AppendNLPInfoToPlatformConf() {

    if [ "$NLP_PORT" != "" ]; then
	$ECHO "" >> $PLATFORM_CONF
        $GREP "netletproxy.port=" $PLATFORM_CONF > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            $ECHO "netletproxy.port=$NLP_PORT" >> $PLATFORM_CONF
        fi
    fi

    if [ "$NLP_IP" != "" ]; then
        $GREP "gateway.bindipaddress=" $PLATFORM_CONF > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            $ECHO "gateway.bindipaddress=$NLP_IP" >> $PLATFORM_CONF
        fi
    fi

    $GREP "gateway.sockretries=" $PLATFORM_CONF > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        $ECHO "gateway.sockretries=3" >> $PLATFORM_CONF
    fi

}

################################################################
# Use existing configuration created by RWP
################################################################
UseExistingConfiguration() {

    FILES="$BIN_DIR/netletd $BIN_DIR/certadmin $BIN_DIR/checknetletd /etc/init.d/netletd $BIN_DIR/changepasswd"
    ReplaceTokens $FILES
    AppendNLPInfoToPlatformConf

}

################################################################
# Create new netlet proxy configuration
################################################################

CreateNewConfiguration() {

    $CP $PS_CONFIG_DIR/platform.conf-orig $PLATFORM_CONF
    FILES="$BIN_DIR/netletd $BIN_DIR/certadmin $BIN_DIR/checknetletd $PLATFORM_CONF /etc/init.d/netletd"
    ReplaceTokens $FILES

    AppendNLPInfoToPlatformConf

    if [ "$CREATE_IS_INSTANCE" = "y" ]; then
        CreateISInstance
        $ECHO "portal.server.instance=$NLP_GATEWAY_PROFILE" >> $PLATFORM_CONF
    fi

    if [ "$SELF_SIGNED_CERT" = "y" ]; then
        GenerateCert
    fi

    CreateLogUserPassword

}

################################################################
# Configure Netlet proxy instance
################################################################

ConfigureNLPInstance() {

    if [ -f $PS_CONFIG_DIR/platform.conf.$NLP_GATEWAY_PROFILE ]; then
        UseExistingConfiguration
    else
        CreateNewConfiguration
    fi

}

################################################################
# Start netlet proxy instance if neccessary.
################################################################

StartNetletProxy() {

    NETLETPROXY="$NLP_BASEDIR/$PS_PRODUCT_DIR/bin/netletd"
    if [ "$START_NETLETPROXY" = "y" ]; then
        $NETLETPROXY -n $NLP_GATEWAY_PROFILE start
    fi

}

################################################################
# Create new IS instance
################################################################

CreateISInstance() {

    # Get IS ProductDir and Version
    if [ "$OSTYPE" = "Linux" ]; then
      GetExistingISInfoForLinux
    else
      GetExistingISInfo
    fi

    # Get the new IS info from NLP state file.
    GetNewISInfo

    # Initilize the other required variables.
    TEMP_DIR_PREFIX="/var"
    DEBUG_DIR_PREFIX="$IDSAME_VAR_DIR/debug"
    APPSERVERDEPLOYMENT="false"
    SERVER_HOSTNAME=`$ECHO $SERVER_HOST | $CUT -f1 -d.`

    # Create a new AMConfig-<instance>.properties file
    $CP $IDSAME_CONFIG_DIR/config/AMConfig.properties.template $IDSAME_CONFIG_DIR/config/AMConfig-$NLP_GATEWAY_PROFILE.properties

    # Replace the tokens in newly created AMConfig-<instance>.properties file
    FILES="$IDSAME_CONFIG_DIR/config/AMConfig-$NLP_GATEWAY_PROFILE.properties"
    for FILE in $FILES; do
        $CP -p $FILE $FILE-tmp
        $SED  -e "s#TEMP_DIR_PREFIX#$TEMP_DIR_PREFIX#g" \
              -e "s#DEBUG_DIR_PREFIX#$DEBUG_DIR_PREFIX#g" \
              -e "s#INST_ORGANIZATION#$ORG_DN#g" \
              -e "s#ORG_BASE#$ORG_DN#g" \
              -e "s#SERVER_HOST#$SERVER_HOST#g" \
              -e "s#HOST_NAME#$SERVER_HOSTNAME#g" \
              -e "s#SERVER_PROTO#$SERVER_PROTOCOL#g" \
              -e "s#SERVER_PORT#$SERVER_PORT#g" \
              -e "s#SERVER_DEPLOY_URI#$IS_DEPLOY_URI#g" \
              -e "s#DPRO_VERSION#$DPRO_VERSION#g" \
              -e "s#BASEDIR#$IS_BASEDIR#g" \
              -e "s#PRODUCT_DIR#$IDSAME_PRODUCT_DIR#g" \
              -e "s#APPSERVERDEPLOYMENT#$APPSERVERDEPLOYMENT#g" \
              -e "s#CONSOLE_PROTO#$SERVER_PROTOCOL#g" \
              -e "s#CONSOLE_HOST#$SERVER_HOST#g" \
              -e "s#CONSOLE_PORT#$SERVER_PORT#g" \
              -e "s#PROFILE_HOST#$SERVER_HOST#g" \
              -e "s#PROFILE_PORT#$SERVER_PORT#g" \
              -e "s#PROFILE_URL#$SERVER_URL#g" \
              -e "s#NOTIFICATION_URL#$NOTIFICATION_URL#g" \
              -e "s#CURRENT_PLATFORM_LOCALE#$PLATFORM_LOCALE#g" \
              -e "s#JDK_PATH#$JDK_DIR#g" \
              -e "s#FULLHOSTNAME#$SERVER_HOST#g" \
              -e "s#USER_NAMING_ATTR#$USER_NAMING_ATTR#g" \
              -e "s#AM_ENC_PWD#$IS_PASSWORD_KEY#g" \
        $FILE-tmp > $FILE
        $RM -f $FILE-tmp
    done

    NATIVE2ASCII=/usr/bin/native2ascii
    if [ "$OSTYPE" = "Linux" ]; then
      NATIVE2ASCII=$JDK_DIR/bin/native2ascii
    fi

    $NATIVE2ASCII $IDSAME_CONFIG_DIR/config/AMConfig-$NLP_GATEWAY_PROFILE.properties $IDSAME_CONFIG_DIR/config/AMConfig-$NLP_GATEWAY_PROFILE.properties+
    if [ $? -eq 0 ]; then
        mv $IDSAME_CONFIG_DIR/config/AMConfig-$NLP_GATEWAY_PROFILE.properties+ $IDSAME_CONFIG_DIR/config/AMConfig-$NLP_GATEWAY_PROFILE.properties
    fi

}

################################################################
# Main
################################################################

# Make sure root is executing this script.
CheckUser

# Read the name of gateway profile from command line argument.
NLP_GATEWAY_PROFILE=$1
if [ "$NLP_GATEWAY_PROFILE" = "" ]; then
    GetGatewayProfileName
    CopyStateFile
fi

# Update NLP_STATE_FILE variable to point to correct state file.
UpdateStateFilePointer

# Make sure the NLPConfig state file is present
CheckStateFile

# Read the required informations from NLPConfig state file
GetNLPConfigInfo

# Initialize the other variables
InitializeOther

# Configure Netlet proxy instance
ConfigureNLPInstance

# Start the netlet proxy if needed
StartNetletProxy

if [ "$OSTYPE" = "SunOS" ]; then
  $INSTALLF -f "SUNWpsnlp"
fi

exit 0

# End - Main
