#!/bin/bash -p
#

###########################################################################################
moshelldir=`dirname "$0"`
if [[ $moshelldir != /* ]] ; then moshelldir=`pwd`/$moshelldir ; fi
vobsinstallation=0
unamea=$(uname -a)
gawk="$moshelldir/gawk"
if   [[ $vobsinstallation = 1 && $unamea = [Ll][iI][nN][uU][xX]* ]] ; then gawk="$moshelldir/gawk.linux" 
elif [[ $vobsinstallation = 1 && $unamea = SunOS*sparc*          ]] ; then gawk="$moshelldir/gawk.solaris" 
elif [[ $vobsinstallation = 1 && $unamea = SunOS*                ]] ; then gawk="$moshelldir/gawk.sol86" 
fi
jarclasspath="$moshelldir/commonjars/tracetools/args4j.args4j-2.0.29.jar:$moshelldir/commonjars/tracetools/ch.qos.logback.logback-classic-1.1.3.jar:$moshelldir/commonjars/tracetools/ch.qos.logback.logback-core-1.1.3.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.cliutil-R2D.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.ltetracetools.ltt-R10B.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.ltetracetools.ltt-remote-R10B.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.usage.usage-reporter-R4K.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.usage.usage-shared-R4K.jar:$moshelldir/commonjars/tracetools/com.google.code.gson.gson-2.6.2.jar:$moshelldir/commonjars/tracetools/commons-cli.commons-cli-1.2.jar:$moshelldir/commonjars/tracetools/commons-codec.commons-codec-1.6.jar:$moshelldir/commonjars/tracetools/commons-logging.commons-logging-1.1.3.jar:$moshelldir/commonjars/tracetools/org.apache.httpcomponents.httpclient-4.3.2.jar:$moshelldir/commonjars/tracetools/org.apache.httpcomponents.httpcore-4.3.1.jar:$moshelldir/commonjars/tracetools/org.slf4j.slf4j-api-1.7.12.jar"
if [[ $(uname) = CYG* ]] ; then jarclasspath=`cygpath -wp "$jarclasspath"` ; fi
###########################################################################################

function parse_varfile()
{
$gawk ' { gsub("\r",""); if ($1 ~ /^java=/) java=gensub(/^.*=|#.*$/,"","g",$1) };END{printf java} ' $1
}
#Path to java read from moshell file and/or moshellrc file
tempvar=$(parse_varfile $moshelldir/moshell) ; if [[ -n $tempvar ]] ; then java=$(eval echo $tempvar) ; fi
tempvar=$(parse_varfile $moshelldir/jarxml/moshellrc) ; if [[ -n $tempvar ]] ; then java=$(eval echo $tempvar) ; fi
tempvar=$(parse_varfile $HOME/.moshellrc) ; if [[ -n $tempvar ]] ; then java=$(eval echo $tempvar) ; fi 

javaVersion="$($java -version 2>&1)"
if [[ "$(echo $javaVersion)" = *1.[6-9].* ]] ; then : 
else echo "Java version must be 1.6 or higher." ; echo "Java path can be set in the moshell uservariable \"java\", in the file moshell/jarxml/moshellrc or ~/.moshellrc ." ; exit 1 ; fi

javacommand="$java -Dmoncapture -classpath \"$jarclasspath\" -client com.ericsson.ltt.client.capture.TraceCapture $*"
#echo $javacommand
$java -Dmoncapture -classpath "$jarclasspath" -client com.ericsson.ltt.client.capture.TraceCapture $*
