#!/bin/ksh

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

BELL_CHAR='\a'

PS_HOME=${BASE_DIR}/$PS_PRODUCT_DIR
IS_HOME=${IDSAME_BASE_DIR}/$IDSAME_PRODUCT_DIR
IS_CONFIG_FILE="$IDSAME_CONFIG_DIR/config/AMConfig.properties"


GW_STATE_FILE="$PS_CONFIG_DIR/GWConfig.properties"


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


TEXTDOMAIN=gwscripts
TEXTDOMAINDIR=${BASE_DIR}/$PS_PRODUCT_DIR/locale
export TEXTDOMAIN
export TEXTDOMAINDIR
export LANG


################################################################
# 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//"`

}



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

Initialize() {

    LIB_DIR=$PS_HOME/lib
    PLATFORM_CONF=$PS_CONFIG_DIR/platform.conf.$GW_GATEWAY_PROFILE

    CERT_DIR=$PS_CONFIG_DIR/cert/$GW_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"

    # JDK base directory
    GrabConfig "$IS_CONFIG_FILE" "com.iplanet.am.jdk.path" "="
    if [ "$ANSWER" != "" ]; then
        JDK_DIR=$ANSWER
    else
        $ECHO "`$GETTEXT 'Error: Cannot determine JDK_DIR. $BELL_CHAR'`"
        exit 1
    fi

}



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"
    else
        LD_LIBRARY_PATH="$JSS_NSS_NSPR_LIBPATH:$LIB_DIR/solaris/sparc:${LD_LIBRARY_PATH}"
    fi

    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:$PS_HOME/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}"

}


GetPassword() {

    $ECHO "`$GETTEXT 'Enter the Log users password for profile '`" "($GW_GATEWAY_PROFILE) :"
    $STTY -echo
    if [ "$OSTYPE" = "Linux" ]; then
      $READ ANSWER
    else
      $LINE | while $READ -r var; do ANSWER=$var; done
    fi
    stty echo
}

# Main

GW_GATEWAY_PROFILE=$1

if [ -z "$GW_GATEWAY_PROFILE" ]; then
	$ECHO "`$GETTEXT 'Gateway profile not specified'`"
	exit -1
fi

Initialize

if [ ! -f $PLATFORM_CONF ]; then
	$ECHO "`$GETTEXT 'Gateway profile instance does not exist : '`" "$GW_GATEWAY_PROFILE"
	exit -1
fi

# Get the password
GetPassword

SRA_LOG_USER_PASSWORD=$ANSWER

CreateLogUserPassword

