#
# htmlizer.conf.pl
# J. Uckelman (uckelman@nomic.net)
# 20 February 2001
# 

# Targets for htmlizer.pl
@all = qw(
	Event_Log
	Judgments_Archive
	Judgments_Archive_Index
	Judgments_Live
	Motions_Archive 
	Motions_Archive_Index
	Motions_Live
	Motions_Voting
	Proposals_Archive
	Proposals_Archive_Index
	Proposals_Live
	Proposals_Voting
	Officers_Historical
	Officers_Live
	Officers_Voting
	Roster
	Rules_Categorical
	Rules_Categorical_Index
	Rules_Changes
	Rules_Changes_Index
	Rules_Historical
	Rules_Historical_Index
	Rules_Numerical
	Rules_Numerical_Index
	Standings
);

# Records per page for multipage targets
$Judgments_Archive_perpage = 25;
$Motions_Archive_perpage = 25;
$Proposals_Archive_perpage = 25;
$Rules_Historical_perpage = 100;
$Rules_Numerical_perpage = 100;

# Category hierarchy for Rules_Categorical*
@Rules_Categorical_hierarchy = (
[ "Actions", -1 ],
[ "Agents" , -1 ],
	[ "Players", 1 ],
[ "The Bank", -1 ],
[ "Contests", -1 ],
[ "Contracts", -1 ],
[ "Definitions, Conventions, and Protocols", -1 ],
[ "Fora", -1 ],
[ "Judiciary", -1 ],
[ "Motions", -1 ],
	[ "Proposals", 9 ],
[ "Objects", -1 ],
[ "Offices and Officers", -1 ],
[ "Orders", -1 ],
[ "Scoring", -1 ],
[ "The Game", -1 ],
[ "The Rules", -1 ],
[ "Voting", -1 ],
[ "Winning", -1 ],
[ "Miscellaneous", -1 ]
);

# Target functions
sub Event_Log {
	my $head = interpolate_file("$TPL/standard.header", 'pagetitle', 'Event Log') . "<hr>\n";

	my @data = query("select date, event from event order by date desc, id desc");
	my ($i, $body) = ();
	for $i (0 .. $#data) {
		my ($date, $event) = @{$data[$i]};
		$body .= "$date, $event<br>\n";
	}

	my $foot = "<hr>\n" . interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});
	
	write_file("$WWW/EventLog.html", "$head$body$foot");
	return 'ok';
}

sub Rules_Changes {
	my @page = query("select distinct truncate((unix_timestamp(start)-970444800)/864000+1, 0) as page from rule order by page");  

	my $page;
	foreach $page (@page) {
		my $head = interpolate_file("$TPL/paged.header", 'pagetitle', "Ruleset Changes, Nweek $page"); 	
		my $links = "<center>\n[ " . (join ' | ', links($page, $page, "<a href=\"Rules.Changes.\$page.html\">\$page</a>", @page)) . " ]</center>\n<hr>\n";

		my @data = query("select rule.number, rule.revision, rule.title, rule.text from rule where start between from_unixtime(($page-1)*864000+970444800) and from_unixtime($page*864000+970444800) order by start desc, rule.number desc, rule.revision desc");
		my ($i, $j, $body) = ();
		for $i (0 .. $#data) {
			my ($number, $revision, $title, $text) = @{$data[$i]}[0 .. 3];

			my @history = query("select rule0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from rule0history, event where rule0history.number = $number and rule0history.revision = $revision and rule0history.id = event.id order by event.date");
			my $history = '';
			for $j (0 .. $#history) {
				my ($hrevision, $event, $date) = @{$history[$j]};
				$history .= "$hrevision. $event, $date<br>\n";
			}
			$history = "<font size=-1>\n$history</font>\n<hr>";

			$body .= "<a name = \"$number.$revision\"><b>Rule $number/$revision : $title</b>\n<blockquote>$text</blockquote>\n$history\n";
		}

		$body = add_break_tags($body);

		my $foot = interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

		write_file("$WWW/Rules.Changes.$page.html", "$head$links$body$links$foot");
	}

	return 'ok';
}

