#!/usr/bin/perl -w
use strict;

die "Must be executed in the 'debian' subdir" unless -d 'upstream';

`rm -f upstream/phpbb3_l10n-*.info`;

my @lists;
my @langs;

my $index='http://www.phpbb.com/languages/index.php';
my $trans='http://www.phpbb.com/customise/db/translation/';
my $downl='http://www.phpbb.com/customise/db/download/id_';

# Get language pages and other list pages
open DOWNLOADS, "wget -q -O- $index|";
while (<DOWNLOADS>) {
	if ( @lists == 0 and m,<a href="\./\.\./languages/index.php(\?sid=\w+&amp;start=\d+)">, ) {
	       push @lists, $1 while s,<a href="\./\.\./languages/index.php\?sid=\w+&amp;(start=\d+)">,,;
	}
	push @langs, $1 if m,<a href="$trans(.+?)/sid_\w+?">,;
}
close DOWNLOADS;

# Get other language pages
for (@lists) {
	open DOWNLOADS, "wget -q -O- $index\?$_|";
	while (<DOWNLOADS>) {
		push @langs, $1 if m,<a href="$trans(.+?)/sid_\w+?">,
	}
	close DOWNLOADS;
}

# Get information about language pack
for (@langs) {
	open DOWNLOADS, "wget -q -O- http://www.phpbb.com/customise/db/translation/$_|";
	my ($url,$code,$md5);
	while (<DOWNLOADS>) {
		$url = $1 if m,<a class="download-main" href="($downl\d+)-sid_\w+",;
		$code = $1 if m,<strong>ISO Code</strong> (.+)\n,;
		$md5 = $1 if m,<strong>MD5 checksum</strong> (\w+)\n,;
	}
	close DOWNLOADS;
	open INFO, "> upstream/phpbb3_l10n-$code.info";
	print INFO "Url=$url\n";
	print INFO "Md5=$md5\n";
	print INFO "Copyright=GPL-2\n";
	print INFO "Include=yes\n";
	close INFO;
}
