#!/usr/bin/perl

use strict;


sub tail {
    my (%a) = @_;
    my $host =  ( $a{host} or "b0" );
    my $lines = ( $a{lines} or 10 );
    my $grep =  ( $a{grep} or '.' );
    my $cut =   ( $a{cut} or '43-' );

    my $log = "/home/j2eetck/agent-logs/$host.log";

    return `cut -c $cut $log | egrep $grep | tail -n $lines | perl -pe 's/(.{19}).{55}/\$1 - /'`;
} 

print "Content-Type: text/html\n\n";

my @hosts = qw/stan kenny kyle cartman chef jimmy timmy/;

my %content = ();

$content{"1. Last 10 Created"} = tail( grep => 'producer');
$content{"2. Last 10 Started"} = tail( grep => 'Started');
$content{"3. Last 10 Failed"} = tail( grep => 'Completed.*Failed');
$content{"4. Last 10 Succeeded"} = tail( grep => 'Completed.*Success');

foreach my $n ( 1 .. 6 ) {
    my $host = $hosts[$n];
    $content{"5.$n $host (started and completed)"} .= tail(lines => 6, grep => "\"$host.*(Started|Completed)\"");
}

my @sections = sort( keys %content);

my $hr = '---------------------------------------------------------------------------------------------------------------------------------------------------';

print <<HTML;
<html>
<head><title>GBUILD :: TCK :: QuickStatus</title>
<link rel="stylesheet" href="../default.css" type="text/css" />

</head>
<body>
<p/>
<div id="Content">
<table>
<tr>
<td>
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
    <tr class="topBar">
        <td align="left" width="85%">&nbsp;
            <span class="topBarDiv">GBUILD :: TCK :: QuickStatus</span>
        </td>
    </tr>
    </table>

</td>
</tr>
HTML

foreach my $section (@sections){
print <<DOC;
<tr>
<td>
    <div class="basicPanelContainer" style="width: 100%">
        <div class="basicPanelTitle">$section</div>

<div class="preformatted"><div class="preformattedContent">
<pre>
$content{$section}</pre>
    </div>
</td>
</tr>
DOC

}

print <<HTML;
</table>
<div class="bottomshadow"></div>
</div>
</body>
</html>
HTML