sub Rules_Changes_Index {
	my $head = interpolate_file("$TPL/standard.header", 'pagetitle', 'Rules Changes Index') . "<hr>\n";

	my @data = query("select start, truncate((unix_timestamp(start)-970444800)/864000, 0), number, revision, title from rule order by start desc, number desc, revision desc");

	my ($i, $body) = ();
	for $i (0 .. $#data) {
		my ($start, $page, $number, $revision, $title) = @{$data[$i]};
		$body .= "$start <a href=\"Rules.Changes.$page.html#$number.$revision\">$number/$revision</a>: $title<br>\n";
	}

	my $foot = "<hr>\n" . interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Rules.Changes.Index.html", "$head$body$foot");
	return 'ok';
}

#------------------------------------------------------------------------------------------
sub Judgments_Archive {
	my @page = (split / /, single_query("select distinct ceiling(number/$Judgments_Archive_perpage)as page from judgment", "\$page "));

	my $page;
	foreach $page (@page) {
		my $from = ($page-1)*$Judgments_Archive_perpage+1;
		my $to = $page*$Judgments_Archive_perpage;
		my $out = interpolate_file("$TPL/paged.header", 'pagetitle', "Judgments Archive, $from-$to") . 
		"<center>\n" .
		page_links_old($page, "[Judgments $from-$to]", "[<a href=\"Judgments.Archive.\$page.html\">Judgments \@{[(\$page-1)*$Judgments_Archive_perpage+1]}-\@{[\$page*$Judgments_Archive_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n";

		my $body = history_number_query("select judgment.number, judgment.revision, if(judgment.vitality, ' : active', '') as activity, judgment.statement, judgment.ruling, judgment.p_analysis, judgment.j_analysis from judgment where number between $from and $to order by judgment.number, judgment.revision", "<a name = \"\$number.\$revision\"><b>Judgment \$number/\$revision\$activity</b>\n<p>Statement:\n<blockquote>\$statement</blockquote>\n<p>Ruling:\n<blockquote>\$ruling</blockquote>\n<p>Plaintiff's Analysis:\n<blockquote>\$p_analysis</blockquote>\n<p>Judge's Analysis and Orders:\n<blockquote>\$j_analysis</blockquote>\n\$history\n", "select judgment0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from judgment0history, event where judgment0history.number = \$number and judgment0history.id = event.id order by event.date", "\$revision. \$event, \$date<br>\n", 'history', "<font size=-1>\n", "</font>\n<hr>", "select max(revision) as maxrev from judgment where number = \$number", "\$maxrev", 'revision');
		$body =~ s/([^>])\n([^<])/$1<br>\n$2/gs;
		$body =~ s/\n{2}/\n\n<p>/gs;

		$out .= $body .
		"<center>\n" .
		page_links_old($page, "[Judgments $from-$to] ", "[<a href=\"Judgments.Archive.\$page.html\">Judgments \@{[(\$page-1)*$Judgments_Archive_perpage+1]}-\@{[\$page*$Judgments_Archive_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n" .
		interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

		write_file("$WWW/Judgments.Archive.$page.html", $out);
	}

	return 'ok';
}

sub Judgments_Archive_Index {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Judgments Archive Index') .
	"<hr>\n" .
	single_query("select judgment.number, judgment.revision, ceiling(number/$Judgments_Archive_perpage) as page, judgment.statement, judgment.ruling from judgment order by judgment.number, judgment.revision", "<a href=\"Judgments.Archive.\$page.html#\$number.\$revision\">\$number/\$revision</a>: \$statement : <i>\$ruling</i><br>\n") .
	"<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Judgments.Archive.Index.html", $out);
	return 'ok';
}

sub Judgments_Live {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Live Judgments') .
	"<hr>\n"; 

	my $body = history_all_query("select judgment.number, judgment.revision, judgment.statement, judgment.ruling, judgment.p_analysis, judgment.j_analysis from judgment where vitality order by judgment.number, judgment.revision", "<a name = \"\$number.\$revision\"><b>Judgment \$number/\$revision</b>\n<p>Statement:\n<blockquote>\$statement</blockquote>\n<p>Ruling:\n<blockquote>\$ruling<\blockquote>\n<p>Plaintiff's Analysis:\n<blockquote>\$p_analysis</blockquote>\n<p>Judge's Analysis and Orders:\n<blockquote>\$j_analysis</blockquote>\n<font size=-1>\n\$history</font>\n<hr>\n", "select judgment0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from judgment0history, event where judgment0history.number = \$number and judgment0history.id = event.id order by event.date", "\$revision. \$event, \$date<br>\n", 'history');
	$body =~ s/([^>])\n([^<])/$1<br>\n$2/gs;
	$body =~ s/\n{2}/\n\n<p>/gs;

	$out .= $body .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Judgments.Live.html", $out);
	return 'ok';
}

sub Motions_Archive {
	my @page = (split / /, single_query("select distinct ceiling(number/$Motions_Archive_perpage)as page from motion", "\$page "));

	my $page;
	foreach $page (@page) {
		my $from = ($page-1)*$Motions_Archive_perpage+1;
		my $to = $page*$Motions_Archive_perpage;
		my $out = interpolate_file("$TPL/paged.header", 'pagetitle', "Motions Archive, $from-$to") . 
		"<center>\n" .
		page_links_old($page, "[Motions $from-$to]", "[<a href=\"Motions.Archive.\$page.html\">Motions \@{[(\$page-1)*$Motions_Archive_perpage+1]}-\@{[\$page*$Motions_Archive_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n";

		my $body = history_number_query("select motion.number, motion.revision, if(isnull(motion.end), concat(' : ', elt(motion.activity+1, 'inactive', 'active', 'withdrawn')), '') as activity, motion.title, motion.text from motion where number between $from and $to order by motion.number, motion.revision", "<a name = \"\$number.\$revision\"><b>Motion \$number/\$revision : \$title\$activity</b>\n<blockquote>\$text</blockquote>\n\$history\n", "select motion0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from motion0history, event where motion0history.number = \$number and motion0history.id = event.id order by event.date", "\$revision. \$event, \$date<br>\n", 'history', "<font size=-1>\n", "</font>\n<hr>", "select max(revision) as maxrev from motion where number = \$number", "\$maxrev", 'revision');
		$body =~ s/([^>])\n([^<])/$1<br>\n$2/gs;
		$body =~ s/\n{2}/\n\n<p>/gs;

		$out .= $body .
		"<center>\n" .
		page_links_old($page, "[Motions $from-$to] ", "[<a href=\"Motions.Archive.\$page.html\">Motions \@{[(\$page-1)*$Motions_Archive_perpage+1]}-\@{[\$page*$Motions_Archive_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n" .
		interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

		write_file("$WWW/Motions.Archive.$page.html", $out);
	}

	return 'ok';
}

sub Motions_Archive_Index {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Motions Archive Index') .
	"<hr>\n" .
	single_query("select number, revision, title, ceiling(number/$Motions_Archive_perpage) as page from motion order by number, revision", "<a href=\"Motions.Archive.\$page.html#\$number.\$revision\">\$number/\$revision</a>: \$title<br>\n") .
	"<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});
	
	write_file("$WWW/Motions.Archive.Index.html", $out);
	return 'ok';
}

sub Motions_Live {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Live Motions') .
	"<hr>\n";

	my $body = history_all_query("select motion.number, motion.revision, if(motion.activity = 1, 'active', 'inactive') as activity, motion.title, motion.text from motion where isnull(motion.end) order by number, revision", "<a name = \"\$number.\$revision\"><b>Motion \$number/\$revision : \$title : \$activity</b>\n<blockquote>\$text</blockquote>\n\<font size=-1>\n\$history</font>\n<hr>\n", "select motion0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from motion0history, event where motion0history.number = \$number and motion0history.id = event.id order by event.date", "\$revision. \$event, \$date<br>\n", 'history');
	$body =~ s/([^>])\n([^<])/$1<br>\n$2/gs;
	$body =~ s/\n{2}/\n\n<p>/gs;

	$out .= $body .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Motions.Live.html", $out);
	return 'ok';
}

sub Motions_Voting {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Motions Voting') .
	"<hr>\n<pre>" .
	(voting_summary("select distinct date from motion0voting order by date", "\$date\n", 'binary', "select max(length(concat(number, '/', revision))) as length from motion0voting where date = '\$date'", "select distinct name.name from name, motion0voting where name.id = motion0voting.id and motion0voting.date = '\$date' and name.start <= '\$date' and (isnull(name.end) or '\$date' <= name.end) order by name.name", 3, "select distinct number, revision from motion0voting where date = '\$date' order by number, revision", "\$number/\$revision", "\n", "select motion0voting.vote from motion0voting, name where motion0voting.date = '\$date' and motion0voting.id = name.id and name.start <= '\$date' and (isnull(name.end) or '\$date' <= name.end) and motion0voting.number = \$number and motion0voting.revision = \$revision order by motion0voting.number, motion0voting.revision, name.name", "\$column_fill\$vote", "", "") or '') .
	"</pre>\n<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});
	write_file("$WWW/Motions.Voting.html", $out);
	return 'ok';
}

sub Officers_Historical {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Officers Past') .
	"<hr>\n" .
	list_query("select id, office from office order by office", "<b>\$office</b>\n<ol>\n", "</ol>\n", "select if(officer.vacant, 'vacant', name.name) as name, date_format(officer.start, '%d %b %Y %T') as start, ifnull(date_format(officer.end, '%d %b %Y %T'), 'present') as end from officer, office, name where name.id = officer.id and office.id = officer.office and officer.office = \$id and ((officer.end between name.start and name.end) or (isnull(name.end) and (name.start <= officer.end or isnull(officer.end)))) order by officer.start", "<li>\$name, \$start - \$end</li>\n") .
	"<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Officers.Historical.html", $out);
	return 'ok';
}

sub Officers_Live {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Current Officers') .
	"<hr>\n" .
	single_query("select office.office, name.name, date_format(officer.start, '%d %b %Y %T') as start, if(office.elected, concat('term ends ', date_format(date_add(officer.start, interval 50 day), '%d %b %Y %T')), 'no fixed term') as end from office, officer, name where officer.office = office.id and isnull(officer.end) and name.id = officer.id and isnull(name.end) order by office.office", "<b>\$office</b>: \$name, since \$start, \$end<br>\n") .	"<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Officers.Live.html", $out);
	return 'ok';
}

sub Officers_Voting {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Election Voting') .
	"<hr>\n<pre>\n" .
	voting_summary("select distinct date from election0voting order by date", "\$date\n", 'election', "select max(length(office.office)) as length from office, election0voting where date = '\$date' and election0voting.office = office.id", "select distinct name.name from name, election0voting where name.id = election0voting.id and election0voting.date = '\$date' and name.start <= '\$date' and (isnull(name.end) or '\$date' <= name.end) order by name.name", 3, "select distinct office.office, repeat(' ', \$max_issue_length - length(office.office)) as padding from office, election0voting where date = '\$date' and election0voting.office = office.id order by office.office", "\$office\$padding", "\n", "select lpad(election0voting.vote, 2, ' ') as vote from election0voting, name, office where election0voting.date = '\$date' and election0voting.id = name.id and name.start <= '\$date' and (isnull(name.end) or '\$date' <= name.end) and election0voting.office = office.id and office.office = '\$office' order by office.office, name.name", "\$column_fill\$vote", "select distinct name.name, election0voting.vote from election0voting, name where election0voting.date = '\$date' and election0voting.vote = name.id and election0voting.vote <> '-' and name.start <= '\$date' and (isnull(name.end) or '\$date' <= name.end) order by name.name", "\$name, \$vote\n") .
	"</pre>\n<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Elections.Voting.html", $out);
	return 'ok';
}

