#!/bin/bash -p
# swcomp v1.3 2006-09-06 , Finn Magnusson, finn.magnusson@ericsson.com
# Type "swcomp" for help.

###########################################################################################
# Pathes to files:
###########################################################################################
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

moshell="$moshelldir/moshell"
funcs="$moshelldir/funcs.awk"
defLogDir="$moshelldir/commonjars/defLogDir.sh"
moshellrc=$($defLogDir)
globmoshellrc=$moshelldir/jarxml/moshellrc
logdir=${moshelldir}/logfiles/logs_cabview
temp_logdir=$($defLogDir cabview)
if [[ -w $temp_logdir && -d $temp_logdir ]] ; then logdir=$temp_logdir ; fi
###########################################################################################

function print_usage()
{
cat <<EOF
Usage:  
    swcomp <IpAddress1> <Password1> <IpAddress2> <Password2>
or: swcomp <Ipaddress1> <Password1> <CabsLog2>
or: swcomp <CabsLog1> <CabsLog2>
	
Examples: 
 - swcomp 10.1.0.17 rnc1 10.2.0.17 rnc2
 - swcomp 10.1.0.17 rnc2 ~/moshell/logfiles/logs_cabview/rnc2.log
 - swcomp ~/moshell/logfiles/logs_cabview/rnc1.log ~/moshell/logfiles/logs_cabview/rnc2.log
 
Cabs logfile can be taken:
  - with moshell command "cabs" (do l+ before and l- after to open/close logfile)
  - or with unix command: "cabview -s ipaddress password > logfile"
	
EOF
}

function get_logs()
{
$gawk -v moshelldir="$moshelldir" -v moshell="$moshell" -v moshellrc="$moshellrc" -v gawk="$gawk" -v ipaddress1=$1 -v password1=$2 -v logfile1=$3 -v ipaddress2=$4 -v password2=$5 -v logfile2=$6 -v vobsinstallation="$vobsinstallation" -v globmoshellrc="$globmoshellrc" -f "$funcs" -l "$filefunc" --source '
BEGIN{
	loadFilefuncs()
	parse_user_variables(moshell)	
	parse_user_variables(globmoshellrc)		
	parse_user_variables(moshellrc)
	parse_common_variables()
	printf("\nFetching SW information....\n")
	cabview_session=sprintf("%s/moshell -v password=%s %s \x27 cabs \x27 %s >/dev/null",moshelldir,password1,ipaddress1,logfile1)
	print "Running "cabview_session
	system(cabview_session)
	logfile["node1"]=logfile1
	ipaddress["node1"]=ipaddress1
	if (ipaddress2 != "")
	{
		cabview_session=sprintf("%s/moshell -v password=%s %s \x27 cabs \x27 %s >/dev/null",moshelldir,password2,ipaddress2,logfile2)	
		print "Running "cabview_session
		system(cabview_session)
		logfile["node2"]=logfile2
		ipaddress["node2"]=ipaddress2
	}
	for (site in logfile)
	{
		while (getline line < logfile[site])
		{
			if (line ~ /^Unable to connect |Wrong Password/) exit 1
		}
		close(logfile[site])
	}
}
'
return $?
}

