#!/usr/bin/perl

use strict;
use warnings qw(all);
use Time::Local;
use Date::Format;
use Date::Language;

umask 0000;

# This script generate a 'blog' page with last entries 
# You MUST use the following skel :
# <dokuroot>/home/pages/<blog>/<date>/<title>
# where date is in the YYYYMMDD form.
#
# The <dokuroot>/home/pages/<blog>.txt file will be generate
# with the last $<blog>->entries post. It will add the post's content
# and a permanent link to the post.
#
# The lang key is used to compute the date in a textual form

my $data = '/data3/debianfr/asyd/asyd.net/home/data/';

# config
my $blogs = {
	geeklog => {
		data => $data . '/pages/geeklog',
		entries => 5,
		fullentries => 100,
		lang => 'English',
	},
#	blog => {
#		data => '/data3/debianfr/asyd/asyd.net/home/data/pages/blog',
#		entries => 5,
#		fullentries => 100,
#		lang => 'French',
#	},
};

my $links = {
	French => {
		linktitle => 'Lien permanent',
	},
	English => {
		linktitle => 'Permanent link and discussions',
	},
};

# main
foreach my $blog (keys %{$blogs}) {
	parse_blog ($blog,
		$blogs->{$blog}->{'data'},
		$blogs->{$blog}->{'entries'}, 
		$blogs->{$blog}->{'fullentries'}, 
		$blogs->{$blog}->{'lang'},
	);
}

sub list_entries {
	my $blog = shift;
	my $fh;
	my @buffer;
	my $entries = {};

	# open changelog 
	open ($fh, "$data/changes.log");
	while (<$fh>) {
		chomp;
		my $id = [split /\s+/]->[2];
		if ($id =~ /^$blog:(\d{4}):(\d{2}):(\d{2}):.+/) {
			my $tmp = $1 . '/' . $2 . '/' . $3;
			if (not defined $entries->{$tmp}) {
				push @buffer, $tmp;
				$entries->{$tmp} = 1;
			}
		}
	}

	close ($fh);
	return sort {$b cmp $a} @buffer;
}

sub parse_blog {
	my ($blog, $data, $entries, $fullentries, $lang) = @_;
	my $fh;
	my $fhfull;
	my $dir;

	my @data = list_entries($blog);

	chmod 0666, "$data.txt";
	chmod 0666, "$data-full.txt";

	open ($fh, "> $data.txt")
		or die "Can't open file > $data-test.txt";
	open ($fhfull, "> ". $data . "-full.txt")
		or die "Can't open file > $data-full.txt";

	# List blog posts and sort them (numerical desc)
	my $index = 0;
	foreach my $entry (@data) {
		my @titles;

		unless ($entry =~ m#(\d{4})/(\d{2})/(\d{2})#) {
			warn("Invalid date $entry !");
			next;
		};

		my $time = timelocal(0, 0, 0, $3, $2 - 1, $1);
		my $date = Date::Language->new($lang);

		print $fh "====== ", ucfirst($date->time2str("%A %e %B %Y", $time))," ======\n\n"
			if ($index < $entries);
		print $fhfull "====== ", ucfirst($date->time2str("%A %e %B %Y", $time))," ======\n\n"
			if ($index < $fullentries);

		opendir (my $blogdir, "$data/$entry");
		@titles = grep { ! /^(\.|discussion)/ && ! /\.lock$/ } readdir ($blogdir);
		foreach my $title (@titles) {
			$title =~ s/\.txt$//;

			# Replace _ by ' ' in title
			my $buffer = $title;
			$buffer =~ s/_/ /g;
			# On copie le contenu du premier message
			if ($index == 0) {
				 print $title, "\n";
			    my $fh2;
			    open ($fh2, "< $data/$entry/$title.txt");
			    while (<$fh2>) {
					unless (/~~DISCUSSION~~/) {
						print $fh $_;
					}
			    }
			    close ($fh2);
				$entry =~ s#/#:#g;
			   print $fh "\n//[[$blog:$entry:$title|", $links->{$lang}->{'linktitle'}, "]]// \n\n";
			} elsif ($index < $entries) {
				print $fh "===== ", ucfirst($buffer), " =====\n";
				print $fh "\n\n";
				print $title, "\n";
			   print $fh "//[[$blog:$entry:$title|", $links->{$lang}->{'linktitle'}, "]] //\n\n";
			}
			if ($index < $fullentries) {
				print $fhfull "   * [[$blog:$entry:$title|", ucfirst($buffer), "]]\n";
			}
		}
		print $fh "\n";
		print $fhfull "\n";
		closedir ($blogdir);
		$index++;
	}

	close ($fh);
	close ($fhfull);

	chmod 0444, "$data.txt";
	chmod 0444, "$data-full.txt";

}
