#!/bin/sh
# Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
# PROPRIETARY/CONFIDENTIAL.  Use of this product is subject to license terms.

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

PATH=/bin:/usr/bin:/sbin:/usr/sbin

#
BASE="${BASE_DIR}"

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

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

###############################################################################

###############################################
#
###############################################
GrabConfig() {
    ANSWER=`grep "^$2$3" $1 | uniq | sed -e "s/$2$3//"`
}

###############################################
#
###############################################
ParseArguments() {
    while getopts n: name
    do
        case $name in
            n)  nflag=1
                CONF_NAME="$OPTARG"
                ;;

            ?)  $ECHO "`$GETTEXT 'Usage:'` $0 -n <config-name>"
                exit 2;;
        esac
    done
}

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

nflag=

ParseArguments $*
shift `expr $OPTIND - 1`

if [ -z "$nflag" ]; then
    $ECHO "`$GETTEXT 'Usage:'` $0 -n <config-name>"
    exit 2
fi

PS_HOME=${BASE_DIR}/$PS_PRODUCT_DIR
IS_HOME=${IDSAME_BASE_DIR}/$IDSAME_PRODUCT_DIR
PLATFORM_CONF=$PS_CONFIG_DIR/platform.conf.$CONF_NAME

if [ ! -f $PLATFORM_CONF ]; then
    echo "`$GETTEXT 'certadmin cannot get started. Configuration for specified profile does not exist!`"
    exit 1
fi

GrabConfig $PLATFORM_CONF "gateway.jdk.dir" "="
JAVA_HOME=$ANSWER

CERTADMIN_CLASSPATH="$JSS_JAR:$PS_HOME/lib/certadmin.jar:$PS_HOME/lib/jce1_2_1.jar:$PS_HOME/lib/sunjce_provider.jar"
CERTADMIN_CLASSPATH="$CERTADMIN_CLASSPATH:$PS_HOME/lib/local_policy.jar:$PS_HOME/lib/US_export_policy.jar:$PS_HOME/locale"

# Classpath
if [ -z "$CLASSPATH" ] ; then
    CLASSPATH="$CERTADMIN_CLASSPATH"
else
    CLASSPATH="$CERTADMIN_CLASSPATH:$CLASSPATH"
fi
CLASSPATH=$CLASSPATH:${IDSAME_BASE_DIR}/$IDSAME_PRODUCT_DIR/lib:$IDSAME_CONFIG_DIR/config
export CLASSPATH

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

# LD_LIBRARY_PATH
if [ -z "$LD_LIBRARY_PATH" ] ; then
    LD_LIBRARY_PATH="$JSS_NSS_NSPR_LIBPATH:$PS_HOME/lib/solaris/sparc:$EXTRA_LIBS"
else
    LD_LIBRARY_PATH="$JSS_NSS_NSPR_LIBPATH:$PS_HOME/lib/solaris/sparc:$EXTRA_LIBS:$LD_LIBRARY_PATH"
fi
export LD_LIBRARY_PATH


# Start
CMD="$JAVA_HOME/bin/java -classpath $CLASSPATH com.sun.portal.cli.cert.CertAdmin $CONF_NAME $PS_CONFIG_DIR ${IDSAME_BASE_DIR}/$IDSAME_PRODUCT_DIR/lib"
$CMD
