#!/bin/sh
BINDIR=`dirname $0`

if [ -n "$BINDIR" ]
then
    FABAN_HOME=`cd $BINDIR/.. > /dev/null 2>&1 && pwd`
    export FABAN_HOME
fi

JAVA=""

# If JAVA_HOME is not defined, we try our best to locate it.
if [ -z "$JAVA_HOME" ] ; then

    # Check that Java is reachable at all.
    JAVA_BIN=`which java`
    if [ -z "$JAVA_BIN" ] ; then
        echo "Could not find java. Please make sure the JDK is installed \
and set JAVA_HOME or PATH accordingly." >&2
        exit 1
    fi
    JAVA=$JAVA_BIN
else
    JAVA=$JAVA_HOME/bin/java
fi

# Check whether the java -client option is supported on this JVM. If so, use
# the client JVM.  It is much lighter weight, less threads, less memory
# and we do not need much performance for the agent.
$JAVA -client -version >/dev/null 2>&1
if [ "$?" = "0" ] ; then
    JAVA="$JAVA -client"
fi

CLASSPATH=$FABAN_HOME/lib/fabanagents.jar:$FABAN_HOME/lib/fabancommon.jar
export CLASSPATH

$JAVA -Xmx2m -cp "$CLASSPATH" \
      com.sun.faban.harness.tools.MemcacheStats $* &
pid=$!

#  For some reason killing this script doesn't kill java process
# So we manually kill it and exit
trap "kill $pid" 2 3 15
wait
exit 0
