#!/usr/bin/perl

# Copyright 2003-2005 Nathan Walp <faceprint(at)faceprint(dot)com>
# Copyright 2008 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 50 Temple Place, Suite 330, Boston, MA 02111-1307  USA
#

use POSIX qw(strftime);


my $PACKAGE="geany";


use Locale::Language;


$lang{'ca@valencia'} = "Catalan (Valencian)";
$lang{en_AU} = "English (Australian)";
$lang{en_CA} = "English (Canadian)";
$lang{en_GB} = "English (British)";
$lang{gl} = "Galician";
$lang{my_MM} = "Burmese (Myanmar)";
$lang{pt_BR} = "Portuguese (Brazilian)";
$lang{pt_PT} = "Portuguese (Portuguese)";
$lang{'sr@Latn'} = "Serbian (Latin)";
$lang{zh_CN} = "Chinese (Simplified)";
$lang{zh_TW} = "Chinese (Traditional)";
$lang{uk_UA} = "Ukrainian";
$lang{ast} = "Asturian";


opendir(DIR, ".") || die "can't open directory: $!";
@pos = grep { /\.po$/ && -f } readdir(DIR);
foreach (@pos) { s/\.po$//; };
closedir DIR;

@pos = sort @pos;

$now = `date`;

system("intltool-update --pot --gettext-package geany > /dev/null");

$_ = `LANG=C msgfmt --statistics geany.pot -o /dev/null 2>&1`;

die "unable to get total: $!" unless (/(\d+) untranslated messages/);

$total = $1;
$generated = strftime "%Y-%m-%d %H:%M:%S", gmtime;

sub find_translator
{
open(INPUT, "@_") or die "Couldn't open $infile for reading: $!\n";

	while (<INPUT>)
	{
		if (/.+Last-Translator: (.*) <.*/ or /.+Last-Translator: (.*) ?\\n"$/)
		{
			return $1;
		}
	}
	close(INPUT);
}


print "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">
	<head>
		<title>Geany translation statistics</title>
		<meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />
		<meta name=\"generator\" content=\"Geany 0.15\" />
		<link rel='icon' href='/favicon.ico' type='image/x-icon' />
		<link rel='shortcut icon' href='/favicon.ico' />
		<style type=\"text/css\">
			.bargraph {
				width: 200px;
				height: 20px;
				border-collapse: collapse;
				border-spacing: 0px;
				margin: 0px;
				border: 0px;
				padding: 0px;
			}
			.table_wo_border_green {
				border: 0px;
				background-color: #00ff00;
			}
			.table_wo_border_yellow {
				border: 0px;
				background-color: #ffff00;
			}
			.table_wo_border_red {
				border: 0px;
				background-color: #ff0000;
			}
			.td_spaced {
				padding-left: 3px;
				padding-right: 3px;
				text-align: center;
			}
			a {
				font-weight: bold;
			}
			body, p, pre, td {
				font-size: 11px;
				font-family: Arial,Verdana,sans-serif;
			}
			img {
				border: 0px;
			}
			th, td {
				border: #909090 1px solid;
				padding: 1px;
			}
		</style>
	</head>
	<body>
		<h1>Geany translation statistics</h1>
		<p>For more information about translating Geany, please see <a href=\"http://www.geany.org/Support/I18N\">www.geany.org/Support/I18N</a>.</p>
		<table>
			<tr>
				<th>Language (Last Translator)</th>
				<th colspan='2' style=\"background-color: #00ff00\">Translated</th>
				<th colspan='2' style=\"background-color: #ffff00\">Fuzzy</th>
				<th colspan='2' style=\"background-color: #ff0000\">Untranslated</th>
			</tr>
";


foreach $index (0 .. $#pos) {
	$trans = $fuzz = $untrans = 0;
	$po = $pos[$index];
	print STDERR "$po..." if($ARGV[0] eq '-v');
	system("msgmerge $po.po geany.pot -o $po.new 2>/dev/null");
	$_ = `LANG=C msgfmt --statistics $po.new -o /dev/null 2>&1`;
	chomp;
	if(/(\d+) translated message/) { $trans = $1; }
	if(/(\d+) fuzzy translation/) { $fuzz = $1; }
	if(/(\d+) untranslated message/) { $untrans = $1; }
	unlink("$po.new");

	$name = "";
	$name = $lang{$po};
	$name = code2language($po) unless $name ne "";
	$name = "???" unless $name ne "";
	$translator = find_translator("$po.po");

	$percentage_trans = sprintf("%.2f", ($trans / $total) * 100);
	$percentage_fuzz = sprintf("%.2f", ($fuzz / $total) * 100);
	$percentage_untrans = sprintf("%.2f", ($untrans / $total) * 100);

	$width_trans = sprintf("%.2f", ($trans / $total) * 200);
	$width_fuzz = sprintf("%.2f", ($fuzz / $total) * 200);
	$width_untrans = sprintf("%.2f", ($untrans / $total) * 200);

	$title = $name ." (". $po .") - Translated: ". $trans ." Fuzzy: ". $fuzz ." Untranslated: ". $untrans;

	$skip_cols = ($fuzz eq 0) + ($untrans eq 0);

	print "
			<tr>
				<td><a href=\"". $po .".po\">". $name ."</a> (". $translator .")</td>
				<td class=\"td_spaced\">". $trans ."</td>
				<td class=\"td_spaced\">". $percentage_trans ." %</td>
				<td class=\"td_spaced\">". $fuzz ."</td>
				<td class=\"td_spaced\">". $percentage_fuzz ." %</td>
				<td class=\"td_spaced\">". $untrans ."</td>
				<td class=\"td_spaced\">". $percentage_untrans ." %</td>
				<td>
					<table class='bargraph' title=\"". $title ."\">
						<tr class='table_wo_border'>
							<td class='table_wo_border_green' style=\"width: ". $width_trans ."px;\"";
	if ($skip_cols gt 0)
	{
		print " colspan=\"$skip_cols\"";
	}
	print "></td>\n";
	if ($fuzz gt 0)
	{
		print "							<td class='table_wo_border_yellow' style=\"width: ". $width_fuzz ."px;\"></td>\n";
	}
	if ($untrans gt 0)
	{
		print "							<td class='table_wo_border_red' style=\"width: ". $width_untrans ."px;\"></td>\n";
	}

	print "
						</tr>
					</table>
				</td>
			</tr>
	";
}

print "
		</table>
		<p><a href=\"geany.pot\">geany.pot</a> generated on $generated UTC (will be generated every 24 hours)</p>
		<p>&nbsp;</p>
		<p>
			This page was generated by <a href=\"i18n_stats.pl\">i18n_stats.pl</a>.
			Thanks to Nathan Walp &lt;faceprint(at)faceprint(dot)com&gt; for the original script.
		</p>
		<p>&nbsp;</p>
		<p>
			<a href=\"http://validator.w3.org/check?uri=referer\">
			<img src=\"http://i18n.geany.org/button-xhtml.png\" alt=\"Valid XHTML 1.0 Strict\" />
			</a>
			&nbsp;&nbsp;
			<a href=\"http://www.geany.org\">
			<img src=\"http://i18n.geany.org/button-geany.png\" alt=\"Geany - a small and lightweight IDE\" />
			</a>
		</p>
	</body>
</html>";


