#!/usr/bin/perl -w
# vim: set sw=4 ts=4 si et:
# Copyright: GPL, Author: Guido Socher
# $Revision: $, last changed: $Date: $
#
use strict;
use vars qw($opt_h);
use Getopt::Std;
sub person2ref($);
#
getopts("a:h")||die "ERROR: No such option. -h for help.n";
help() if ($opt_h);
my @dat;
my %a2issue;
my @issues;
my $tmp="";
open(IDB,"issuedb.txt")||die;
while(<IDB>){
	chomp;
	next unless(/^=0/);
	@dat=split(/\s*\-\+\-\s*/,$_);
	if ($dat[1]){
		$a2issue{$dat[1]}=$dat[2];
	}
	if ($dat[2] ne $tmp){
		$tmp=$dat[2];
		push(@issues,$tmp);
	}
}
close IDB;
my %article;
my %person;
my @dat2;
#
open(IDB,"maindb.txt")||die;
while(<IDB>){
	chomp;
	next unless(/^=/);
	@dat=split(/\s*\-\+\-\s*/,$_);
	if ($dat[0] eq "=1"){
		$dat2[2]=~s/ +//;
		$article{"article$dat2[1]"}->{'theme'}=$dat2[2];
		$dat2[3]=~s/\~/-/g;
		$dat2[3]=~s/\& /\&amp; /g;
		$article{"article$dat2[1]"}->{'title'}=$dat2[3];
		if ($a2issue{$dat2[1]}){
			$article{"article$dat2[1]"}->{'issueref'}=$a2issue{$dat2[1]};
			my $path="../../English/".$a2issue{$dat2[1]};
			my $filet="target";
			my $file="article$dat2[1].html";
			if ( -f "$path/article$dat2[1].meta.shtml"){
				$file="article$dat2[1].meta.shtml";
				$filet="meta";
			}
			if ( -f "$path/article$dat2[1].meta.html"){
				$file="article$dat2[1].meta.html";
				$filet="meta";
			}
			if ( -f "$path/article$dat2[1].shtml"){
				$file="article$dat2[1].shtml";
				$filet="target";
			}
			$article{"article$dat2[1]"}->{'filet'}=$filet;
			$article{"article$dat2[1]"}->{'file'}=$file;
		}else{
            $article{"article$dat2[1]"}->{'filet'}="";
            $article{"article$dat2[1]"}->{'file'}="";
			$article{"article$dat2[1]"}->{'issueref'}="none";
        }
		if ($dat[1]){
			$dat[1]=~s/\~/\-/g;
            $dat[1]=~s/\& /\&amp; /g;
			$dat[1]=~s/<\/?\w+>//g;
            $article{"article$dat2[1]"}->{'abstract'}=$dat[1];
		}else{
                $article{"article$dat2[1]"}->{'abstract'}="";
        }
		my $p=person2ref($dat2[5]);
        $dat2[4]=~s/^\s+//;
        $dat2[4]=~s/\s+$//;
        $dat2[5]=~s/^\s+//;
        $dat2[5]=~s/\s+$//;
        $dat2[5]=~s/\s+/ /g;
		$person{$p}->{'htmlname'}=$dat2[5];
		$person{$p}->{'asciiname'}=$dat2[5];
        $person{$p}->{'asciiname'}=~s/&(\w)\w+;/$1/g;
        $person{$p}->{'asciiname'}=~s/[^a-zA-Z \.\-]/*/g;
		$person{$p}->{'email'}=$dat2[4];
		$article{"article$dat2[1]"}->{'personref'}=$p;
	}else{
		@dat2=@dat;
	}
}
close IDB;
#
print "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>
<!DOCTYPE database SYSTEM \"xmldb.dtd\">
<!-- vim: set sw=2 ts=2 et tw=80: -->
<database>\n";

print "  <persons>\n";
foreach my $pp (sort keys %person){
	print "   <person id=\"$pp\">\n";
	print "     <asciiname>".$person{$pp}->{'asciiname'}."</asciiname>\n";
	print "     <htmlname>".$person{$pp}->{'htmlname'}."</htmlname>\n";
	print "     <email>".$person{$pp}->{'email'}."</email>\n";
	print "   </person>\n";
}
print "  </persons>\n";
print "<!-- ================ -->\n";
my %m2d=('January'=>'01','February'=>'02','March'=>'03','April'=>'04',
    'May'=>'05','June'=>'06','July'=>'07','August'=>'08','September'=>'09',
    'October'=>'10','November'=>'11','December'=>'12');
print "  <issues>\n";
	print "   <issue id=\"none\" code=\"999999\">\n";
	print "     <status>open</status>\n";
	print "   </issue>\n";
foreach my $pp (@issues){
	my $c="999999";
	if ($pp=~/([A-Za-z]+)(\d+)/){
		$c=$2 . $m2d{$1};
	}
	print "   <issue id=\"$pp\" code=\"$c\">\n";
	print "     <status>published</status>\n";
	print "   </issue>\n";
}
print "  </issues>\n";
print "<!-- ================ -->\n";
print "  <articles>\n";
my $pp;
foreach my $p (1..300){
    next unless ($article{"article$p"});
    $pp="article$p";
	print "   <article id=\"$pp\" numid=\"".sprintf("%04d",$p)."\">\n";
	print "     <title>".$article{"$pp"}->{'title'}."</title>\n";
	print "     <personref href=\"".$article{"$pp"}->{'personref'}."\"/>\n";
	print "     <theme id=\"".$article{"$pp"}->{'theme'}."\"/>\n";
	print "     <issueref href=\"".$article{"$pp"}->{'issueref'}."\"/>\n";
	print "     <file type=\"".$article{"$pp"}->{'filet'}."\">".$article{"$pp"}->{'file'}."</file>\n" if ($article{"$pp"}->{'filet'});
	print "     <abstract>".$article{"$pp"}->{'abstract'}."</abstract>\n";
	print "   </article>\n";
}
print "  </articles>\n";
print "<!-- ================ -->\n";
print "\n</database>\n";
#
#
sub person2ref($){
	my $name=shift;
	$name=~s/&(\w)\w+;/$1/g;
	$name=~tr/A-Z/a-z/;
	if ($name=~/(\w\w)\w+\s*.*?\s+(\w\w)/){
		return("$1_$2");
	}else{
		return("strangename");
	}
}
#
sub help{
print "help\n";
exit;
}
__END__ 