function swcomp()
{
$gawk -v log1=$1 -v log2=$2 '
BEGIN{
	print "\nNODE1: "log1
	print "NODE2: "log2
	read_log(log1,swlist1,boardlist1,swname1)
	read_log(log2,swlist2,boardlist2,swname2)
	compare(swlist1,swlist2,boardlist1,boardlist2,swname1,swname2)
}
function read_log(logfile,swlist,boardlist,swname)
{
	while (getline < logfile)
	{
		if ($5 ~ /^CXC/)
		{
			swid=gensub(/_.*$/,"","g",$5)
			swrev=gensub(/^.*_/,"","g",$5)
			swlist[$1,$2,swid]=swrev
			boardlist[$1,$2]=$3
			swname[swid]=$6
		}
	}
	close(logfile)	
}
function compare(swlist1,swlist2,boardlist1,boardlist2,swname1,swname2,comb)
{
 	for (comb in swlist1)
	{
		if (swlist1[comb] != swlist2[comb] && swlist2[comb] != "")
		{
			split(comb,array,SUBSEP)
			mismatch[comb]=sprintf("%-9s  %-9s  %-15s  %-10s  %-10s  %s",boardlist1[array[1],array[2]],boardlist2[array[1],array[2]],array[3],swlist1[comb],swlist2[comb],swname1[array[3]])
			mismatchboards[comb]=sprintf("%s,%s",array[1],array[2])
			#print array[1]" "array[2]" "array[3]" "mismatch[array[1],array[2]]
		}
	}
	for (comb in swlist1)
	{
		if (swlist2[comb] == "")
		{
			split(comb,array,SUBSEP)
			only1[comb]=sprintf("%-9s  %-15s  %-10s  %s",boardlist1[array[1],array[2]],array[3],swlist1[comb],swname1[array[3]])
			only1boards[comb]=sprintf("%s,%s",array[1],array[2])
		}
	}
	for (comb in swlist2)
	{
		if (swlist1[comb] == "") 
		{
			split(comb,array,SUBSEP)
			only2[comb]=sprintf("%-9s  %-15s  %-10s  %s",boardlist2[array[1],array[2]],array[3],swlist2[comb],swname2[array[3]])
			only2boards[comb]=sprintf("%s,%s",array[1],array[2])
		}		
	}
	printf("\n\
SOFTWARE FOUND IN BOTH NODES BUT WITH DIFFERENT REVISIONS\n\
============================================================================================\n\
%3s  %3s  %-9s  %-9s  %-15s  %-10s  %-10s  %s\n\
============================================================================================\n","SMN","APN",\
"BOARD1","BOARD2","SW ID","SW REV 1","SW REV 2","SW NAME")
	for (i=0;i<=99;i++)
	{
		for (j=0;j<=27;j++)
		{
			print_line=0
			for (comb in mismatch)
			{
				if (mismatchboards[comb]==sprintf("%s,%s",i,j))
				{
					printf("%3s  %3s  %s\n",i,j,mismatch[comb])
					print_line=1
				}
			}
			if (print_line==1) printf("--------------------------------------------------------------------------------------------\n")
		}
	}
	printf("\n\
SOFTWARE FOUND ONLY IN NODE 1\n\
============================================================================================\n\
%3s  %3s  %-9s  %-15s  %-10s  %s\n\
============================================================================================\n","SMN","APN",\
"BOARD","SW ID","SW REV","SW NAME")
	for (i=0;i<=99;i++)
	{
		for (j=0;j<=27;j++)
		{
			print_line=0
			for (comb in only1)
			{
				if (only1boards[comb]==sprintf("%s,%s",i,j))
				{
					printf("%3s  %3s  %s\n",i,j,only1[comb])
					print_line=1
				}
			}
			if (print_line==1) printf("--------------------------------------------------------------------------------------------\n")
		}
	}
	printf("\n\
SOFTWARE FOUND ONLY IN NODE 2\n\
============================================================================================\n\
%3s  %3s  %-9s  %-15s  %-10s  %s\n\
============================================================================================\n","SMN","APN",\
"BOARD","SW ID","SW REV","SW NAME")
	for (i=0;i<=99;i++)
	{
		for (j=0;j<=27;j++)
		{
			print_line=0
			for (comb in only2)
			{
				if (only2boards[comb]==sprintf("%s,%s",i,j))
				{
					printf("%3s  %3s  %s\n",i,j,only2[comb])
					print_line=1
				}
			}
			if (print_line==1) printf("--------------------------------------------------------------------------------------------\n")
		}
	}		
	
}
'
}

case $# in
4) timestamp=$(date +%y%m%d-%H%M)
   logfile1=$logdir/${timestamp}_$1.log
   logfile2=$logdir/${timestamp}_$3.log
   if ! get_logs $1 $2 $logfile1 $3 $4 $logfile2 ; then 
   	echo "!!!!Failed to get cabview logfiles. Exiting..."
   	exit 1
   fi
   swcomp $logfile1 $logfile2
   ;;
3) timestamp=$(date +%y%m%d-%H%M)
   logfile1=$logdir/${timestamp}_$1.log
   logfile2=$3
   if ! test -f $logfile2 ; then   
        echo "!!!!Can't find logfile "$logfile2". Exiting..."
        exit 1
   fi
   if ! get_logs $1 $2 $logfile1 ; then 
   	echo "!!!!Failed to get cabview logfiles. Exiting..."
   	exit 1
   fi
   swcomp $logfile1 $logfile2   
   ;;
2) logfile1=$1
   logfile2=$2
   if ! test -f $logfile1 ; then echo "!!!!Can't find logfile "$logfile1". Exiting..." ; exit 1; fi
   if ! test -f $logfile2 ; then echo "!!!!Can't find logfile "$logfile2". Exiting..." ; exit 1; fi
   swcomp $logfile1 $logfile2 ;;
*) print_usage ; exit 1
  ;;   
esac
