#!/usr/bin/perl -w
eval 'LANG=C exec perl -w -S $0 ${1+"$@"}'
    if $running_under_some_shell;
$running_under_some_shell = 0;

######################################################################
#
# Copyright  Freescale Semiconductor, Inc. 2004-2007. All rights reserved.
#
# Stuart Hughes, stuarth@freescale.com,  29th May 2006
#
# This file is part of LTIB.
# LTIB is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# LTIB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LTIB; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#
######################################################################
use Getopt::Std;

$exclude = { noautobuild => 1};
$build_log = 'build.log';
$date = scalar(gmtime());
chomp($hostname = `hostname`);

$opt_d = 0;
$opt_s = 0;
$opt_e = time();
$ti    = '<br>';

getopts("ds:e:") or die <<TXT;
Usage: mk_pkg_results [ -a ]
    where:
      -d      : just show the delta failure listing
      -s      : start time (seconds since the epoch)
      -e      : end time (seconds since the epoch)
TXT

if($opt_s) {
    $sdate = scalar gmtime($opt_s);
    $edate = scalar gmtime($opt_e);
    $elaps = $opt_e - $opt_s;
    $ti = <<TXT;
<center><table border=1>
<tr> <th>start time</th> <th> end time   </th> <th> elapsed </th> </tr>
<tr> <td>$sdate GMT </td> <td> $edate GMT </td> <td> $elaps seconds </td></tr>
</table></center>
TXT
}

# loop around each autobuilt directory and collect the target platform
# built and a list of packages built and failures
foreach $dir ( glob("*") ) {
    next unless -d $dir;
    next if $exclude->{$dir};

    # extract the target for this directory
    open(MCF, "$dir/.config") or warn("no main config for $dir\n"), next;
    while(<MCF>) {
        if(m,^CONFIG_PLATFORM_DIR="[^\s]+/([^/"]+)",) {
            ($tgt) = $1;
            last;
        }
    }
    close MCF;
    next unless $tgt;
    push @tgts, $tgt;

    # collect the lists of builds and fails
    # 1 => on the build list
    # 2 => failed to build
    $grab_line = 0;
    open(BL, "$dir/$build_log") or warn("open $dir/$build_log : $!\n"), next;
    while(<BL>) {
        if(m,^Processing: (.*),) {
            $pkgs->{$1}{$tgt} = 1;
        }
        if($grab_line) {
            $grab_line = 0;
            foreach $fpkg (split(/\s+/, $_)) {
                $pkgs->{$fpkg}{$tgt} = 2;
            }
        }
        if(m,^These packages failed to build:$,) {
            $grab_line = 1;
        }
    }
}
@astgts = sort @tgts;

if($opt_d) {

# save today's results for comparison next run
$wf = "pkg_res";
rename($wf, "$wf.old") if -f $wf;
open(WF, ">$wf") or die("open(>$wf): $!\n");
foreach $tgt (@astgts) {
    print WF "$tgt";
    foreach $pkg ( sort keys %$pkgs ){
        if($pkgs->{$pkg}{$tgt} && $pkgs->{$pkg}{$tgt} == 2) {
            print WF "\t$pkg";
            $new->{$tgt}{$pkg} = 1;
        }
    }
    print WF "\n";
}
close(WF);

# calculate the diff list if there is a .old file
goto CALC_RES unless -f "$wf.old";
$dt = 1;
open(WF, "$wf.old") or die("open($wf.old): $!\n");
while($line = <WF>) {
    chomp $line;
    @fails = split(/\s+/, $line);
    $tgt = shift(@fails);
    # the second term is to list all packages
    map { $old->{$tgt}{$_} = 1; $pkgs->{$_}{$tgt} = 1 } @fails;
}
close(WF);

CALC_RES:
print <<TXT;
<html>
<body>
<center><b><u>
BSP package build failures on $date GMT from $hostname
</u></b></center>
$ti
For full build results and other information <a href=http://bitshrine.org/ltib/>click here</a><br>
<b><u>KEY</u>:</b>
<b>x</b> : Failed, failed last time, 
<b>+</b> : Failed, new failure, 
<b>-</b> : Changed from fail to pass<br>
TXT

print "<table border=1 cellpadding=2>\n<tr><th>Package</th>\n";
foreach $tgt (@astgts) {
    print "<th>$tgt</th>";
}
print "</tr>\n";

# merge the old and new fails into a single list
foreach $t (keys %$new) {
    foreach $pkg ( keys %{$new->{$t}} ) {
        $fails->{$pkg} = 1;
    }
    foreach $pkg ( keys %{$old->{$t}} ) {
        $fails->{$pkg} = 1;
    }
}
foreach $pkg ( sort keys %$fails ){
    print "<tr>";
    print "<td>$pkg</td>";
    foreach $tgt (@astgts) {
        if(! $dt) {
           $str = $new->{$pkg}{$tgt} ? 'x' : '&nbsp;';
           print "<td align=center>$str</td>";
           next;
        }
        if(       ! $new->{$tgt}{$pkg} && ! $old->{$tgt}{$pkg}) {
            $str = '&nbsp;';
        } elsif ( ! $new->{$tgt}{$pkg} &&   $old->{$tgt}{$pkg} ) {
            $str = '-';
        } elsif (   $new->{$tgt}{$pkg} && ! $old->{$tgt}{$pkg} ) {
            $str = '+';
        } elsif  (   $new->{$tgt}{$pkg} &&   $old->{$tgt}{$pkg} ) {
            $str = 'x';
        }
        print "<td align=center>$str</td>";
        
    }
    print "</tr>\n";
}
print "</table>\n";

print <<TXT;
</body>
</html>
TXT
exit 0;

}

# or: print the full results
print <<TXT;
<html>
<body>
<center><b><u>
BSP package build information $date GMT from $hostname
</u></b></center>
$ti
<b><u>KEY</u>:</b> <b>o</b> = Built okay, <b>x</b> = Failed<br>
TXT

print "<table border=1 cellpadding=2>\n<tr><th>Package</th>\n";
foreach $tgt (@astgts) {
    print "<th>$tgt</th>";
}
print "</tr>\n";
foreach $pkg ( sort keys %$pkgs ){
    print "<tr>";
    print "<td>$pkg</td>";
    foreach $tgt (@astgts) {
        if(! $pkgs->{$pkg}{$tgt} ) {
            $str = '&nbsp;';
        } elsif( $pkgs->{$pkg}{$tgt} == 1 ) {
            $str = 'o';
        } elsif( $pkgs->{$pkg}{$tgt} == 2 ) {
            $str = 'x';
        }
        print "<td align=center>$str</td>";
        next;
    }
    print "</tr>\n";
}
print "</table>\n";


print <<TXT;
</body>
</html>
TXT
exit 0;