sub Proposals_Archive {
	my @page = (split / /, single_query("select distinct ceiling(number/$Proposals_Archive_perpage) as page from proposal order by page", '$page '));

	my $page;
	foreach $page (@page) {
		my $from = ($page-1)*$Proposals_Archive_perpage+1;
		my $to = $page*$Proposals_Archive_perpage;
		my $out = interpolate_file("$TPL/paged.header", 'pagetitle', "Proposals Archive, $from-$to") . 
		"<center>\n" .
		page_links_old($page, "[Proposals $from-$to] ", "[<a href=\"Proposals.Archive.\$page.html\">Proposals \@{[(\$page-1)*$Proposals_Archive_perpage+1]}-\@{[\$page*$Proposals_Archive_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n" ; 

		my $body = history_number_query("select proposal.number, proposal.revision, if(isnull(proposal.end), concat(' : ', elt(proposal.activity+1, 'inactive', 'active', 'withdrawn')), '') as activity, proposal.title, proposal.text from proposal where number between $from and $to order by proposal.number, proposal.revision", "<a name = \"\$number.\$revision\"><b>Proposal \$number/\$revision : \$title\$activity</b>\n<blockquote>\$text</blockquote>\n\$history\n", "select proposal0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from proposal0history, event where proposal0history.number = \$number and proposal0history.id = event.id order by event.date", "\$revision. \$event, \$date<br>\n", 'history', "<font size=-1>\n", "</font>\n<hr>", "select max(revision) as maxrev from proposal where number = \$number", "\$maxrev", 'revision');
		$body =~ s/([^>])\n([^<])/$1<br>\n$2/gs;
		$body =~ s/\n{2}/\n\n<p>/gs;

		$out .= $body .
		"<center>\n" .
		page_links_old($page, "[Proposals $from-$to] ", "[<a href=\"Proposals.Archive.\$page.html\">Proposals \@{[(\$page-1)*$Proposals_Archive_perpage+1]}-\@{[\$page*$Proposals_Archive_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n" .
		interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

		write_file("$WWW/Proposals.Archive.$page.html", $out);
	}

	return 'ok';
}

sub Proposals_Archive_Index {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Proposals Archive Index') .
	"<hr>\n" .
	single_query("select number, revision, title, ceiling(number/$Proposals_Archive_perpage) as page from proposal order by number, revision", "<a href=\"Proposals.Archive.\$page.html#\$number.\$revision\">\$number/\$revision</a>: \$title<br>\n") .
	"<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Proposals.Archive.Index.html", $out);
	return 'ok';
}

sub Proposals_Live {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Live Proposals') .
	"<hr>\n"; 

	my $body = history_all_query("select proposal.number, proposal.revision, if(proposal.activity = 1, 'active', 'inactive') as activity, proposal.title, proposal.text from proposal where isnull(proposal.end) order by number, revision", "<a name = \"\$number.\$revision\"><b>Proposal \$number/\$revision : \$title : \$activity</b>\n<blockquote>\$text</blockquote>\n\<font size=-1>\n\$history</font>\n<hr>\n", "select proposal0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from proposal0history, event where proposal0history.number = \$number and proposal0history.id = event.id order by event.date", "\$revision. \$event, \$date<br>\n", 'history');
	$body =~ s/([^>])\n([^<])/$1<br>\n$2/gs;
	$body =~ s/\n{2}/\n\n<p>/gs;

	$out .= $body .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Proposals.Live.html", $out);
	return 'ok';
}

sub Proposals_Voting {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Proposals Voting') .
	"<hr>\n<pre>" .
	voting_summary("select distinct date from proposal0voting order by date", "\$date\n", 'binary', "select max(length(concat(number, '/', revision))) as length from proposal0voting where date = '\$date'", "select distinct name.name from name, proposal0voting where name.id = proposal0voting.id and proposal0voting.date = '\$date' and name.start <= '\$date' and (isnull(name.end) or '\$date' <= name.end) order by name.name", 3, "select distinct number, revision from proposal0voting where date = '\$date' order by number, revision", "\$number/\$revision", "\n", "select proposal0voting.vote from proposal0voting, name where proposal0voting.date = '\$date' and proposal0voting.id = name.id and name.start <= '\$date' and (isnull(name.end) or '\$date' <= name.end) and proposal0voting.number = \$number and proposal0voting.revision = \$revision order by proposal0voting.number, proposal0voting.revision, name.name", "\$column_fill\$vote", "", "") .
	"</pre>\n<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Proposals.Voting.html", $out);
	return 'ok';
}

