#!/usr/local/bin/perl -w

#
# db_to_html_table
# Copyright (C) 2002 by John Heidemann <johnh@isi.edu>
# $Id: db_to_html_table,v 1.5 2005/03/14 02:58:40 johnh Exp $
#
# This program is distributed under terms of the GNU general
# public license, version 2.  See the file COPYING
# in $dblibdir for details.
#
sub usage {
    print <<END;
usage: $0 

Covert an existing dbtable to an HTML table.
Does NOT do any of the HTML stuff around the table.

options:

    -g N   group by color into groups of n

Related programs:
    dbcolneaten, dbrecolize

END
    exit 1;
}

BEGIN {
    $dblibdir = "/usr/local/lib/jdb";
    push(@INC, $dblibdir);
}
require "$dblibdir/dblib.pl";
use DbGetopt;

my($debug) = 0;
my($grouping) = undef;
my(@orig_argv) = @ARGV;
my($prog) = &progname;
my($dbopts) = new DbGetopt("d?g:", \@ARGV);
my($ch);
while ($dbopts->getopt) {
    $ch = $dbopts->opt;
    if ($ch eq 'd') {
	$debug++;
    } elsif ($ch eq 'g') {
	$grouping = $dbopts->optarg;
    } else {
	&usage;
    };
};
#&usage if ($#ARGV < 0);
my($new_fs_code) = @ARGV;


&readprocess_header;

@f = ();   # suppress warning
my($group_n) = 0;
print "<table>\n";

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

my($color1, $color2) = ("#f0f0f0", "#d0d0d0");

while (<STDIN>) {
    &delayed_pass_comments && next;
    &split_cols;
    my($color) = "";
    if (defined($grouping)) {
	if ($group_n++ >= $grouping) {
	    ($color1, $color2) = ($color2, $color1);
	    $group_n = 1;
	};
	$color = "bgcolor=\"$color1\"";
    };
    print "<tr $color>";
    foreach (@f) {
	$_ = dblib_text2html($_);
	print "<td>$_</td> ";
    };
    print "</tr>\n";
};
print "</table>\n";

# print "#  | $prog ", join(" ", @orig_argv), "\n";
exit 0;

# warning suppression
@colnames = ();
