#! /usr/bin/perl

use strict;
use XML::DOM;
use dutil;

&main(@ARGV);

sub main {
    my @files = @_;

    my $parser = new XML::DOM::Parser;

    my @items = ();
    foreach my $file (@files) {
	&parse_one_month($parser, $file, \@items);
    }
    @items = reverse @items;

    &print_header("日誌");
    &print_fronter(\@items);

    foreach my $item (@items) {
	print $item->{str};
    }

    &print_footer();
}

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

    print <<"HEAD";
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"> 
<html lang="ja">
<head>
  <link rev="MADE" href="mailto:AE5T-KSN\@asahi-net.or.jp">
  <link rel="INDEX" href="./">
  <link rel="StyleSheet" href="d/d.css">
  <meta http-equiv="Content-Type" content="text/html; charset=ISO-2022-JP">
  <meta http-equiv="Content-Style-Type" content="text/css">
  <title>$title</title>
</head>
<body>
<p class="note">[<a href="./">トップ</a>][現在][<a href="d/">過去</a>]</p>
<h1>$title</h1>
HEAD
}

sub print_footer {
    print <<"FOOT";
<p class="note">[<a href="./">トップ</a>][現在][<a href="d/">過去</a>]</p>
</body>
</html>
FOOT
}

sub print_fronter {
    my $items = shift;

    my ($y1, $m1, $d1) = ($$items[0]->{year},
			  $$items[0]->{month}, $$items[0]->{mday});
    my ($y2, $m2, $d2) = ($$items[$#$items]->{year},
			  $$items[$#$items]->{month}, $$items[$#$items]->{mday});

    printf("<p class=\"date\">%04d/%d/%d 〜%04d/%d/%d</p>\n",
	   $y2, $m2, $d2, $y1, $m1, $d1);
}

sub parse_one_month {
    my ($parser, $file, $items) = @_;

    my $doc = $parser->parsefile ($file);
    
    my $root = $doc->getDocumentElement();
    
    my $year = $root->getAttribute("year");
    my $month = $root->getAttribute("month");

    foreach my $day (sort bymday $root->getElementsByTagName("day")) {
	my $out = '';
	my $mday = $day->getAttribute("mday");
	if ($day->getAttribute("from") > 0) {
	    $out .= sprintf("<h2%s><a name=\"%02d\">%d/%d - %d</a></h2>\n",
			    &getclass($year, $month, $mday,
				      $day->getAttribute("class")),
			    $mday,
			    $month, $day->getAttribute("from"),
			    $day->getAttribute("to"));
	} else {
	    $out .= sprintf("<h2%s><a name=\"%02d\">%d/%d</a></h2>\n",
			    &getclass($year, $month, $mday,
				      $day->getAttribute("class")),
			    $mday,
			    $month, $mday);
	}
	$out .= "  <ul>\n";
	my $idx = 1;
	foreach my $item ($day->getChildNodes) {
	    next if ($item->getNodeType != XML::DOM::ELEMENT_NODE ||
		     $item->getNodeName ne 'li');
	    my $nameval = sprintf "%02d-%02d", $mday, $idx++;

	    my $text = $doc->createTextNode(".");
	    my $name = $doc->createElement("a");
	    $name->setAttribute("name", $nameval);
	    $name->setAttribute("href",
				sprintf("d/%04d%02d.html#%s",
					$year, $month, $nameval));
	    $name->appendChild($text);
	    $item->insertBefore($name, $item->getFirstChild);
	    #my $val = $item->expandEntityRefs($item->toString());
	    my $val = $item->toString();
	    $out .= sprintf "  %s\n", $val;
	}
	$out .= "  </ul>\n";

	if (@$items >= 7) {
	    shift @$items;
	}
	push @$items, {year=>$year, month=>$month, mday=>$mday, str=>$out};
    }
}

sub bymday {
    return $a->getAttribute("mday") <=> $b->getAttribute("mday");
}

# end of script
# Local Variables:
# mode:perl-mode
# coding:utf-8
# End:
