#!/usr/bin/perl -w

use strict;

my @list = undef;
my @fields = undef;
my @sort = undef;
my @sorted = undef;
my $line = undef;

my $prefix = undef;
my $suffix = undef;

my $count = 0;
my $id = 0;

#open (INFILE, "<ethereal-G-sortedXLS-escaped.txt")
#	or die "Error: can't open input file: $!\n";

open (INFILE, "tethereal -G|")
	or die "Error: can't execute tethereal -G for input: $!\n";

open (OUTFILE, ">ethereal-G-index.html")
	or die "Error: can't open output file: $!\n";

open (TMP, ">ethereal-G-foo.html")
	or die "Error: can't open temp file: $!\n";

# The protocol field tables look like:
#
# .pn (protocol name)  |   .pl (protocol long name) (spans 2 columns)
# ---------------------+   --------------------------------------------------
#
# .pd (protocol field) |   .pt (datatype)       .ps (short field description)
#    (spans 2 rows)    |   .pe (extended field description) (spans 2 columns)
#
# This layout is reflected in the CSS style defined below.

print OUTFILE << "EOS";
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <title>Ethereal protocols and protocol fields</title>
  <style type="text/css">
  body {
	font-family: Arial, Helvetica, Verdana;
	font-size: 12pt;
  }
  h1 {
	background: #ffffff;
	color: #000000;
	font-size: 20pt;
	font-weight: bold;
  }
  h2 {
	background: #ffffc0;
	color: #000000;
	font-size: 16pt;
	font-weight: bold;
  }
  .pn {
	font-family: Courier;
	color: #5050ff;
	font-weight: bold;
	border-bottom: 2px solid #000000;
	border-right: 1px solid #000000;
  }
  .pl {
	color: #000000;
	font-weight: bold;
	border-bottom: 2px solid #000000;
  }
  .pd {
	font-family: Courier;
	color: #a0a0a0;
	font-weight: bold;
	vertical-align: top;
	border-right: 1px solid #000000;
  }
  .pt {
	font-family: Courier;
	color: #000000;
  }
  .pe {
	color: #5050ff;
  }
  .ps {
	color: #000000;
  }
  </style>
</head>
<body>
<h1><a name="_top_">Select a protocol</a></h1>
<p align="center">
EOS

while (<INFILE>) {

	# &, < and > have special meanings in html
	s/\&/\&amp;/g;
	s/</\&lt;/g;
	s/>/\&gt;/g;

	# general preparations/bookkeeping
	chomp;
	$count += 1;

	if (/^P/) { # Protocol entry
		$id += 1;
		@list = split ("\t");
		$list[0] = "$id";
		print OUTFILE <<"EOS";
<a href="#$list[2]">$list[2]</a>
EOS
		if ($count > 1) {
			@sorted = sort @sort;
			print TMP @sorted;
			print TMP <<"EOS";
</table>
<p><a href="#_top_">Back to protocol list</a></p>

EOS
			$count = 0;
			@sort = undef;
		}
		print TMP <<"EOS";
<h2><a name=$list[2]><tt>$list[2]</tt> - $list[1]</a></h2>
<table border="0" cellspacing="0" summary="Protocol field list for $list[2]">
<thead>
<tr>
  <td class="pn">$list[2]</td>
  <td colspan="2" class="pl">$list[1]</td>
</tr>
<tbody>

EOS
	} else { # Protocol field entry
		s/^F/$id/;
		@list = split ("\t");
		if ($#list == 4) {
			push (@list, "&nbsp;");
		}

		@fields = split ("[.]", $list[2]);
		$suffix = pop (@fields);
		$prefix = join (".", @fields);
		$line = <<"EOS";
<tr>
  <td rowspan="2" class="pd">$prefix<font color="#ff0000">.$suffix</font></td>
  <td class="pt">[$list[3]]</td>
  <td class="ps">$list[1]</td>
</tr>
<tr>
  <td colspan="2" class="pe">$list[5]</td>
</tr>

EOS
		push (@sort, $line);
	}
}

my $date=`date`;
chomp $date;

@sorted = sort @sort;
print TMP @sorted;
print TMP <<"EOS";
</table>
<p align="left"><a href="#_top_">Back to protocol list</a></p>

<p>This list has been generated on $date.</p>

</body>
</html>
EOS

close (TMP);

open (TMP, "<ethereal-G-foo.html")
	or die "Error: can't read from temp file: $!\n";

print OUTFILE "</p>\n\n<h1>Protocol and field name description</h1>\n\n";

while (<TMP>) { print OUTFILE $_; }


close (TMP);
close (OUTFILE);

