#!/bin/bash -p
#

###########################################################################################
moshelldir=`dirname "$0"`
if [[ $moshelldir != /* ]] ; then moshelldir=`pwd`/$moshelldir ; fi
vobsinstallation=0
unamea=$(uname -a)
gawkext=""
gawklib=""
if [[ $unamea = [Ll][iI][nN][uU][xX]*x86_64* ]] ; then gawklib="lin64"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".lin64" ; fi
elif [[ $unamea = [Ll][iI][nN][uU][xX]* ]]      ; then gawklib="linux"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".linux" ; fi
elif [[ $unamea = SunOS*sparc* ]]               ; then gawklib="solaris" ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".solaris" ; fi
elif [[ $unamea = SunOS* ]]                     ; then gawklib="sol86"   ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".sol86" ; fi
elif [[ $unamea = Darwin* ]]                    ; then gawklib="app"     ; if [[ $vobsinstallation = 1 ]] ; then gawkext=".app"   ; fi
else gawklib="cygwin" ; gawkext=".exe"
fi
gawkprog="gawk${gawkext}" 
gawk="$moshelldir/$gawkprog"

#special case where moshell has been installed on a linux 32 bit machine but should be run on linux 64 sharing the same file system
if [[ $unamea = [Ll][iI][nN][uU][xX]*x86_64* && $vobsinstallation != 1 && `file "$moshelldir/gawk"` = *32-bit* ]] ; then gawklib="linux" ; fi

filefunc="$moshelldir/commonjars/lib/${gawklib}/filefuncs"
export LD_LIBRARY_PATH=${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}$moshelldir/commonjars/lib/${gawklib}
export LANG=C
export LC_ALL=C

jarclasspath="$moshelldir/commonjars/tracetools/args4j.args4j-2.37.jar:$moshelldir/commonjars/tracetools/ch.qos.logback.logback-classic-1.2.11.jar:$moshelldir/commonjars/tracetools/ch.qos.logback.logback-core-1.2.11.jar:$moshelldir/commonjars/tracetools/com.ericsson.lcda.usage-2-1.0.2.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.cliutil-R3A.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.ltetracetools.ltt-14.4.5.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.ltetracetools.ltt-remote-14.4.5.jar:$moshelldir/commonjars/tracetools/com.fasterxml.jackson.core.jackson-annotations-2.16.0.jar:$moshelldir/commonjars/tracetools/com.fasterxml.jackson.core.jackson-core-2.16.0.jar:$moshelldir/commonjars/tracetools/com.fasterxml.jackson.core.jackson-databind-2.16.0.jar:$moshelldir/commonjars/tracetools/com.fasterxml.jackson.datatype.jackson-datatype-jsr310-2.16.0.jar:$moshelldir/commonjars/tracetools/org.codehaus.janino.commons-compiler-3.1.12.jar:$moshelldir/commonjars/tracetools/org.codehaus.janino.janino-3.1.12.jar:$moshelldir/commonjars/tracetools/org.slf4j.slf4j-api-1.7.36.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 2>/dev/null
}
#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.[1-8].* ]] ; then 
 echo "Java version must be 11 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 $*
