#!/usr/bin/perl

my $some_dir="output";

opendir(DIR, $some_dir) || die "can't opendir $some_dir: $!";
@logs = sort grep { /^allbuilds/ && -f "$some_dir/$_" } readdir(DIR);
closedir DIR;

my $building = $ARGV[0];

my @b;
my @rounds;
my %round;
my %dir; # hash per type for build dir

# number of builds in the output table
my $numout = 4;

sub nicehead {
    print <<MOO
<?xml version="1.0"?>
<RSS version="2.0">
    <channel>
        <title>Rockbox Latest Build Output</title>     
        <link>http://www.rockbox.org/cvs.shtml</link>
        <description>Rockbox Latest Build Output</description>
MOO

}


sub urlencode {
    shift() if ref($_[0]) || $_[0] eq $DefaultClass;
    my $toencode = shift;

    return undef unless defined($toencode);
    $toencode=~s/([^a-zA-Z0-9_.-])/uc sprintf("%%%02x",ord($1))/eg;
    return $toencode;
}

my $build=0;

sub log2html {
    my ($file)=@_;

    my $log=0;
    my $bigstart=-1;

    open(LOG, "<$file");
    while(<LOG>) {
        $line = $_;

        if($line =~ /^Build Server: (.*)/) {
            $lserver = $1;
        }
        elsif($line =~ /^Build Time: (\d+)/) {
            $btime = $1;
        }
        elsif( $line =~ /^Build End All Systems/) {
            # this is how a complete build round MUST stop
            $bigstart=0;
        }
        if(!$log) {
            if( $line =~ /^Build Start All Systems/) {
                if($bigstart > 0) {
                    # the round was not completed properly until this new one
                    # starts so we don't care of the previous one but instead
                    # try to hide it
                }
                else {
                    $bigstart=1;
                    $build++; # number of this "round"
                    push @rounds, $build;
                }
            }
            elsif($line =~ /^Build Start Single/) {
            }
            elsif($line =~ /^Build Date: (.*)/) {
                $date = $1;
            }
            elsif($line =~ /^Build Dir: (.*)/) {
                # usually after the build type
                $dir{$type}=$1;
            }
            elsif($line =~ /^Build Type: (.*)/) {
                $type = $1;

                push @b, $type.$date;

                $alltypes{$type}.="$date%";

                # remember all types in this round
                $round{$build}.= "$type$date%";
            }
            elsif($line =~ /^Build Status: (.*)/) {
                $status{$type.$date} = $1;
                $type{$type.$date} = $type;
                $date{$type.$date} = $date;
                $server{$type.$date} = $lserver;
            }
            elsif($line =~ /^Build Log Start/) {
                $btime{$type.$date} = $btime;
                $log = 1;
            }
        }
        else {
            if($line =~ /^Build Log End/) {
                $log = 0;
            }
            else {
                $compile{$type.$date} .= $line;

                if($line =~ /^([^:]*):(\d*):.*warning: (.*)/) {
                    if($3 !~ /\(near/) {
                        # we don't count "(near" comments as warnings
                        $warnings{$type.$date}++;
                    }
                }
                elsif(($line =~ /^([^:]+):(\d+):(.+)/) ||
                      ($line =~ /: undefined reference to/) ||
                      ($line =~ /gcc: .*: No such file or/) ||
                      ($line =~ /ld returned (\d+) exit status/) ||
                      ($line =~ /^ *make\[.*\*\*\*/) ) {
                    # error
                    $errors{$type.$date}++;
#                    print STDERR "Error $type $date $1 $2\n";
                }
            }
        }
    }
}


for(@logs) {
    log2html("$some_dir/$_");
}
nicehead();

my $numbuilds = scalar(keys %alltypes);
if($building) {
    print <<MOO
            <item>
                <title>Build in progress</title>
                <link>$building</link>
                <description>Build in progress</description>
            </item>
MOO
}

my $count=0;
for(reverse @rounds) {
    my @types = split("%", $round{$_} );

    if($count++ >= $numout) {
        last;
    }

    my $batch = $date{$types[0]};
    my $stamp=$batch;

    if( -f "output/chlog-$batch.html") {
        my $vis = $batch;
        $vis =~ s/ /%20/g;
        $batch =~ s/(\d\d):(\d\d):(\d\d)/$1:$2/g; # cut off number of seconds
    }
    print "            <item>\n";
    print "                <title>Build $batch</title>\n";
    print "                <link>http://www.rockbox.org/cvsmod/chlog-$vis.html</link>\n";
    print "                <description>Build $batch, Score ";

    my $score=0;
    for(sort keys %alltypes) {
        my $show = $_;
        for(@types) {
            if($show eq $type{$_}) {
                my $stat =  $status{$_};

                if ($errors{$_} == 0) {
                    if ($stat =~ /Failed/) {
                        $score += 20;
                    }
                    else {
                        if($warnings{$_}) {
                            $score += $warnings{$_};
                        }
                    }
                }
                else {
                    $score += ($errors{$_} * 10) + $warnings{$_};
                }
            }
        }
    }
    print "$score</description>\n";
    print "            <item>\n";
}
print <<MOO
    </channel>
</RSS>
MOO
