#!/usr/bin/perl
use strict;
use warnings;
use CGI ':all';
use DBI;

####################### CONFIG HERE #######################
my $conf_file = 'myconf.conf';                            #
my $pb        = "./parrotbench.gat -c $conf_file";        #
my $db        = 'benchmark.db';                           #
###########################################################

open (MYCONF, '<', $conf_file) or die "Unable to open $conf_file for reading : $!";

my @table;
push @table, /^([^:]+):/ while <MYCONF>;

open (TESTS, '-|', "$pb -l" ) or die "Unable to call $pb : $!";
my @test = map { s/-/_/g; /^([^,]+),/ } <TESTS>;

my $dbh = DBI->connect("dbi:SQLite2:dbname=$db","","") or die $DBI::errstr;

Stats( $dbh, \@table, \@test );
Main( $dbh );

sub Stats {
   my ($dbh, $tables, $fields) = @_;

   my $sth = $dbh->prepare("SELECT time FROM parrot");
   $sth->execute() or die $dbh->errstr;
   my $results = $sth->fetchall_arrayref;

   for my $date ( map { $_->[0] } @{ $results } ) {
       open ( HTML , '>' , "$date.html" ) or die "Unable to open $date.html for writing : $!";
       select HTML;
       $| = 1;
       my @results;
       for my $table ( @{ $tables } ) {    
           my $sth = $dbh->prepare("SELECT * FROM $table where time = $date");
           $sth->execute() or die $dbh->errstr;
           push @results , [ $sth->fetchrow_array ];
       }
       my $title = "BENCHMARK STATS FOR " . scalar localtime $date;
       print
           start_html( -title => $title, -bgcolor => "#ffffcc" ),
           div( { -align => "center" },
               a({ href=>"parrot_bench.html"}, "Main Menu" ),
               p(h1( $title ) ),

               table(
                   {
                   -bgcolor     => "#000000",
                   -border      => "0",
                   -cellpadding => "2",
                   -cellspacing => "1",
                   },
                   Tr( { -style => "background-color:#CCCCCC" },
                       th( [ 'PROGRAM', 'TOTAL', map {uc} @$fields ]  ),
                   ),
                   Tr( { -style => "background-color:#CCCCCC" },
                       [ map {
                             my $table = $_;
                             my $row = $results[$table];
                             td([ strong(uc($tables->[$table])), map {$row->[$_]} 1 .. $#$row ]),
                         } 0 .. $#$tables 
                       ]
                   ),
               ),
           ),
       end_html;
    }
}

sub Main {
   my $dbh = shift;
   my $file = 'parrot_bench.html';
   open ( HTML , '>' , $file ) or die "Unable to open $file for writing : $!";
   select HTML;
   $| = 1;

   my $sth = $dbh->prepare("SELECT time FROM parrot");
   $sth->execute() or die $dbh->errstr;
   my $results = $sth->fetchall_arrayref;

   print
       start_html( -title => 'MAIN MENU', -bgcolor => "#ffffcc" ),
       div( { -align => "center" },
           p(h1( 'MAIN MENU' ) ),
           map {
               p( a({ href=>"$_->[0].html"}, scalar localtime($_->[0]) ) ),
           } sort { $a->[0] <=> $b->[0] } @{ $results }
       ),
       end_html;
}
