#!/bin/sh

#
# Confidential property of Sybase, Inc.
# Copyright 1987, 2003
# Sybase, Inc.  All rights reserved.
# Unpublished rights reserved under U.S. copyright laws.
#
# This software contains confidential and trade secret information of Sybase,
# Inc.   Use,  duplication or disclosure of the software and documentation by
# the  U.S.  Government  is  subject  to  restrictions set forth in a license
# agreement  between  the  Government  and  Sybase,  Inc.  or  other  written
# agreement  specifying  the  Government's rights to use the software and any
# applicable FAR provisions, for example, FAR 52.227-19.
# Sybase, Inc. One Sybase Drive, Dublin, CA 94568, USA
#


usage()

{       
	echo "Usage: "
        echo "    ${PROGNAME} -l listenPort -h targethost -p targetPort"
	echo ""
	echo "    listenPort: Port number to listen for SOAP requests on."
	echo "    targetHost: Hostname to forward SOAP requests to."
	echo "    targetPort: Port number to forward SOAP requests to."

	echo ""
	echo "${PROGNAME} execution complete at `date`"
	echo ""

	exit 1
}

PROGNAME=`basename $0`

echo ""
echo "${PROGNAME} initiating execution at `date`."
echo ""

if [ "`echo -n`" = "-n" ]; then
    ECHOOPT=""
    ECHOSUFF="\c"
else
    ECHOOPT="-n"
    ECHOSUFF=""
fi

if [ "$SYBASE" = "" ]
then
    echo "The environment variable SYBASE must be set."
    echo "Please retry after setting the SYBASE environment variable."
    usage 
fi

if [ "$SYBASE_WS" = "" ]
then
    echo "The environment variable SYBASE_WS is not set."
    SYBASE_WS=WS-15_0/
fi

#
#  Find out where Java is. Our order of searching is:
#
#  1. The environment variable SYBASE_JRE6. By default, InstallShield 
#     will populate this environment variable correctly.
#
#  2. The Java installation specified by JAVA_HOME.
#
#
#  If we cannot locate a java executeable, we exit with an error to the
#  end user.
#


if [ "$SYBASE_JRE6" = "" ]
then
    echo "The environment variable SYBASE_JRE6 is not set."
    if [ "$JAVA_HOME" = "" ]
    then
        echo "The environment variable JAVA_HOME is also not set."
        echo "ERROR: Could not find a valid Java installation."
        echo "Please verify ASE installation or use the environment variable \$SYBASE_JRE6  "
        echo "or \$JAVA_HOME to specify a JRE location. "

        exit 1
    else
        echo "Using \$JAVA_HOME to locate Java."
        SYBASE_JRE6=$JAVA_HOME
    fi
fi

echo "Using SYBASE as $SYBASE" 
echo "Using SYBASE_WS as $SYBASE_WS" 
echo "Using SYBASE_JRE6 as $SYBASE_JRE6"

JRE=$SYBASE_JRE6
JAVA=${JRE}/bin/java

#
#  We ensure that the $JAVA exists and is executable.
#

if [ ! -x "$JAVA" ]
then
    echo "ERROR: Could not find java executable at $JAVA. "
    echo "Please verify that a valid JRE installation exists. "
    echo ""
    exit 1
fi

#
# We need to set LIBPATH before accessing JRE
# on AIX
#
if [ "`uname -s`" = "AIX" ]
then
    export LIBPATH=$JRE/bin/classic:$JRE/bin
fi

PROP_FILE=${SYBASE}/${SYBASE_WS}/props/ws.properties

while getopts p:v opt
do
    case $opt in
        p) PROP_FILE="$OPTARG" ;;
		v) VERSION=1 ;;
		*)  usage ;;
    esac
done

SYBASE_WS_LIB=${SYBASE}/${SYBASE_WS}/lib

LISTENPORT=1995
TARGETHOST=nnebbish
TARGETPORT=8186

while getopts l:h:p: opt
do
    case $opt in
	l) LISTENPORT="$OPTARG" ;;
	h) TARGETHOST="$OPTARG" ;;
	p) TARGETPORT="$OPTARG" ;;
	*)  usage ;;
    esac
done

PRCLASSPATH=${SYBASE_WS_LIB}/axis.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/jaxrpc.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/saaj.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/commons-logging.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/commons-discovery.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/log4j-1.2.4.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/wsdl4j.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/xercesImpl.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE_WS_LIB}/xmlParserAPIs.jar
PRCLASSPATH=${PRCLASSPATH}:${SYBASE}/${SYBASE_WS}/props

echo "Starting TCPMon..."

${JAVA} -classpath ${PRCLASSPATH} org.apache.axis.utils.tcpmon ${LISTENPORT} ${TARGETHOST} ${TARGETPORT}

echo ""
echo "${PROGNAME} execution complete at `date`"
echo ""
