#!/usr/bin/perl

use DBI;

use CGI qw(-no_debug :standard);
$q = new CGI;

$dbh = DBI->connect("dbi:mysql:algol60", "root", "");
die DBI::errstr if not defined $dbh;

$sth = $dbh->prepare(<<END);
	select * from case order by type, num, phase
END
die $dbh->errstr, "\n" if not defined $sth;

print  $q->start_html( -title => "Corey and George's Test Plan", -text => 'black', -bgcolor => 'white' ),
       $q->h1("SAN474: Algol60 compiler"),
       $q->hr,
       $q->table;
$sth->execute;

die "usage: $0 [index|complete]\n\n" if -z $ARGV[0];
doc_index() if $ARGV[0] eq "index";
doc_complete() if $ARGV[0] eq "complete";

$sth->finish;

print $q->end_table, $q->end_html; 
$dbh->disconnect;

sub doc_index {
    my @names = @{ $sth->{NAME} };
    print $q->Tr, $q->td({-colspan=>scalar(@names)});
    print "View ", $q->a({-href=>'grammar.html'}, "our current grammar"),
	  " or the acutal ",  $q->a({-href=>'/~greerga/compiler/RRA60/'}, "Algol60 spec."),
	  $q->br, 
	  "We also have online copies of the ", $q->a({-href=>'mu.grammar.html'}, "MU grammar"),
	  ", and the ", $q->a({-href=>'compilerProject.html'}, "project description"), ".";
    print $q->end_td, $q->end_Tr;

    print $q->Tr({-bgcolor=>'silver'}, [ $q->td([ map { ucfirst } @names ]) ]);

    my %row;
    while( %row = %{ $sth->fetchrow_hashref() } )
    {
       $row{description} = $q->a({-href=>"complete.html#$row{type}_$row{num}"}, $row{description});

       my @vals;
       foreach $column ( @names ) { push(@vals, $row{$column}); }

       print $q->Tr([ $q->td([ @vals ]) ]);
    }
}

sub doc_complete {
    my %row;
    while( %row = %{ $sth->fetchrow_hashref() } )
    {
       if( $row{type} eq "fail" ) {
	    $algol = "test/fail/".$row{num}."/in";
	    $right = "test/fail/".$row{num}."/err";
	    $bgcolor = "#ddddee";
       } else {
	    $algol = "test/pass/".$row{num}."/in";
	    $right = "test/pass/".$row{num}."/mu";
	    $bgcolor = "#eedddd";
       }
       $proposed = -k $algol ? $q->b('Proposed') : '';

       @header = ( $proposed, $q->a({-name=>"$row{type}_$row{num}"}) . ucfirst($row{type}), "Case: $row{num}", "Phase: $row{phase}", $row{description} );
       print $q->Tr({ -bgcolor=>$bgcolor }, [
		    $q->td({-colspan=>2}, [
			$q->table([ $q->Tr([ $q->td([ @header ]) ]) ])
			])
		    ]);

       print $q->Tr, $q->td({-valign=>'top'});

       open(ALGOL, "<$algol");
       print '<PRE>';
       while (<ALGOL>) {
         print;
       }
       close(ALGOL);
       print '</PRE>';

       print $q->end_td, $q->td({-valign=>'top'});

       open(MU, "<$right");
       print $q->font({-face=>'courier'}, join("<br>", <MU>));
       close(MU);

       print $q->end_td, $q->end_Tr;
    }
}

