#!/usr/bin/perl -w
#
# $Revision: 1.7 $ $Date: 2012-07-26 17:11:47-04 $
# $Source: /home/vogelke/bin/RCS/eztable,v $
# $Host: sys7.com $
# $UUID: 5145b72e-e64f-3e6f-92b9-5a34896b72a9 $
#
#<eztable: read title and tab-separated fields, write table.

# Read the overall title and column description
# from the first two lines.

chomp($title = <>) || die "no title";
chomp($_     = <>) || die "no columns";
@cols = split(/\t/);

# Store the remainder of the file.  We could do
# line-at-a-time processing, but we might want to simply
# pass everything to a template.

while (<>) {
    chomp;
    push(@rest, $_);
}

# Print the HTML header, title, and table start.

print <<"EndHeader";
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>$title</title>
 <style>
 body {
   width: 70%;
   margin-left: 15%;
 }
 table.main {
   border: 1px solid black;
   width: 100%;
 }
 table.main th {
   border-bottom: 1px solid black;
 }
 table.main td {
   padding: 5px;
   text-align: right;
 }
 table.footer {
   width: 100%;
   background-color: lightblue;
 }
 </style>
</head>
<body>
 <h1>$title</h1>
 <table class="main">
EndHeader

print " <tr>\n";
foreach (@cols) {
    print " <th>$_</th>\n";
}
print " </tr>\n";

# Print the main table entries.

foreach (@rest) {
    print "<tr>\n";
    foreach (split(/\t/)) {
        $_ = htmlsafe($_);
        print " <td>$_</td>\n";
    }
    print "</tr>\n";
}

# Print the footer.

my $adate = arpadate(time());
my @v     = split(/\s+/, version());

print <<"EndFooter";
 </table>
 <p><table class="footer"><tr>
 <td>Created: <em>$adate</em></td>
 <td align="right">Generated by: <em>$v[0] $v[1]</em></td></tr>
 </table>
 </body>
</html>
EndFooter

exit(0);

#---------------------------------------------------------------------
# Escape non-HTML-safe characters.

sub htmlsafe {
    my $text = $_[0];
    return undef unless defined $text;

    # do it in this order, or you get lots of &...
    $text =~ s/\&/&amp;/g;
    $text =~ s/>/&gt;/g;
    $text =~ s/</&lt;/g;
    $text;
}

#---------------------------------------------------------------------
# Return the current version.

sub version {
    my $rcsid  = '$Id: eztable,v 1.7 2012-07-26 17:11:47-04 vogelke Exp $';
    $_ = $rcsid;
    s/,v / /;
    @_ = split;
    return "$_[1]  v$_[2]  $_[3] $_[4]";
}

#---------------------------------------------------------------------
# Accept a date in total seconds since the epoch, and write it
# in ARPA-mail standard form:
#       Fri, 28 Mar 1997 15:40:57 -0500

sub arpadate {
    require "timelocal.pl";
    my ($mday, $mon, $year, $wday);

    my $seconds = time();
    $[ = 0;
    my @DoW = ('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
    my @MoY = (
        'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
        'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'
        );

    # Figure out the timezone.
    my @zone  = gmtime($seconds);
    my $sec   = &timelocal(@zone);
    my $tzmin = ($seconds - $sec) / 60;

    my $sign = "+";
    if ($tzmin < 0) {    # west of GMT
        $sign  = "-";
        $tzmin = -$tzmin;
    }

    # Turn minutes into a recognizable offset from GMT.
    my $hr  = sprintf("%2.2d", int($tzmin / 60));
    my $min = sprintf("%2.2d", $tzmin % 60);
    my $offset = $sign . $hr . $min;    # like +0430 or -0500

    # Now create complete time string.
    ($sec, $min, $hr, $mday, $mon, $year, $wday, undef) =
      localtime($seconds);
    $year += ($year < 70) ? 2000 : 1900;

    my $result = sprintf("%s, %d %s %4d %2.2d:%2.2d:%2.2d %s",
        $DoW[$wday], $mday, $MoY[$mon], $year, $hr, $min, $sec, $offset);

    return $result;
}
