#!/usr/bin/perl

open (DATABASE, "<index.rdb");
while (<DATABASE>) {
	chomp;
	if (/digest/) {
		push @entries, $_;
		$digest_file = $_; 
		$digest_file =~ s/.*\t([a-z]*-unstable-digest_[0-9]+).*/\1/;
		$single_file = $_;
		$single_file =~ s/^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t([^\t]*).*/\1/;
		push @digest_files, $digest_file;
		$subject = $_;
		$subject =~ s/^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t([^\t]*).*/\1/;
		$subject .= "\t" . $single_file;
		push @subjects, $subject;
		$name = $_;
		$name =~ s/^[^\t]*\t[^\t]*\t[^\t]*\t([^\t]*).*/\1/;
		$name =~ s/[\s]+$//;
		$name =~ s/^[\s]$//;
		push @names, $name;
		$address = $_;
		$address =~ s/^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t([^\t]*).*/\1/;
		$address =~ tr/[A-Z]/[a-z]/;
		push @addresses, $address;
		}
	}
close (DATABASE);

# Generate index_subjects.html
open (SUBJECTS, ">index-subjects.html");
print SUBJECTS &html_header ("subjects");
@subjects = sort(@subjects);
foreach $subject_line(@subjects) {
	($subject, $file) = split ("\t",$subject_line);
	if ($subject) {print SUBJECTS "<li><a href=\"$file\" name=\"$subject\">$subject<\/a>\n"}
	}
print SUBJECTS &html_footer("index.html","unstable digest index");
close (SUBJECTS);
print "index-subjects.html successfully generated.\n";


# Generate index_names.html

open (NAMES, ">index-names.html");
print NAMES &html_header("names");
@names = &uniq(@names);
@names = sort(@names);
foreach $name(@names) {
	print NAMES "<li><a name=\"$name\">$name</a>\n";
	print NAMES "<ul>\n";
	$pattern = &cleanpattern($name);
	foreach $entry(@entries) {
		if ($entry =~ /$pattern/i) {
			$subject = $entry;
			$subject =~ s/^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t([^\t]*).*/\1/;
			$file = $entry;
			$file =~ s/^[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t[^\t]*\t([^\t]*).*/\1/;
			print NAMES "<li><a href=\"$file\">$subject</a>\n";
			}
		}
	print NAMES "</ul>\n\n";
	}
print NAMES &html_footer("index.html","unstable digest index");
close (NAMES);
print "index-names.html successfully generated.\n";


# Generate index-addresses.html

open (ADDRESSES, ">index-addresses.html");
print ADDRESSES &html_header("addresses");
@addresses = &uniq(@addresses);
@addresses = sort(@addresses);
foreach $address(@addresses) {
	print ADDRESSES "<li><a name=\"$address\">$address\n";
	print ADDRESSES "<ul>\n";
	$pattern = &cleanpattern($address);
	foreach $entry(@entries) {
		if ($entry =~ /$pattern/i) {
			$subject = $entry;
			$subject =~ s/^[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t([^\t]+).*/\1/;
			$file = $entry;
			$file =~ s/^[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t[^\t]+\t([^\t]+).*/\1/;
			print ADDRESSES "<li><a href=\"$file\">$subject</a>\n";
			}
		}
	print ADDRESSES "</ul>\n\n";
	}
print ADDRESSES &html_footer("index.html","unstable digest index");
close (ADDRESSES);
print "index-addresses.html successfully generated.\n";


# Generate homepage index.html
open (HOMEPAGE, ">index.html");
print HOMEPAGE &html_header("unstable digests");
@digest_files = &uniq(@digest_files);
foreach $digest_file(@digest_files) {
	$digest_title = $digest_file;
	$digest_title =~ s/-/ /g;
	$digest_title =~ s/_/ vol /;
	print HOMEPAGE "<li><a href=\"$digest_file\/index.html\" name=\"$digest_file\">$digest_title</a>\n";
	}
print HOMEPAGE &html_footer('','',"<hr>\n\n<ul>\n<li><a href=\"index-subjects.html\">work by subject lines<\/a>\n<li><a href=\"index-addresses.html\">work by addresses</a>\n<li><a href=\"index-names.html\">work by names/aliases</a>\n</ul>\n\n");


close (HOMEPAGE);
print "index.html successfully generated.\n";


sub uniq {
	my @input = @_;
	my @output, $x, $y, $match;
	for ($x=1; $x<=$#input; $x++) {
		$match = 0 ;
		for ($y=0; $y<$x; $y++) { 
			if ($input[$x] eq $input[$y]) {$match=1}
			}
		if ($match==0) {push @output,$input[$x]}
		}
	return @output;
 	}

sub html_header {
	my $output;
	my $title = $_[0];
	$output = "<html>\n" . "<head>\n" . "<title>$title</title>\n" . "</head>\n\n" . "<body>\n\n" . "<h1>$title</h1>\n\n" . "<ul>\n";
	return $output;
	}

sub html_footer {
	my $url = $_[0];
	my $page = $_[1];
	my $extra = $_[2];
	my $output;
	$output .= "</ul>\n\n";
	if ($extra) {
		$output .= $extra;
		}
	if ($url && $page) {
		$output .= "<hr>\n\n";
		$output .= "Back to <a href=\"$url\">$page</a>\n\n";
		}
	$output .=  "</body>\n";
	$output .= "</html>\n";
	return $output;
	}

sub cleanpattern {
	my $output = $_[0];
	$output =~ s/^[\W]+//;
	$output =~ s/[\W]+$//;
	$output =~ s/\//\\\//g;
	$output =~ s/\?/\\\?/g;
	$output =~ s/\=/\\\=/g;
	$output =~ s/\*/\\\*/g;
	$output =~ s/\+/\\\+/g;
	$output =~ s/\[/\\\[/g;
	$output =~ s/\]/\\\]/g;
	$output =~ s/\(/\\\(/g;
	$output =~ s/\)/\\\)/g;
	return $output;
	}

