#!/bin/bash -p
#parseline, to parse lines from te log read
#appends translations for:
#  - CXC names
#  - Aal2 Reject codes (in trace9:Ans_aal2ncc2_proc
#  - MTP3/SCCP codes 

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
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

globmoshellrc=$moshelldir/jarxml/moshellrc
commonjardir=$moshelldir/commonjars
jarxmldir=$moshelldir/jarxml
defLogDir=$commonjardir/defLogDir.sh
moshellrc=$($defLogDir)


$gawk -f $moshelldir/funcs.awk -f $moshelldir/extern.awk -v moshelldir="$moshelldir" -v vobsinstallation="$vobsinstallation" -v globmoshellrc="$globmoshellrc" -v moshellrc="$moshellrc" -v jarxmldir="$jarxmldir" -l "$filefunc" --source '
BEGIN{
	show_colors=1
	loadFilefuncs()
	find_jarxmldir(moshelldir"/moshell")
	find_jarxmldir(globmoshellrc)
	find_jarxmldir(moshellrc)
	print_lmid=1
	lmiddb=sprintf("%s/lmids.txt",jarxmldir)
	upiddb=sprintf("%s/commonjars/upids.txt",moshelldir)
	uptomomdb=sprintf("%s/uptomom.txt",jarxmldir)
	updatadb=sprintf("%s/updata.txt",jarxmldir)
	updatadb2=sprintf("%s/updata2.txt",jarxmldir)
	cppdatadb=sprintf("%s/cppdata.txt",jarxmldir)
	define_codes()
}
{ 
	#If the printout is coming from flow.awk, there is one field less (the date), we add this field 
	#eg: 14:57:25.520] 001400/Ans_aal2ncc2_proc netconn.c:553 TRACE9:aal2NciRejCause = 1f, rejectType = 2
	if ($1 ~ /\]$/ && $2 !~ /\]$/) $0="[ "$0
	print appendCXCtranslation($0) 
}'