sub Rules_Categorical {
	my @page = col(0, @Rules_Categorical_hierarchy);

	my $i;
	for $i (0 .. $#page) {
		my $out = interpolate_file("$TPL/paged.header", 'pagetitle', "Categorical Ruleset, $page[$i]") . 
		"<center>\n" .
		page_links_old($page[$i], "[$page[$i]] ", "[<a href=\"Rules.Categorical.\@{[\$i+1]}.html\">\$page</a>] ", @page) .
		"</center>\n<hr>\n";

		my $body = history_all_query("select rule.number, rule.revision, rule.title, rule.text from rule, rule0category, category where isnull(rule.end) and rule.number = rule0category.number and rule.revision = rule0category.revision and rule0category.category = category.id and category.category = '$page[$i]' order by rule.number, rule.revision", "<a name = \"\$number.\$revision\"><b>Rule \$number/\$revision : \$title</b>\n<blockquote>\$text</blockquote>\n<font size=-1>\n\$history</font>\n<hr>\n", "select rule0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from rule0history, event where rule0history.number = \$number and rule0history.id = event.id order by event.date", "\$revision. \$event, \$date<br>\n", 'history');
		$body =~ s/([^>])\n([^<])/$1<br>\n$2/gs;
		$body =~ s/\n{2}/\n\n<p>/gs;

		$out .= $body .
		"<center>\n" .
		page_links_old($page[$i], "[$page[$i]] ", "[<a href=\"Rules.Categorical.\@{[\$i+1]}.html\">\$page</a>] ", @page) .
		"</center>\n<hr>\n" .
		interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

		write_file("$WWW/Rules.Categorical.@{[$i+1]}.html", $out);
	}

	return 'ok';
}

