#! /usr/bin/perl

use strict;
use warnings;

use vars qw/%DB %OWNS %PARENT %CURR $last_field $debugmatch $debugparse $key $owner $parent $ref $note_rest $printdupl $maxsize $fileno $fileid $filename/;

$maxsize=0x20000;
%DB=();
%CURR=();
$debugparse=0;
$printdupl=0;
$debugmatch=0;

sub name_to_key
{
my( $r )=@_;

	$r=~tr/A-Z/a-z/;
	$r=~tr/a-z0-9//cd;
	return $r;
}

sub rec_to_name
{
my( %rec )=@_;
my( @list )=("Genus","Species");
my( $item,$r );

	$r="";
	while ($item=shift @list)
		{ $r.=" ".$rec{$item} if exists $rec{$item}; }
	$r=~s/^ //;
	return $r;
}

sub htmlquote
{
($_)=@_;

	s/&/&amp;/g;
	s/</&lt;/g;
	s/>/&gt;/g;
	s/\n/&nl;/g;
	s/"/&quot;/g;
	return $_;
}

sub print_rec
{
my( $preinsert,$postinsert,%rec )=@_;

	return if (!%rec);
	print OUT "<table border=\"1\" frame=\"border\" rules=\"none\"><col width=\"0*\" /><col width=\"1*\" />\n";
	print OUT "<tr><th colspan=\"2\">".$preinsert.&rec_to_name(%rec).$postinsert."</th></tr>\n";
	delete $rec{"Genus"};
	delete $rec{"Species"};
	foreach $key (sort keys %rec)
		{ print OUT "<tr><td class=\"left\">".&htmlquote($key).":</td><td>".&htmlquote($rec{$key})."</td></tr>\n"; }
	print OUT "</table>\n";
}

sub flush_CURR
{
my( $key );

	$key=&name_to_key(&rec_to_name(%CURR));
	return if (!$key);
	delete $CURR{"Family"};
	if (exists $DB{$key})
		{ print STDERR "Key \"$key\" is duplicated!\n" if $printdupl; }
	else {
		print STDERR "ADDKEY: $key\n" if $debugparse;
		%{$DB{$key}}=%CURR;
		}
	%CURR=();
	@{$OWNS{$key}}=();
}

%OWNS=();

while (<>) {
	tr/\r\n//d;
	if (/^ *\304\301\304/) {
		&flush_CURR();
		print STDERR "---\n" if $debugparse;
		}
	elsif (/^([^ł]*[^ ]) *ł *(.*)$/) {
		print STDERR "FIELD: $1, VALUE: $2\n" if $debugparse;
		$last_field=$1;
		$CURR{$1}=$2;
		}
	elsif (/^ *ł *(.*)$/) {
		print STDERR "APPEND: $1\n" if $debugparse;
		$CURR{$last_field}.=" ".$1;
		}
	else {
		print STDERR "DISCARD: $_\n" if $debugparse;
		}
	}
&flush_CURR();
undef %CURR;

%PARENT=();

sub try_reparent
{
my( $reparent,$refkey );

	$refkey=&name_to_key($ref);
	return 0 if ($refkey eq "");
	return 0 if ($parent eq $refkey);
	return 0 if !exists $DB{$refkey};
	print STDERR "try_reparent: SUCCESS: key=\"$key\", refkey=\"$refkey\"\n" if $debugmatch;
	if ($note_rest)
		{ $DB{$key}{"Notes"}=$note_rest; }
	else
		{ delete $DB{$key}{"Notes"}; }

	foreach $reparent (@{$OWNS{$parent}}) {
		$PARENT{$reparent}=$refkey;
		}
	@{$OWNS{$parent}}=() if (!defined $OWNS{$parent});
	@{$OWNS{$refkey}}=() if (!defined $OWNS{$refkey});
	@{$OWNS{$refkey}}=(@{$OWNS{$refkey}},$parent,@{$OWNS{$parent}});
	delete $OWNS{$parent};

	return 1;
}

MATCH: foreach $key (keys %DB) {
	$parent=$PARENT{$key};
	$parent=$key if (!defined $parent); 
	$ref=$DB{$key}{"Notes"};
	next MATCH if (!defined $ref);
	$ref=~tr/()//d;
	$ref=~s/^=//;
	$ref=~s/^O\. *//i;
	$ref=~s/^Orchidaceae *//i;
	$ref=~s/\. *(.*)$//;
	$note_rest=$1;
	next MATCH if &try_reparent();
	$ref=$DB{$key}{"Rank"}." $ref" if exists $DB{$key}{"Rank"};
	next MATCH if &try_reparent();
	$ref=$DB{$key}{"Genus"}." $ref" if exists $DB{$key}{"Genus"};
	next MATCH if &try_reparent();
	}
undef %PARENT;

sub print_header
{
my($header)=@_;

	print OUT
"<?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\" xml:lang=\"en\" lang=\"en\">
<head><title>Kewensis $header</title>
<style type=\"text/css\"><!--
table            { border: ridge; }
blockquote table { border: groove; }
td      { border: none; }
td.left { white-space: nowrap; font-style: italic; }
--></style>
</head><body>
\n";
}

$fileno=-1;

sub OUT_flush
{
	print OUT "</body></html>\n";
	close(OUT);
}

my( $filename );
foreach $owner (sort keys %OWNS) {
my( $child );

	if ($fileno<0 || tell(OUT)>=$maxsize) {
		&OUT_flush() if ($fileno>=0);
		$fileid=sprintf("%04d",++$fileno);
		$filename="kew-$fileid.html";
		open(OUT,">$filename") or die "Cannot open \"$filename\": $!";
		&print_header("chunk $fileid");
		}

	if ($debugmatch) {
		print STDERR "($owner):";
		foreach $child (@{$OWNS{$owner}})
			{ print STDERR " ($child)"; }
		print STDERR "\n";
		}
	&print_rec("<a name=\"".&htmlquote($owner)."\">","</a>",%{$DB{$owner}});
	if (@{$OWNS{$owner}}) {
		print OUT "<blockquote>\n";
		foreach $child (reverse @{$OWNS{$owner}})
			{ &print_rec("","",%{$DB{$child}}); }
		print OUT "</blockquote>\n";
		}
	print OUT "\n";
	$DB{$owner}{"_filename"}=$filename;
	}
&OUT_flush();
open(OUT,">kew-index.html") or die "Cannot open \"kew-index.html\": $!";
&print_header("Index");

sub print_href
{
my($owner)=@_;
my(%rec)=%{$DB{$owner}};

	print OUT &htmlquote(&rec_to_name(%rec));
}

foreach $owner (sort keys %OWNS) {
my( $child );

	print OUT "<p><a href=\"".$DB{$owner}{"_filename"}."#$owner\">";
	&print_href($owner);
	print OUT "</a></p>";
	if (@{$OWNS{$owner}}) {
		print OUT "<ul>\n";
		foreach $child (reverse @{$OWNS{$owner}}) {
			print OUT "<li>";
			&print_href($child);
			print OUT "</li>\n";
			}
		print OUT "</ul>";
		}
	print OUT "\n";
	}
OUT_flush();
