package Gruntmaster::Page;

use 5.014000;
use strict;
use warnings;

use Fcntl qw/:flock/;
use File::Basename qw/fileparse/;
use File::Path qw/make_path/;
use File::Slurp qw/read_file write_file/;
use IO::Compress::Gzip qw/gzip/;
use IO::File;
use Gruntmaster::Data;

our $VERSION = '0.001';
our @generators;

use constant LANGUAGES => [ 'en' ];
use constant CONTENT_TYPES => {
	html => 'text/html; charset=UTF-8',
	txt => 'text/plain; charset=UTF-8',
};

sub declaregen{
	my ($generator, $regex) = @_;
	$generator = "Gruntmaster::Page::$generator";
	eval "require $generator";
	push @generators, [$regex,  $generator];
}

{
	my $component = qr'[^/]+';
	my $contest = qr,(?:ct/$component/)?,;
	declaregen Us				=> qr,^us/index$,;
	declaregen 'Us::Entry'		=> qr,^us/$component$,;
	declaregen Ct				=> qr,^ct/index$,;
	declaregen 'Ct::Entry'		=> qr,^ct/$component/index$,;
	declaregen St				=> qr,^${contest}log/st$,;
	declaregen Log				=> qr,^${contest}log/(?:\d+|index)$,;
	declaregen 'Log::Entry'	=> qr,^${contest}log/job/$component$,;
	declaregen Submit			=> qr,^${contest}submit$,;
	declaregen Pb				=> qr,^${contest}pb/index$,;
	declaregen 'Pb::Entry'		=> qr,^${contest}pb/$component$,;
}

sub generate{
	my ($path, $topic) = @_;
	my ($path_noext, $ext) = $path =~ m/^(.*)\.(.*)$/;
	my ($basename, $directories) = fileparse $path_noext;
	make_path $directories;

	IO::File->new(">$path_noext.var")->close unless -f "$path_noext.var";
	flock my $lockfh = IO::File->new("<$path_noext.var"), LOCK_EX;
	open my $typemap, ">$path_noext.var.new";
	say $typemap "URI: $basename\n";

	my $fill_typemap = sub {
		for my $lang (@{LANGUAGES()}) {
			my $page = $_[0]->($lang);
			write_file "$path_noext.$lang.$ext.new", $page;
			say $typemap "URI: $basename.$lang.$ext\nContent-Language: $lang\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
			gzip \$page => "$path_noext.$lang.gz.$ext.new", Minimal => 1;
			say $typemap "URI: $basename.$lang.gz.$ext\nContent-Language: $lang\nContent-Encoding: gzip\nContent-Type: " . CONTENT_TYPES->{$ext} . "\n";
		}
	};

	if ($topic eq 'genpage') {
		for my $gen (@generators) {
			my ($regex, $generator) = @$gen;
			next unless $path_noext =~ $regex;
			$fill_typemap->(sub { $generator->generate($path, $_[0]) });
			last
		}
	} else {
		my $get_article = sub {
			my $article = read_file "$ENV{GRUNTMASTER_ARTICLE_ROOT}/$basename.$_[0]";
			my $title   = read_file "$ENV{GRUNTMASTER_ARTICLE_ROOT}/$basename.$_[0].title";
			Gruntmaster::Page::Base::header($_[0], $title) . $article . Gruntmaster::Page::Base::footer($_[0])
		};

		$fill_typemap->($get_article);
	}

	for my $lang (@{LANGUAGES()}) {
		rename "$path_noext.$lang.$ext.new", "$path_noext.$lang.$ext";
		rename "$path_noext.$lang.gz.$ext.new", "$path_noext.$lang.gz.$ext";
	}
	rename "$path_noext.var.new", "$path_noext.var";
	close $typemap;
}

sub gensrc{
	my ($contest, $job) = split /\./, $_[0];
	local $Gruntmaster::Data::contest = $contest if $contest;
	my $ext = job_extension $job;
	my $log = $contest ? "ct/$contest/log" : 'log';
	make_path "$log/src/";
	say STDERR "Writing to $log/src/$job.$ext";
	write_file "$log/src/$job.$ext", job_inmeta($job)->{files}{prog}{content};
}

1;
__END__
# Below is stub documentation for your module. You'd better edit it!

=head1 NAME

Gruntmaster::Page - Perl extension for blah blah blah

=head1 SYNOPSIS

  use Gruntmaster::Page;
  blah blah blah

=head1 DESCRIPTION

Stub documentation for Gruntmaster::Page, created by h2xs. It looks like the
author of the extension was negligent enough to leave the stub
unedited.

Blah blah blah.

=head2 EXPORT

None by default.



=head1 SEE ALSO

Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

Marius Gavrilescu, E<lt>marius@E<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2013 by Marius Gavrilescu

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.18.1 or,
at your option, any later version of Perl 5 you may have available.


=cut
