#!/usr/bin/perl

# Automatic generation of years for http://fr.wikipedia.org
# (c) Yann Forget
# Release under GPL, see http://www.gnu.org/

use Lingua::FR::Numbers qw(number_to_fr);

@centuries=(2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20);

@deceny=(0,1,2,3,4,5,6,7,8,9);

@codes=("en:","de:","pl:","eo:","es:","nl:","sv:","pt:","no:","da:");

@romains=
(
    "",  # For indice 0
    "I",
    "II",
    "III",
    "IV",
    "V",
    "VI",
    "VII",
    "VIII",
    "IX",
    "X",
    "XI",
    "XII",
    "XIII",
    "XIV",
    "XV",
    "XVI",
    "XVII",
    "XVIII",
    "XIX",
    "XX",
    "XXI",
    "XXII",
    "XXIII",
    "XXIV",
    "XXV",
    "XXVI",
    "XXVII",
    "XXVIII",
    "XXIX",
    "XXX"
);

sub main() {
foreach $century (@centuries) {				# centuries: IIIe au XXe

	$format_century="<center>\n";
	$format_century.="[[$romains[$century]e si?cle|$romains[$century]<sup>e</sup> si?cle]] | \n";
	$format_century.="<b>[[$romains[$century+1]e si?cle|$romains[$century+1]<sup>e</sup> si?cle]]</b> | \n";
	$format_century.="[[$romains[$century+2]e si?cle|$romains[$century+2]<sup>e</sup> si?cle]]\n";
	$format_century.="</center>\n\n";

	#print $century," ",$format_century;
	foreach $deceny (@deceny) {

		foreach $year (@deceny) {			# Years
			$dec=$century.$deceny."0";
			&format_dec($dec);

			$y=$century.$deceny.$year;
			&format_year($y);
			$format_links=&format_links($y);

			$file="$y.txt";
			
			&print_file($file);
		} # End year
	} # End deceny
} # End century
} # End main

# Ier si?cle
sub Ier() {
$format_century="<center>\n";
$format_century.="[[Ier si?cle av. J.-C.|I<sup>er</sup> si?cle av. J.-C.]] | \n";
$format_century.="<b>[[Ier si?cle|I<sup>er</sup> si?cle]]</b> | \n";
$format_century.="[[IIe si?cle|II<sup>e</sup> si?cle]]\n";
$format_century.="</center>\n\n";

foreach $deceny (@deceny) {

	foreach $year (@deceny) {			# Years
		$century="";
		$dec=$century.$deceny."0";
		&format_dec($dec);

		$y=$century.$deceny.$year;
		&format_year($y);
		$format_links=&format_links($y);

		$file="$y.txt";

		&print_file($file);
	}
}
} # End Ier

# IIe si?cle
sub IIe() {
$format_century="<center>\n";
$format_century.="[[Ier si?cle|I<sup>er</sup> si?cle]] | \n";
$format_century.="<b>[[IIe si?cle|II<sup>e</sup> si?cle]]</b> | \n";
$format_century.="[[IIIe si?cle|III<sup>e</sup> si?cle]]\n";
$format_century.="</center>\n\n";

foreach $deceny (@deceny) {

	foreach $year (@deceny) {			# Years
		$century="1";
		$dec=$century.$deceny."0";
		&format_dec($dec);

		$y=$century.$deceny.$year;
		&format_year($y);
		$format_links=&format_links($y);

		$file="$y.txt";

		&print_file($file);
	}
}
} # End IIe

# Ier si?cle av. J.-C.
sub Ier_av_JC() {
$format_century="<center>\n";
$format_century.="[[IIe si?cle av. J.-C.|II<sup>e</sup> si?cle av. J.-C.]] | \n";
$format_century.="<b>[[Ier si?cle av. J.-C.|I<sup>er</sup> si?cle av. J.-C.]]</b> | \n";
$format_century.="[[Ier si?cle|I<sup>er</sup> si?cle]]\n";
$format_century.="</center>\n\n";

foreach $deceny (@deceny) {

	foreach $year (@deceny) {			# Years
		$century="-";
		$dec=$century.$deceny."0";
		&format_dec($dec);

		$y=$century.$deceny.$year;
		&format_year($y);

		$file="$y.txt";

		&print_file($file);
	}
}
} # End Ier av JC

