#!/bin/sh

# $Id: $
# $Revision: $

#set -x

case $0 in
    /*)
        LAUNCH_HOME=`dirname $0`/..
        ;;
    *)
        LAUNCH_HOME=`pwd`/`dirname $0`/..
        ;;
esac

cd $LAUNCH_HOME
#echo "Debug: LAUNCH_HOME: $LAUNCH_HOME"

# Cygwin support in this is not complete
case $(uname -s) in
    CYGWIN*) 
        CYGWIN=TRUE 
        ;;
    *) 
        ;;
esac

#echo "Debug: LIB_ROOT: $LIB_ROOT"
if [ ! -d $LIB_ROOT ] ; then
    echo "$LIB_ROOT not found.  Did you forget to deploy?"
    exit 1
fi

# Use a symlink from 'lib/native/<hostname>' to the appropriate lib directory for that host
# If the symlink doesn't exist, create it.
# This will save java from having to be called to determine the lib path every time the cli is executed, which saves some execution time.
# Of course, this assumes you have create/write permissions on the lib/native dir.
NATIVE=`hostname`
if [ ! -d $LIB_ROOT/../native/$NATIVE ] ; then

    NATIVE_RELATIVE=`java -cp lib/server/nn-platform.jar:lib/server/nn-core.jar:lib/server/guava.jar com.netapp.common.platform.OperatingSystem -libpath`
    if [ -n "$CYGWIN" ] ; then
        # System.out.println() on Windows does CRLF, backtick in
        # cygwin only strips the LF. Go-go-gadget hack.
        NATIVE_RELATIVE=$(cygpath -u $(echo $NATIVE | d2u -U))
    fi
    ln -s $NATIVE_RELATIVE $LIB_ROOT/../native/$NATIVE &> /dev/null
    
    if [ ! -d $LIB_ROOT/../native/$NATIVE ] ; then
       # if the link still doesn't exist, then don't use a link 
       NATIVE=$NATIVE_RELATIVE
    fi    
    
fi
#echo "Debug: NATIVE: $NATIVE"
#echo "Debug: NATIVE_RELATIVE: $NATIVE_RELATIVE"

PLATFORM=`echo $NATIVE | sed -e 's#\(.*\)/.*#\1#'`

JVM_ARGS="$JVM_ARGS -XX:+HeapDumpOnOutOfMemoryError -XX:MaxPermSize=128m -Djava.net.preferIPv4Stack=true -enableassertions"

#echo "Debug: JVM_ARGS: $JVM_ARGS"

if [ -n "$DEBUG_PORT" ] ; then
    JVM_DEBUG_ARGS="-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=$DEBUG_PORT,timeout=20000"
fi
#echo "Debug: JVM_DEBUG_ARGS: $JVM_DEBUG_ARGS"

CLASSPATH="$CLASSPATH:$LAUNCH_HOME/etc"

#echo "Debug: CLASSPATH: $CLASSPATH"
    
for file in `find $LIB_ROOT -follow -name "*.jar"`; do
    CLASSPATH="${CLASSPATH}:$file"
done

#echo "Debug: CLASSPATH: $CLASSPATH"
# If an instrumented version of COORDINATOR is deployed, then the
# instrumentation metadata is and will be stored in this file.
# Otherwise, this argument will just be ignored
JVM_ARGS="$JVM_ARGS -Dnet.sourceforge.cobertura.datafile=$LIB_ROOT/cobertura.ser"

if [ "$PLATFORM" = "windows" ] ; then
    CLASSPATH=`cygpath -wp $CLASSPATH`
    NATIVE_LIB_PATH="$(cygpath -wp $LIB_ROOT/../native/$NATIVE)"
else
    NATIVE_LIB_PATH="$LIB_ROOT/../native/$NATIVE"
fi

JVM_ARGS="$JVM_ARGS -Djava.library.path=$NATIVE_LIB_PATH -Djna.library.path=$NATIVE_LIB_PATH"
JVM_ARGS="$JVM_ARGS -Dtool.install.dir=$LAUNCH_HOME"

# Define hostname and pid as System Properties for reference in logback pattern layout
JVM_ARGS="$JVM_ARGS -Dpid=$$"
#echo "CLASSPATH::::::::"
#echo $CLASSPATH
export CLASSPATH

#echo "Debug: JVM_ARGS: $JVM_ARGS"
#echo "JAVA COMMAND:::::::::::::::::::::::::::"
#echo java ${JVM_ARGS} ${JVM_DEBUG_ARGS} ${1+"$@"}
exec java ${JVM_ARGS} ${JVM_DEBUG_ARGS} ${1+"$@"}
#/usr/bin/time -v java ${JVM_ARGS} ${JVM_DEBUG_ARGS} ${1+"$@"}
