#! /usr/bin/perl -w

use strict;

use lib "/home/mquinson/src/DEBIAN/webwml/Perl";
use Webwml::L10n::Db;

my $data = Webwml::L10n::Db->new();
unless (-e "unstable.ftp-master") {
    system "wget -q -O- http://ftp-master.debian.org/~barbier/l10n/material/data/unstable.gz > unstable.ftp-master.gz" || die;
    system "wget -q -O- http://non-us.debian.org/~barbier/l10n/material/data/unstable.gz > unstable.non-US.gz" || die;
    system "gunzip unstable.ftp-master.gz" || die;
    system "gunzip unstable.non-US.gz" || die;
}
$data->read("unstable.ftp-master");
$data->read("unstable.non-US");


my @lines = <STDIN>;
my @line;
my @len   = (0,1,1,1,1,1);
my $s;

chomp @lines;

foreach (@lines) {
	/([^\t]*)?\t([^\t]*)?\t([^\t]*)?\t([^\t]*)?\t([^\t]*)?\t(.*)/;
	next unless $3;
	@line = (0, $1 || "", $2 || "", $3 || "", $4 || "", $5 || "", $6 || "");
	s/^"\s*// foreach (@line);
	s/\s*"$// foreach (@line);
	for (my $i = 1; $i < 7; $i++) {
		$len[$i] = $len[$i]<length($line[$i]) ? length($line[$i]) : $len[$i];
	}
}
$len[3] = $len[3]>13 ? 13 : $len[3];
$s = "%".$len[1]."s %".$len[2]."s %".$len[3]."s %".$len[4]."s %".$len[5]."s %s\n";

foreach (@lines) {
	/([^\t]*)?\t([^\t]*)?\t([^\t]*)?\t([^\t]*)?\t([^\t]*)?\t(.*)/;
	@line = ($1 || "", $2 || "", $3 || "", $4 || "", $5 || "", $6 || "");
	s/^"\s*// foreach (@line);
	s/\s*"$// foreach (@line);

	if ($line[2] || $line[3] || $line[4]) {
	    $data->add_switch(lc($line[0]), sprintf $s, @line);
	} else {
	    $data->add_switch(lc($line[0]), join(" ", @line));
	}
}

foreach my $pkg ($data->list_packages()) {
    next if ($data->has_templates($pkg) || $data->has_switch($pkg));
    $data->clear_pkg($pkg);
}

$data->write("/tmp/output");