sub Rules_Categorical_Index {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Categorical Ruleset Index') .
	"<hr>\n" .
	list_unpack(-1, "<ul>\n", "<li>\$element</li>\n", "</ul>\n", "select rule.number, rule.revision, rule.title from rule, rule0category, category where isnull(rule.end) and rule.number = rule0category.number and rule.revision = rule0category.revision and rule0category.category = category.id and category.category = '\$category' order by rule.number", "<li><a href=\"Rules.Categorical.\$parent.html#\$number.\$revision\">\$number/\$revision</a>: \$title</li>\n", @Rules_Categorical_hierarchy) .
	"<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Rules.Categorical.Index.html", $out);
	return 'ok';
}

sub Rules_Historical {
	my @page = (split / /, single_query("select distinct ceiling(number/$Rules_Historical_perpage) as page from rule order by page", '$page '));

	my $page;
	foreach $page (@page) {
		my $from = ($page-1)*$Rules_Historical_perpage+1;
		my $to = $page*$Rules_Historical_perpage;
		my $out = interpolate_file("$TPL/paged.header", 'pagetitle', "Historical Ruleset, $from-$to") . 
		"<center>\n" .
		page_links_old($page, "[Rules $from-$to] ", "[<a href=\"Rules.Historical.\$page.html\">Rules \@{[(\$page-1)*$Rules_Historical_perpage+1]}-\@{[\$page*$Rules_Historical_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n";

		my $body = history_number_query("select rule.number, rule.revision, rule.title, rule.text from rule where number between $from and $to order by rule.number, rule.revision", "<a name = \"\$number.\$revision\"><b>Rule \$number/\$revision : \$title</b>\n<blockquote>\$text</blockquote>\n\$history\n", "select rule0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from rule0history, event where rule0history.number = \$number and rule0history.id = event.id order by event.date", "\$revision. \$event, \$date<br>\n", 'history', "<font size=-1>\n", "</font>\n<hr>", "select max(revision) as maxrev from rule where number = \$number", "\$maxrev", 'revision');
		$body =~ s/([^>])\n([^<])/$1<br>\n$2/gs;
		$body =~ s/\n{2}/\n\n<p>/gs;

		$out .= $body .
		"<center>\n" .
		page_links_old($page, "[Rules $from-$to] ", "[<a href=\"Rules.Historical.\$page.html\">Rules \@{[(\$page-1)*$Rules_Historical_perpage+1]}-\@{[\$page*$Rules_Historical_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n" .
		interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

		write_file("$WWW/Rules.Historical.$page.html", $out);
	}

	return 'ok';
}

sub Rules_Historical_Index {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Historical Ruleset Index') .
	"<hr>\n" .
	single_query("select number, revision, title, ceiling(number/$Rules_Numerical_perpage) as page from rule order by number, revision", "<a href=\"Rules.Historical.\$page.html#\$number.\$revision\">\$number/\$revision</a>: \$title<br>\n") .
	"<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Rules.Historical.Index.html", $out);
	return 'ok';
}

sub Rules_Numerical {
	my @page = (split / /, single_query("select distinct ceiling(number/$Rules_Numerical_perpage) as page from rule order by page", '$page '));

	my $page;
	foreach $page (@page) {
		my $from = ($page-1)*$Rules_Numerical_perpage+1;
		my $to = $page*$Rules_Numerical_perpage;
		my $out = interpolate_file("$TPL/paged.header", 'pagetitle',  "Numerical Ruleset, $from-$to") . 
		"<center>\n" .
		page_links_old($page, "[Rules $from-$to] ", "[<a href=\"Rules.Numerical.\$page.html\">Rules \@{[(\$page-1)*$Rules_Numerical_perpage+1]}-\@{[\$page*$Rules_Numerical_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n";

		my $body = history_all_query("select rule.number, rule.revision, rule.title, rule.text from rule where isnull(rule.end) and number between $from and $to order by rule.number, rule.revision", "<a name = \"\$number.\$revision\"><b>Rule \$number/\$revision : \$title</b>\n<blockquote>\$text</blockquote>\n<font size=-1>\n\$history</font>\n<hr>\n", "select rule0history.revision, event.event, date_format(event.date, '%d %b %Y %T') as date from rule0history, event where rule0history.number = \$number and rule0history.id = event.id order by event.date", "\$revision. \$event, \$date<br>\n", 'history');
		$body =~ s/([^>])\n([^<])/$1<br>\n$2/gs;
		$body =~ s/\n{2}/\n\n<p>/gs;

		$out .= $body .
		"<center>\n" .
		page_links_old($page, "[Rules $from-$to] ", "[<a href=\"Rules.Numerical.\$page.html\">Rules \@{[(\$page-1)*$Rules_Numerical_perpage+1]}-\@{[\$page*$Rules_Numerical_perpage]}</a>] ", @page) .
		"</center>\n<hr>\n" .
		interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

		write_file("$WWW/Rules.Numerical.$page.html", $out);
	}

	return 'ok';
}

sub Rules_Numerical_Index {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Numerical Ruleset Index') .
	"<hr>\n" .
	single_query("select number, revision, title, ceiling(number/$Rules_Numerical_perpage) as page from rule where isnull(rule.end) order by number, revision", "<a href=\"Rules.Numerical.\$page.html#\$number.\$revision\">\$number/\$revision</a>: \$title<br>\n") .
	"<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Rules.Numerical.Index.html", $out);
	return 'ok';
}

sub Roster {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Roster') .
	"<hr>\n<b>Current Players</b>\n<p><table border cellspacing=0 cellpadding=2>\n<tr><th>name</th><th>mail</th><th>since</th></tr>\n" .
	single_query("select name.name, mail.address, date_format(player.start, '%d %b %Y %T') as start from player, name, mail where isnull(player.end) and player.id = name.id and name.id = mail.id and isnull(name.end) and isnull(mail.end) order by name.name", "<tr><td>\$name</td><td>\$address</td><td>\$start</td></tr>\n") .
	"</table>\n<p><b>Former Players</b>\n<p><table border cellspacing=0 cellpadding=2>\n<tr><th>name</th><th>mail</th><th>from</th><th>to</th></tr>\n" .
	single_query("select name.name, mail.address, date_format(player.start, '%d %b %Y %T') as start, date_format(player.end, '%d %b %Y %T') as end from player, name, mail where player.end and player.id = name.id and name.id = mail.id and isnull(name.end) and isnull(mail.end) order by name.name", "<tr><td>\$name</td><td>\$address</td><td>\$start</td><td>\$end</td></tr>\n")  .
	"</table>\n<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	$out =~ s/@/ @ /gs;

	write_file("$WWW/Roster.html", $out);
	return 'ok';
}

sub Standings {
	my $out = interpolate_file("$TPL/standard.header", 'pagetitle', 'Standings') .
	"<hr>\n<em>NOTE: These are, at present, completely inaccurate.</em>\n<p><table border cellspacing=0 cellpading=0><col><col align=\"right\">\n<tr><th>name</th><th>score</th></tr>\n" .
	single_query("select name.name, entity.score from entity, name where name.id = entity.id and isnull(name.end) order by entity.score desc", "<tr><td>\$name</td><td>\$score</td></tr>\n") .
	"</table>\n<hr>\n" .
	interpolate_file("$TPL/standard.footer", 'pagedate', @{[`date -u '+%a %d %b %Y %T %Z'`]});

	write_file("$WWW/Standings.html", $out);
	return 'ok';
}

# Do not remove the last line. File must return true.
1
