#!/usr/bin/perl -w

# This is a toy to compute the karma of translators in the Debian web site
# CVS repository.

# It use information about translation revisions please see
# http://www.debian.org/devel/website/uptodate
# it also need informations about the maintainer. Just add 
#   maintainer="Name"
# at the end of the wml::debian::translation-check template invocation

# This is GPL'ed code.
# Copyright 2002 Martin Quinson <martin.quinson@ens-lyon.fr>.

# Invocation:
#   karma.pl [language]

use strict;
use Getopt::Std;
use IO::Handle;
use Date::Parse;

#    These modules reside under webwml/Perl
use lib ($0 =~ m|(.*)/|, $1 or ".") ."/Perl";
use Local::Cvsinfo;
use Local::WmlDiffTrans;
use Webwml::TransCheck;
use Webwml::TransIgnore;

#where to compute the karma
my %karma;

# include only files matching $filename
my $filename = '(\.wml$)|(\.html$)';

my $cvs = Local::Cvsinfo->new();
$cvs->options(
	recursive => 1,
	matchfile => [ $filename ],
	skipdir   => [ "template" ],
);
#   This object is used to retrieve information when original is
#   not English
my $altcvs = $cvs->new();

#   Global .transignore
my $globtrans = Webwml::TransIgnore->new(".");

# language configuration
my $defaultlanguage = 'italian';
if (exists $ENV{DWWW_LANG}) 
{
	$defaultlanguage = $ENV{DWWW_LANG};
} 
elsif (open CONF, "<language.conf")
{
	$defaultlanguage = <CONF>;
	chomp $defaultlanguage;
	close CONF;
}

my $from = 'english';
my $to = shift || $defaultlanguage;
$to =~ s%/$%%; # Remove slash from the end

my $langto = $to;
$langto =~ s,^([^/]*).*$,$1,;

$cvs->readinfo($from);
foreach my $path (@{$cvs->dirs()}) {
	my $tpath = $path;
	$tpath =~ s/^$from/$to/o;
	my $transignore = Webwml::TransIgnore->new($tpath);
	next unless $transignore->found();
	foreach (@{$transignore->local()}) {
		s/^$to/$from/o;
		$cvs->removefile($_);
	}
}

foreach (sort @{$cvs->files()}) {
	my ($path, $tpath);
	$path = $_;
	$tpath = $path;
	$tpath =~ s/^$from/$to/o;
	check_file($tpath,
		$cvs->revision($path),
		str2time($cvs->date($path)));
}

sub check_file {
	my ($name, $revision,$translator) = @_;
	my ($oldr, $oldname, $original);
        my $docname = $name;
        $docname =~ s#^$langto/##;
        $docname =~ s#\.wml$##;
	return unless (-r $name); # file does not exists

	my $transcheck = Webwml::TransCheck->new($name);
	$oldr = $transcheck->revision() || 0;
	if (!$oldr && ($name =~ m#$langto/international/$langto#i)) {
		#   This document is original, check for
		#   english/international/$langto...
		$name =~ s{^$to}{$from};
		$transcheck = Webwml::TransCheck->new($name);
		$oldr       = $transcheck->revision() || 0;
	}
	$translator = $transcheck->maintainer();
	$original   = $transcheck->original();
	return unless $translator; # translator not found    

	$translator =~ s/^\s+//;
	$translator =~ s/\s+$//;
	$karma{$translator}=0 unless defined($karma{$translator});
	$translator =~ s/^\s+//;
	$translator =~ s/\s+$//;

	$karma{$translator} += 1; # page translated. GOOD

	if ($original) {
		my ($fromname, $fromdir);
		$fromname = $name;
		$fromname =~ s{^[^/]+}{$original};
		$fromdir  = $fromname;
		$fromdir  =~ s{/+[^/]+$}{};
		$altcvs->reset();
		$altcvs->readinfo($fromdir, matchfile => [$fromname]);
		$revision = $altcvs->revision($fromname);
	}

	(my $numrev)  = $revision =~ m/^1\.(\d+)$/; $numrev ||= "0";
	(my $numoldr) = $oldr =~ m/^1\.(\d+)$/; $numoldr ||= "0";
	my $age=$numrev - $numoldr; 
#	print "$translator: ";
#	if ($age > 0) {
#	    print "NeedToUpdate $name from version 1.$numoldr to version 1.$numrev\n";
#	} else {
#	    print "Uptodate $name\n";
#	}
	$karma{$translator} -= $age; #out of date page; Bad
}

foreach my $translator (sort {$karma{$b} <=> $karma{$a}} keys %karma) {
    print "$translator have a karma of ".$karma{$translator}."\n";
}
