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

GrabConfig "/var/sadm/pkg/SUNWjss/pkginfo" "BASEDIR" "="
JSS_BASEDIR=$ANSWER

#
# LD_LIBRARY_PATH
#

DIR="$JSS_BASEDIR$JSS_NSS_NSPR_LIB_DIR"
if [ -z "$LD_LIBRARY_PATH" ]; then
  LD_LIBRARY_PATH="$DIR"
else
  LD_LIBRARY_PATH="$DIR:$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"
LOG_JAR="$IDSAME_LIB_DIR/am_logging.jar"
MAIL_JAR="$IDSAME_LIB_DIR/mail.jar"
JAF_JAR="$IDSAME_LIB_DIR/activation.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:$SDK_JARS:$JSS_JAR:$SERVLET_JAR:$JAAS_JAR:$MAIL_JAR:$JAF_JAR:$LOG_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"
SUBS_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/subscriptions.jar"
SRCH_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/searchserver.jar"
SRCH_PROVIDER_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/searchproviders.jar"
SRCH_SDK_JAR="$PS_BASEDIR/$PS_PRODUCT_DIR/web-src/WEB-INF/lib/searchsdk.jar"
KT_SRCH_JAR="$KTSEARCH_LIB_DIR/ktsearch.jar"
PS_CLASSPATH="$PS_LIB_DIR:$PS_WEBINF_DIR:$RW_JAR:$DT_JAR:$SUBS_JAR:$SRCH_JAR:$KT_SRCH_JAR:$SRCH_PROVIDER_JAR:$SRCH_SDK_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 Autoclassify CLI
#

PS_DIR="$PS_BASEDIR/$PS_PRODUCT_DIR"

JARS="$PS_DIR/lib/searchserver.jar:$PS_DIR/lib/searchadmin.jar:$PS_DIR/web-src/WEB-INF/lib/searchsdk.jar:/usr/share/lib/ktsearch.jar:$PS_DIR/lib/db.jar"
CP="$PS_DIR/web-src/WEB-INF/classes:$PS_DIR/lib:$JARS:$CLASSPATH"

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

export LD_LIBRARY_PATH
$JAVA_HOME/bin/java -D"java.protocol.handler.pkgs=com.iplanet.services.comm" -cp $CP com.sun.portal.search.autoclassify.Autoclassify "$@"

exit $?
