#!/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.33.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.testtools.cliutil-R2U.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.ltetracetools.ltt-14.3.1.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.ltetracetools.ltt-remote-14.3.1.jar:$moshelldir/commonjars/tracetools/com.ericsson.testtools.usage.usage-reporter-R5J.jar:$moshelldir/commonjars/tracetools/org.mariadb.jdbc.mariadb-java-client-2.7.5.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 -Dmonviewer -classpath \"$jarclasspath\" -client com.ericsson.ltt.client.viewer.TraceViewer $*"
#echo $javacommand
$java -Dmonviewer -classpath "$jarclasspath" -client com.ericsson.ltt.client.viewer.TraceViewer $*
