#!/bin/ksh

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

BELL_CHAR='\a'

RWP_STATE_FILE="$PS_CONFIG_DIR/RWPConfig.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/jss3.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:$RWP_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=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 RWPConfig state file in not present
################################################################

CheckStateFile() {

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

}

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

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

}

################################################################
# Copy the RWPConfig state file to RWPConfig-<profilename>
################################################################
CopyStateFile() {

    NEW_RWP_STATE_FILE="$PS_CONFIG_DIR/RWPConfig-$RWP_GATEWAY_PROFILE.properties"
    $CP $RWP_STATE_FILE $NEW_RWP_STATE_FILE

}

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

UpdateStateFilePointer() {
	
    NEW_RWP_STATE_FILE="$PS_CONFIG_DIR/RWPConfig-$RWP_GATEWAY_PROFILE.properties"
    RWP_STATE_FILE=$NEW_RWP_STATE_FILE

}

################################################################
# Read all the configuration informations
################################################################
GetRWPConfigInfo() {
    
    # 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

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

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

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

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

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

    # Create self signed certficate ?
    GrabConfig $RWP_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 $RWP_STATE_FILE "CERT_INFO" "="
        if [ "$ANSWER" != "" ]; then
            CERT_INFO=$ANSWER
        else
            $ECHO "Error: Cannot determine CERT_INFO. $BELL_CHAR"
            exit 1
        fi
    fi

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

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

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

    # Identity server port
    GrabConfig $RWP_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 $RWP_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 $RWP_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 $RWP_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 RWP state file
################################################################
GetNewISInfo() {

    # Get the organization DN
    GrabConfig $RWP_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 $RWP_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 $RWP_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=$RWP_BASEDIR/$PS_PRODUCT_DIR/bin
    LIB_DIR=$RWP_BASEDIR/$PS_PRODUCT_DIR/lib
    PLATFORM_CONF=$PS_CONFIG_DIR/platform.conf.$RWP_GATEWAY_PROFILE

    CERT_DIR=$PS_CONFIG_DIR/cert/$RWP_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="$RWP_HOST $RWP_IP"
    SSL_CONNECTION="true"
}

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

    FILES=$*
    for FILE in $FILES; do
	if [ "$OSTYPE" = "SunOS" ]; then
	  $INSTALLF "SUNWpsrwp" $FILE
	fi
        $CP -p $FILE $FILE-tmp
        $SED -e "s#\${BASE_DIR}#$RWP_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#$RWP_HOST#g" \
             -e "s#GATEWAY_IPADDR#$RWP_IP#g" \
             -e "s#GW_PORT#$RWP_PORT#g" \
             -e "s#GW_PROTOCOL#$RWP_PROTOCOL#g" \
             -e "s#GW_VIRTUAL_HOST#$RWP_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 "SUNWpsrwp"
    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:$RWP_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 Rewriter proxy informations to PLATFORM_CONF file
################################################################

AppendRWPInfoToPlatformConf() {

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

    if [ "$RWP_IP" != "" ]; then
        $GREP "gateway.bindipaddress=" $PLATFORM_CONF > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            $ECHO "gateway.bindipaddress=$RWP_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/rwproxyd $BIN_DIR/certadmin $BIN_DIR/checkrp /etc/init.d/rwproxyd $BIN_DIR/chroot/mkchroot"
    ReplaceTokens $FILES
    AppendRWPInfoToPlatformConf

}

################################################################
# Create new rewriter proxy configuration
################################################################

CreateNewConfiguration() {

    $CP $PS_CONFIG_DIR/platform.conf-orig $PLATFORM_CONF
    FILES="$BIN_DIR/rwproxyd $BIN_DIR/certadmin $BIN_DIR/checkrp $PLATFORM_CONF /etc/init.d/rwproxyd $BIN_DIR/chroot/mkchroot $BIN_DIR/changepasswd"
    ReplaceTokens $FILES

    AppendRWPInfoToPlatformConf

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

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

    CreateLogUserPassword

}

################################################################
# Configure Rewriter proxy instance
################################################################

ConfigureRWPInstance() {

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

}

################################################################
# Start rewriter proxy instance if neccessary.
################################################################

StartRewriterProxy() {

    REWRITERPROXY="$RWP_BASEDIR/$PS_PRODUCT_DIR/bin/rwproxyd"
    if [ "$START_REWRITERPROXY" = "y" ]; then
        $REWRITERPROXY -n $RWP_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 RWP 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-$RWP_GATEWAY_PROFILE.properties

    # Replace the tokens in newly created AMConfig-<instance>.properties file
    FILES="$IDSAME_CONFIG_DIR/config/AMConfig-$RWP_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-$RWP_GATEWAY_PROFILE.properties $IDSAME_CONFIG_DIR/config/AMConfig-$RWP_GATEWAY_PROFILE.properties+
    if [ $? -eq 0 ]; then
        mv $IDSAME_CONFIG_DIR/config/AMConfig-$RWP_GATEWAY_PROFILE.properties+ $IDSAME_CONFIG_DIR/config/AMConfig-$RWP_GATEWAY_PROFILE.properties
    fi

}

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

# Make sure root is executing this script.
CheckUser

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

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

# Make sure the RWPConfig state file is present
CheckStateFile

# Read the required informations from RWPConfig state file
GetRWPConfigInfo

# Initialize the other variables
InitializeOther

# Configure Rewriter proxy instance
ConfigureRWPInstance

# Start the rewriter proxy if needed
StartRewriterProxy

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

exit 0

# End - Main
