#!/bin/ksh
#
# 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
###############################################

###############################################
# Get configuration from file
###############################################
GrabConfig() {
  local FILE=$1
  local KEY=$2
  local SEPARATOR=$3

  ANSWER=`$GREP "^$KEY$SEPARATOR" $FILE | $UNIQ | $SED -e "s/$KEY$SEPARATOR//"`
}

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

STATE_FILE="$PS_CONFIG_DIR/PSConfig.properties"
if [ ! -f $STATE_FILE ]; then
  $ECHO "Cannot find $STATE_FILE."
  exit 1
fi

GrabConfig $STATE_FILE "JDK_DIR" "="
JAVA_HOME=$ANSWER

GrabConfig $STATE_FILE "BASEDIR" "="
PS_BASEDIR=$ANSWER

GrabConfig $STATE_FILE "IDSAME_BASEDIR" "="
IS_BASEDIR=$ANSWER

if [ "$OSTYPE" = "SunOS" ]; then
  GrabConfig "/var/sadm/pkg/SUNWjss/pkginfo" "BASEDIR" "="
  JSS_BASEDIR=$ANSWER
else
  JSS_BASEDIR=""
fi

#
# PS CLASSPATH
#

PS_LIB_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/lib"
PS_WEBINF_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/classes"
CONTAINER_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/container.jar"
DESKTOP_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/desktop.jar"
REWRITER_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/rewriter.jar"
PS_CLASSPATH="$PS_WEBINF_DIR:$PS_LIB_DIR:$CONTAINER_JAR:$DESKTOP_JAR:$REWRITER_JAR"

JAXB_CLASSPATH="$PS_LIB_DIR/relaxngDatatype.jar:$PS_LIB_DIR/namespace.jar:$PS_LIB_DIR/jax-qname.jar:$PS_LIB_DIR/xsdlib.jar:$PS_LIB_DIR/jaxb-api.jar:$PS_LIB_DIR/jaxb-impl.jar:$PS_LIB_DIR/jaxb-libs.jar"

JAXRPC_CLASSPATH="$PS_LIB_DIR/jaxrpc-api.jar:$PS_LIB_DIR/jaxrpc-impl.jar:$PS_LIB_DIR/jax-qname.jar:$PS_LIB_DIR/saaj-api.jar:$PS_LIB_DIR/saaj-impl.jar:$PS_LIB_DIR/commons-logging.jar:$PS_LIB_DIR/jaxrpc-spi.jar"

#
# server (JAX-RPC + JAXB) classpath
#

SERVER_CLASSPATH="$JAXB_CLASSPATH:$JAXRPC_CLASSPATH"

#
# IS classpath
#
#

IS_LIB_DIR="$IS_BASEDIR/$IDSAME_PRODUCT_DIR/lib"
IS_LOCALE_DIR="$IS_BASEDIR/$IDSAME_PRODUCT_DIR/locale"
SERVLET_JAR="$IS_LIB_DIR/servlet.jar"
JSS_JAR="$JSS_BASEDIR$JSS_CERT7_JAR_LIB_DIR/jss3.jar"
JAAS_JAR="$IS_LIB_DIR/jaas.jar"
IS_CLASSPATH="$IS_LIB_DIR:$IS_LOCALE_DIR:$IDSAME_CONFIG_DIR/config:$IS_LIB_DIR/am_sdk.jar:$IS_LIB_DIR/am_services.jar:$IS_LIB_DIR/am_logging.jar:$SERVLET_JAR:$JSS_JAR:$JAAS_JAR:$IS_LIB_DIR/mail.jar:$IS_LIB_DIR/activation.jar"

#
# WSRP CLASSPATH
#

WSRP_CLASSPATH="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/wsrp-common.jar:$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/wsrp-consumer.jar"

#
# LD_LIBRARY_PATH
#

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

DIRS="$IS_LIB_DIR/solaris/sparc:$JSS_BASEDIR$JSS_NSS_NSPR_CERT7_LIB_DIR:$EXTRA_LIBS"

if [ -z "$LD_LIBRARY_PATH" ]; then
  LD_LIBRARY_PATH="$DIRS"
else
  LD_LIBRARY_PATH="$DIRS:$LD_LIBRARY_PATH"
fi
export LD_LIBRARY_PATH

#
# CLASSPATH
#

if [ -z "$CLASSPATH" ]; then
  CLASSPATH="$SERVER_CLASSPATH:$IS_CLASSPATH:$PS_CLASSPATH:$WSRP_CLASSPATH:$JAVA_HOME/lib/lib.zip"
else
  CLASSPATH="$SERVER_CLASSPATH:$IS_CLASSPATH:$PS_CLASSPATH:$WSRP_CLASSPATH:$JAVA_HOME/lib/lib.zip:$CLASSPATH"
fi

#
# Call
#

$JAVA_HOME/bin/java -classpath $CLASSPATH -D"wsrp.consumer.propertiesFile=$PS_CONFIG_DIR/wsrp/wsrpconsumerconfig.properties" -D"desktop.propertiesFile=$PS_CONFIG_DIR/desktop/desktopconfig.properties" -D"java.endorsed.dirs=$IS_LIB_DIR/endorsed" com.sun.portal.wsrp.consumer.cli.PEAMain "$@"

exit $?
