#!/usr/bin/perl

open REPORT, ">jtag.tex";

#print REPORT "\\begin{center}\n";
#print REPORT "\\begin{tabular}{lp{0.5\\textwidth}}\n";


for $testfile (`ls jtag/*.log`) {

    chop $testfile;

    $testfile =~ 
        /(\d\d\d\d)(\d\d)(\d\d)-(\d\d)(\d\d)(\d\d).log$/;

    printf REPORT ( "\\textbf{\\large %d.%d.%d %d:%02d:}",
		    $3,$2,$1,$4,$5);
    #"\textbf{$3.$2.$1 $4:$5}"
    


    if (!system ( "grep '^Last connection tested, the whole ring is ok\$' ".
		  "$testfile > /dev/null" ) ) {

	print "$testfile: ok\n";

	#print REPORT "JTAG test is ok. Tested DCS boards:\n\n";
	print REPORT "\\textbf{\\large\\hspace{1mm}OK} \n\\hfill\n";

	print REPORT "\\begin{tabular}[t]{|r|l|r|r|}\n";
	print REPORT "\\hline\n";
	print REPORT "\\multicolumn{1}{|c|}{\\textbf{Hostname}} & ";
	print REPORT "\\multicolumn{1}{c|}{\\textbf{IP}} & ";
	print REPORT "\\multicolumn{1}{c|}{\\textbf{MAC}} &";
	print REPORT "\\multicolumn{1}{c|}{\\textbf{serial}} \\\\\n";
	print REPORT "\\hline\n";
	
	foreach $i (`awk '/active Board/ {print \$5" "\$7}' $testfile`) {
	    ($ip, $mac) = split / /, $i;
	    chop $mac;
	    
	    `host $ip` =~ /domain name pointer (alidcsdcb\d\d\d\d).trd.net./;
	    $hostname = $1;
	    
	    $mac =~ /([0-9a-fA-F]{2}):([0-9a-fA-F]{2}):([0-9a-fA-F]{2}):([0-9a-fA-F]{2}):([0-9a-fA-F]{2}):([0-9a-fA-F]{2})/;

	    $serial = hex "$5$6";

	    #print REPORT "IP: $ip, MAC: $mac Host: $hostname\\\\\n";
	    print REPORT "$hostname & $ip & $mac & $serial \\\\\n";
	}
	
	print REPORT "\\hline\n";
	print REPORT "\\end{tabular}\n";
	print REPORT "\\vspace{5mm}\n\n";
	
    } else {
	
	print "$testfile: errors found\n";

	open LOG, "<$testfile";
	
	
	print REPORT "\\textbf{\\large\\hspace{1mm}NOT OK} \n\\hfill\n";
	print REPORT "{\\small\n";
	print REPORT "\\begin{verbatim}\n";
	
	while (<LOG>) {
	    print REPORT;
	    
	}
	
	print REPORT "\\end{verbatim}\n";
	print REPORT "}\n";
    }
}

#print REPORT "\\end{tabular}\n";
#print REPORT "\\end{center}\n";

close REPORT;


