#!/bin/sh

# Set some required variables. We are dependent on jdk 1.3 being on the path.
JAVA_CMD=java
TESTHARNESS_CLASS=oracle.webdb.testharness.Main
TUNNEL_CLASS=oracle.webdb.testharness.tunnel.Main
VM_TYPE_OPTION=-hotspot
DISPATCHER_OPTION=oracle.webdb.testharness.dispatcher
HTTP_DISPATCHER=oracle.webdb.testharness.client.HttpRequestDispatcher
FILE_DISPATCHER=oracle.webdb.testharness.client.FileRequestDispatcher

# Try to find the PDKTEST_HOME if not set.
if [ -z "$PDKTEST_HOME" ] ; then
    if [ -f "./bin/runTest.sh" ] ; then
        PDKTEST_HOME=`pwd`
    elif [ -f "../bin/runTest.sh" ] ; then
        PDKTEST_HOME=`cd .. ; pwd`
    fi
    if [ -z "$PDKTEST_HOME" ] ; then
        echo The PDKTEST_HOME environment variable is not defined correctly
        echo This environment variable is needed to run the testharness
        exit 1
    fi
fi

# Set the classpath. Start with the request lib dir, the append any jars in jlib.
# Finally append the local classpath.
CP=${PDKTEST_HOME}/rlib
for i in "${PDKTEST_HOME}"/jlib/*.jar
do
    # If the directory is empty, then it will return the input string.
    # This is stupid, so case for it.
    if [ "$i" != "${PDKTEST_HOME}/jlib/*.jar" ] ; then
        CP="$CP":"$i"
    fi
done
if [ -n "$CLASSPATH" ] ; then
    CP="$CP":"$CLASSPATH"
fi

# Decide if we're running the testharness or the recording tunnel
# and take appropriate action
if [ "$1" = "-tunnel" ] ; then
    MAIN_CLASS=${TUNNEL_CLASS}
    DISPATCHER_SYSTEM_PROP=""
    shift
    ARGS="$*"
else
    MAIN_CLASS=${TESTHARNESS_CLASS}
    if [ "$1" = "-debug" ] ; then
        DISPATCHER_SYSTEM_PROP="-D${DISPATCHER_OPTION}=${FILE_DISPATCHER}"
        shift
    else
        DISPATCHER_SYSTEM_PROP="-D${DISPATCHER_OPTION}=${HTTP_DISPATCHER}"
    fi
    ARGS="$*"
fi

# Let user defined JAVA_OPTIONS overide testharness defaults
if [ -z "${JAVA_OPTIONS}" ] ; then
    JAVA_OPTIONS="${VM_TYPE_OPTION} ${DISPATCHER_SYSTEM_PROP}"
fi

# Invoke the JVM
${JAVA_CMD} ${JAVA_OPTIONS} -classpath ${CP} ${MAIN_CLASS} ${ARGS}

