#!/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/tracetools8/args4j.args4j-2.33.jar:$moshelldir/commonjars/tracetools8/ch.qos.logback.logback-classic-1.2.3.jar:$moshelldir/commonjars/tracetools8/ch.qos.logback.logback-core-1.2.3.jar:$moshelldir/commonjars/tracetools8/com.ericsson.testtools.cliutil-R2N.jar:$moshelldir/commonjars/tracetools8/com.ericsson.testtools.ltetracetools.ltt-R13M.jar:$moshelldir/commonjars/tracetools8/com.ericsson.testtools.ltetracetools.ltt-remote-R13M.jar:$moshelldir/commonjars/tracetools8/com.ericsson.testtools.usage.usage-reporter-R5F.jar:$moshelldir/commonjars/tracetools8/commons-cli.commons-cli-1.4.jar:$moshelldir/commonjars/tracetools8/org.mariadb.jdbc.mariadb-java-client-2.7.1.jar:$moshelldir/commonjars/tracetools8/org.slf4j.slf4j-api-1.7.30.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-7].* ]] ; then 
 echo "Java version must be 1.8 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 -Dmonrouter -classpath \"$jarclasspath\" com.ericsson.ltt.router.TraceRouter $*"
#echo $javacommand
$java -Dmonrouter -classpath "$jarclasspath" com.ericsson.ltt.router.TraceRouter $*