sub decenie() {

foreach $century (@centuries)  {
	foreach $deceny (@deceny) {

		$format_century="<center>\n";
		$format_century.="[[$romains[$century-1]e si?cle|$romains[$century-1]<sup>e</sup> si?cle]] | \n";
		$format_century.="<b>[[$romains[$century]e si?cle|$romains[$century]<sup>e</sup> si?cle]]</b> | \n";
		$format_century.="[[$romains[$century+1]e si?cle|$romains[$century+1]<sup>e</sup> si?cle]]\n";
		$format_century.="</center>\n\n";

		$dec=($century-1).$deceny."0";
		$format_dec="<center>\n";
		$format_dec.="[[Ann?es ".($dec-20)."]] | [[Ann?es ".($dec-10)."]] | <b>Ann?es ";
		$format_dec.=$dec."</b> | [[Ann?es ".($dec+10)."]] | [[Ann?es ".($dec+20)."]]\n";
		$format_dec.="</center>\n\n";

		$y=$dec;
		$format_years="<center>\n";
		$format_years.="[[".($y)."]] | [[".($y+1)."]] | [[".($y+2)."]] | [[".($y+3)."]] | [[";
		$format_years.=($y+4)."]] | [[".($y+5)."]] | [[".($y+6)."]] | [[".($y+7)."]] | [[".($y+8);
		$format_years.="]] | [[".($y+9)."]]\n";
		$format_years.="</center>\n\n";

		my $y9=$y+9;
		$format_links="[[en:${y}s]] [[de:${y}er]] [[pl:Lata $y-$y9]]";
		$format_links.=" [[eo:$y-oj]] [[es:${y}s]] [[sv:$y-talet]]";
		$format_links.=" [[no:$y-tallet]] [[da:$y\'erne]]\n";
		# Doesn't work
		# my $d1=$deceny+1;
		# $format_links.="[[nl:$y-${d1}0]]\n";

		$file="an-".$dec.".txt";
		
		print $file,"\n";
		open YEAR, ">$file" or die "Can't create $file: $!";

		print YEAR $format_links;
		print YEAR $format_century;
		print YEAR $format_dec;
		print YEAR $format_years;

		print YEAR "----\n";

		print YEAR "=== ?v?nements ===\n\n";
		print YEAR "*\n\n";
		print YEAR "=== Personnages significatifs ===\n\n";
		print YEAR "*\n\n";
		print YEAR "=== Inventions, D?couvertes, Introductions ===\n\n";
		print YEAR "*\n";
			
		close YEAR;

		}
	}
} # End decenie

sub print_file() {
	my $file = shift;

	print $file,"\n";
	open YEAR, ">$file" or die "Can't create $file: $!";

	print YEAR $format_links;
	print YEAR $format_century;
	print YEAR $format_dec;
	print YEAR $format_years;

	print YEAR "----\n";
	print YEAR "<i>Cette page concerne l'ann?e ".$y." du [[calendrier gr?gorien]]. ";
	print YEAR "Pour le nombre, voyez [[".number_to_fr($y)."]].</i>\n\n";

	print YEAR "=== ?v?nements ===\n\n";
	print YEAR "*\n\n";
	print YEAR "=== Naissances ===\n\n";
	print YEAR "*\n\n";
	print YEAR "=== D?c?s ===\n\n";
	print YEAR "*\n";
			
	close YEAR;
}

sub format_year() {
	my $y = shift;

	$format_years="<center>\n";
	$format_years.="[[".($y-5)."]] | [[".($y-4)."]] | [[".($y-3)."]] | [[".($y-2)."]] | [[";
	$format_years.=($y-1)."]] | <b>".$y."</b> | [[".($y+1)."]] | [[".($y+2)."]] | [[".($y+3);
	$format_years.="]] | [[".($y+4)."]] | [[".($y+5)."]]\n";
	$format_years.="</center>\n\n";
}

sub format_dec() {
	my $dec = shift;

	$format_dec="<center>\n";
	$format_dec.="[[Ann?es ".($dec-20)."]] | [[Ann?es ".($dec-10)."]] | <b>[[Ann?es ";
	$format_dec.=$dec."]]</b> | [[Ann?es ".($dec+10)."]] | [[Ann?es ".($dec+20)."]]\n";
	$format_dec.="</center>\n\n";
}

sub format_links() {
	my $y = shift;
	my $format_links="";

	foreach $l (@codes) {
		
		$format_links.="[[$l$y]]";
	}
	$format_links.="\n";
	return($format_links);
}

&decenie;
#&Ier_av_JC;
#&Ier;
#&IIe;
#&main;

print "OK\n";

