#!/usr/bin/perl
#
# txt2html [file]
#
# This is a prototype program to read text and  convert  it  to  html.
# This program acts like a conventional Unix filter program:  It reads
# the file(s) named on the command line, or stdin if there are no file
# names on the command line; it write to stdout.
#

$| = 1;	# Don't buffer output.
($me = $0) =~ s".*/"";
$D = $ENV{"D_$me"} || 0;

@txt = <>;	#Slurp up all the text into memory.

# We now run around in @txt, rewriting things as we find them.
if ($txt[0] =~ /[A-Z][-A-Za-z0-9_]+:/) {
	print "Mail headers ...\n" if $D;
	splice @txt,0,0,"<pre>\n";
hdr:
	for ($h = 1; $h <= @txt; $h++) {
		print "Hdr $h: $txt[$h]" if $D;
		if ($txt[$h] =~ /^\s*$/) {
			$txt[$h] = "</pre>\n";
			last hdr;
		} elsif ($txt[$h] =~ /^Subject *:\s*(.*)\n/) {
			$subj = $1;
			print "Subj: $subj\n" if $D;
		} elsif ($txt[$h] =~ /rec.humor.funny/) {
			$cite = '<cite>From the <a href=http://comedy.clari.net/rhf/>rec.humor.funny</a> joke archives.<p></cite>';
		}
	}
}
if ($subj) {
	splice @txt,$h,0,"$cite\n";
}
++$h;
if ($subj) {
	splice @txt,$h,0,"<body>\n";
	splice @txt,0,0,
		"<head>\n",
		"<title>\n",
		"$subj\n",
		"</title>\n";
	$h += 5;
}

for ($l = $h; $l < @txt; $l++) {
	$line = $txt[$l];
	print "Txt $l: $line" if $D;
	if ($line =~ /^\s*$/) {
		$txt[$l] = "<p>\n";
		print "Txt $l: $txt[$l]" if $D;
	}
}

# When that loop terminates, we write the results to stdout.
print "-----------------------------\n" if $D;
print "<html>\n";
for $line (@txt) {
	print $line;
}
print "</html>\n";
