#!/usr/bin/perl -w

use warnings;
use strict;
use CGI qw(:standard *table);

# Location of screen/field description file
my $screenflddescpath = "descriptions.txt";

open SCREENFLDDESC, $screenflddescpath
    or die "cannot find screen description file: $!";

my %screenflddesc;

while (<SCREENFLDDESC>) {
    chomp;
    my ($key, $desc) = split /,/;
    $screenflddesc{$key} = $desc;
}

close SCREENFLDDESC;

# Location of index file we will write results to
my $indexfile = ">screenindex.html";
open INDEX, $indexfile or die "cannot open index for writing: $!";

# Location of master GUI file
my $guipath = "Master.gui";
# my $guipath = "Master.gui.test";

open GUIFILE, $guipath or die "cannot find Master.gui: $!";

# Start our index file
print INDEX start_html("GUI Map Index"), "\n";
print INDEX h1("GUI Map Index"), "\n";
print INDEX "Click on a screen name below to go to its description page",
    br, "\n";
print INDEX hr, "\n";

# Flag to let us know if we're inside a description block
my $desc = 0;

# Current file name
my $currfile = "";

# Variables used in loop
my $screen;
my $screenname;
my $fldname;

while (<GUIFILE>) {
    chomp;

    # print "Line: $_\n";

    # Outside description block with no ".": Screen name
    # (or quoted name without following ".")
    # (Regex was originally /^([^.]+):$/)

    if (!$desc and /^(\w+):$/) {
        # print "Screen name: $1\n";

        # Save original version of name (possibly w/ quotes),
        # and strip away quotes (if present)
        $screen = $1;
        ($screenname = $screen) =~ tr/"//d;
        # Use the first 4 characters as the file name, replacing bad
        # characters with _
        (my $screenfilename = substr $screenname, 0, 4) =~ tr[ /][_];

        # If the current file doesn't have this name, finish it up
        # and open a new file
        if ($. == 1 or $currfile ne $screenfilename) {
            if ($. != 1) {
                print SCREEN end_table, end_html;
                close SCREEN;
            }
            # Some exceptions where we want the link name to be the
            # 4-letter file name
            my $sn = "";
            if ($screenname =~ /^(AACE|AM01|MOAC|WADV)/) {
                $sn = $screenfilename;
            } else {
                $sn = $screenname;
            }
            # Print a line to the index pointing to the new file to be
            # created
            print INDEX a({-href => "screens/$screenfilename.html"},
                          $sn), br, "\n";

            # Open a new screen file based on this screen name
            $currfile = $screenfilename;
            open SCREEN, ">screens/$screenfilename.html"
                or die "cannot create screens/$screenfilename.html: $!";
            print SCREEN start_html("Description of $screenname"), "\n";
            print SCREEN a({-href => "../screenindex.html"}, "Back"), "\n";
        } else {
            # Just close the previous table
            print SCREEN end_table, "\n";
            print SCREEN hr, "\n";
        }

        print SCREEN h1($screenname), "\n";
        print SCREEN h2("Description:"), "\n";
        if (defined $screenflddesc{$screenname}) {
            print SCREEN $screenflddesc{$screenname}, br, "\n";
        } else {
            print SCREEN "(No description)", br, "\n";
        }
        print SCREEN h2("Fields:"), "\n";
        print SCREEN start_table({-border => 1}), "\n";
        print SCREEN Tr(td(b("Field name")), td(b("Description")),
                        td(b("Location (row, column)"))), "\n";
    } elsif (/^\{$/) {
        # Start of a description block
        # print "Start of block\n";
        $desc = 1;
    } elsif (/^\}$/) {
        # End of a description block
        # print "End of block\n";
        $desc = 0;
    } elsif (!$desc and /^"?$screen"?\.(.+):/) {
        # print "Field name: $1\n";

        # Strip quotes from field name
        ($fldname = $1) =~ tr/"//d;
    } elsif ($desc and /^\s*id:\s*(\d+)$/) {
        # print "ID for field $fldname: $1\n";

        # The ID for the particular field
        # Get X and Y locations: X = int $id/80 + 1, Y = $id % 80 (or
        # 80 if $id % 80 == 0)
        my $xloc = int (($1/80) + 1);
        my $yloc = $1 % 80;
        if ($yloc == 0) {
            $yloc = 80;
        }
        # Get the description for this field (if it exists)
        my $key = $screenname . "." . $fldname;
        my $flddesc;
        if (defined ($screenflddesc{$key})) {
            $flddesc = $screenflddesc{$key};
        } else {
            $flddesc = "(No description)";
        }
        # Print row describing this field
        print SCREEN Tr(td($fldname), td($flddesc), td("Row $xloc, Column $yloc")), "\n";
    } else {
        # print "(ignored)\n";
    }
} # end while <GUIFILE>
print INDEX end_html;

close GUIFILE;
close INDEX;
