#!/usr/bin/perl -w

print <<END;
<?xml version="1.0"?>
<!DOCTYPE set PUBLIC "-//Norman Walsh//DTD DocBk XML V3.1.4//EN"
	"file:///usr/share/xml/docbook/schema/dtd/4.4/docbookx.dtd">

<article>

<articleinfo xmlns:xi="http://www.w3.org/2001/XInclude">
  <t>Change log</t>
  <abstract>This article contains detailed change information for each release.</abstract>
</articleinfo>

<changelog>
END

sub flush_words {

	if (!defined $_[0]) {
		return;
	}

	$_[0] =~ s/\&/\&amp;/gs;
	$_[0] =~ s/</\&lt;/gs;

	print "<entry><text>$_[0]</text>\n";

	@keywords = split /\W/, $_[0];

	foreach (@keywords) {
		print "<word>" . lc $_ . "</word>";
	}
	
	print "</entry>\n";
}

sub unset_version {
	if (defined $version) {
		print "</release>";
	}
}

sub set_version {

	#
	# Initialize variables and trim whitespace.
	#

	chomp($version = $_[0]);
	$version =~ s/^\s*(.*)\s*$/$1/;

	#
	# Check the version style for included date
	#

	if ($version =~ /(\S+)\s*(2\d\d\d)(\S*)/) {

		#
		# Handle the date, if available, and add a rudimentary
		# edit history.
		#

		$version = $1;
		$year = $2;
		$date = $year . $3;

		print "<release version=\"$version\" date=\"$date\">\n";

		print "<edit by=\"David Hilvert\" in-year=\"$year\"/>";

	} else {

		#
		# Handle the case where the date is not available
		#

		print "<release version=\"$version\">\n";

	}
}

sub flush_path {

	if (!defined @path) {
		return;
	}

	foreach $element (reverse(@path)) {
		if ($element =~ /\S/) {
			print "</$element>";
		}
	}

	undef @path;
}

sub set_path {
	chomp(@path = split /\//, $_[0]);
	foreach $element (@path) {
		if ($element =~ /\S/) {
			print "<$element>";
		}
	}
}


while(<STDIN>) {

	if (/^\S/) {

		##
		###If a non-whitespace character begins a line, this means that any
		###open entry must be closed.
		###

		flush_words($entry);
		undef $entry;
	}

	if (/^o\t/) {

		##
		###Entry
		###

		$entry = $_;
		$entry =~ s/^o\t//;

	} elsif (/^\t/) {
		
		##
		###Entry continuation
		###

		if (defined $entry) {
			$entry .= $_;
		}

	} elsif (/^\//) {

		##
		###Path
		###

		$newpath = $_;
		flush_words($entry);
		flush_path();
		set_path($newpath);

	} elsif (/^V/) {

		##
		###Version
		###

		$newversion = $_;
		$newversion =~ s/V//;

		$newpath= $_;
		flush_path();
		unset_version();
		set_version($newversion);

	} elsif (/^E/) {

		##
		###Edit note
		###

		 s/E//;
		 print;

	}
}

flush_words($entry);
flush_path();
unset_version();

print "</changelog></article>";
