#!/bin/ksh
#
# Copyright 2001 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 "DEPLOY_TYPE" "="
DEPLOY_TYPE=$ANSWER

GrabConfig $STATE_FILE "DEPLOY_DIR" "="
DEPLOY_DIR=$ANSWER

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

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

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

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

#
# 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

DIR="$JSS_BASEDIR$JSS_NSS_NSPR_LIB_DIR"
if [ -z "$LD_LIBRARY_PATH" ]; then
  LD_LIBRARY_PATH="$DIR:$EXTRA_LIBS"
else
  LD_LIBRARY_PATH="$DIR:$EXTRA_LIBS:$LD_LIBRARY_PATH"
fi
export LD_LIBRARY_PATH

#
# IS
#

IDSAME_LOCALE_DIR="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/locale"
IDSAME_LIB_DIR="$IDSAME_BASEDIR/$IDSAME_PRODUCT_DIR/lib"
IDSAME_JAR_DIR="$DEPLOY_DIR/bin/https/jar"
SERVLET_JAR="$IDSAME_LIB_DIR/servlet.jar"
JSS_JAR="$JSS_BASEDIR$JSS_JAR_LIB_DIR/jss3.jar"
MAIL_JAR="$IDSAME_LIB_DIR/mail.jar"
JAAS_JAR="$IDSAME_LIB_DIR/jaas.jar"
SDK_JARS="$IDSAME_LIB_DIR/am_sdk.jar:$IDSAME_LIB_DIR/am_services.jar"
IDSAME_CLASSPATH="$IDSAME_LOCALE_DIR:$IDSAME_LIB_DIR:$IDSAME_CONFIG_DIR/config:$SDK_JARS:$JSS_JAR:$SERVLET_JAR:$JAAS_JAR:$MAIL_JAR"

#
# JAXP
#
JAXP_JARS="$IDSAME_LIB_DIR/jaxp-api.jar:$IDSAME_LIB_DIR/xercesImpl.jar:$IDSAME_LIB_DIR/xalan.jar:$IDSAME_LIB_DIR/dom.jar:$IDSAME_LIB_DIR/sax.jar"

#
# PS
#

PS_LIB_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/lib"
PS_WEBINF_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/classes"
RW_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/rewriter.jar"
DT_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/desktop.jar"
PS_CLASSPATH="$PS_LIB_DIR:$PS_WEBINF_DIR:$RW_JAR:$DT_JAR"

#
# CLASSPATH
#

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

#
# Extract java system properties
#

set -A JAVA_SYSTEM_PROPERTIES
set -A ARGS
for OPT in "$@"; do
  DOPT=${OPT#-D}
  if [ ${#DOPT} -gt 0 ] && [ "$DOPT" != "$OPT" ]; then
    JAVA_SYSTEM_PROPERTIES[${#JAVA_SYSTEM_PROPERTIES[*]}]="$OPT"
  else
    ARGS[${#ARGS[*]}]="$OPT"
  fi
done

#
# Call PS DP CLI
#

if [ "$DEPLOY_TYPE" = "SUNONE" ]; then
  $JAVA_HOME/bin/java -Xms64m -Xmx256m -Xbootclasspath/p:$JAXP_JARS -classpath $CLASSPATH -D"com.iplanet.coreservices.configpath=$IDSAME_CONFIG_DIR/config/ums" -D"desktop.propertiesFile=$PS_CONFIG_DIR/desktop/desktopconfig.properties" "${JAVA_SYSTEM_PROPERTIES[@]}" com.sun.portal.desktop.dp.cli.DPAMain "${ARGS[@]}"
else
  $JAVA_HOME/bin/java -Xms64m -Xmx256m -classpath $CLASSPATH -D"com.iplanet.coreservices.configpath=$IDSAME_CONFIG_DIR/ums" -D"desktop.propertiesFile=$PS_CONFIG_DIR/desktop/desktopconfig.properties" "${JAVA_SYSTEM_PROPERTIES[@]}" com.sun.portal.desktop.dp.cli.DPAMain "${ARGS[@]}"
fi
exit $?
