# po-debconf -- lintian check script

# Copyright (C) 2002-2004 by Denis Barbier <barbier@linuxfr.org>
#
# 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, you can find it on the World Wide
# Web at http://www.gnu.org/copyleft/gpl.html, or write to the Free
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
# MA 02111-1307, USA.

package Lintian::po_debconf;
use strict;
use Tags;
use Util;

sub run {

my $pkg = shift;
my $type = shift;

# First, check wether this package seems to use debconf but not po-debconf
opendir(DEB, 'debfiles')
	or fail("Can't open debfiles directory.");
my $has_template = 0;
for my $file (readdir(DEB)) {
	if ($file =~ m/^(.+\.)?templates$/) {
# TODO: out of this loop, use fields/*?
		open(IN,"debfiles/control")
			or fail("Can't open debfiles/control.");
		while (<IN>) {
			if (m/^Depends:/i) {
				$has_template = 1
					if m/\bdebconf(-[.\d]+)?\b/;
			}
		}
		close(IN);
		last;
	}
}
closedir(DEB);

#TODO: check whether all templates are named in TEMPLATES.pot
if ( $has_template && ! -d "debfiles/po" ) {
	tag "not-using-po-debconf", "";
}

#   Exit if package does not seem to use po-debconf
-d "debfiles/po" or return(0);

my $missing_files = 0;
if (-f "debfiles/po/POTFILES.in") {
	open(POTFILES,"debfiles/po/POTFILES.in")
		or fail("Can't open debfiles/po/POTFILES.in.");
	while (<POTFILES>) {
		chomp;
		s/.*\]\s*//;
		#  Cannot check files which are not under debian/
		next if m,^\.\./, or $_ eq '';
		unless (-f "debfiles/$_") {
			tag "missing-file-from-potfiles-in", "$_";
			$missing_files = 1;
		} 
	}
	close(POTFILES);
} else {
	tag "missing-potfiles-in", "";
	$missing_files = 1;
}
if (! -f "debfiles/po/templates.pot") {
	tag "missing-templates-pot", "";
	$missing_files = 1;
}

if (-x "/usr/bin/msgcmp" && -x "/usr/share/intltool-debian/intltool-update" ) {
	if ($missing_files == 0) {
		$ENV{"INTLTOOL_EXTRACT"} ||= "/usr/share/intltool-debian/intltool-extract";
		system("cd debfiles/po && /usr/share/intltool-debian/intltool-update --gettext-package=test --pot");
		system("/usr/bin/msgcmp debfiles/po/test.pot debfiles/po/templates.pot >/dev/null 2>&1") == 0
                	or tag "newer-debconf-templates";
	}
} else {
	fail("either msgcmp or intltool-update not found");
}

if (! -x "/usr/bin/msgfmt" ) {
	fail("msgfmt not found");
}
opendir(DEBIAN, 'debfiles/po')
        or fail("Can't open debfiles/po directory.");
while (defined(my $file=readdir(DEBIAN))) {
        next unless $file =~ m/\.po$/;
        local ($/) = "\n\n";
        $_ = '';
        open(PO, "< debfiles/po/$file")
                or fail("Can't open debfiles/po/$file file.");
        while (<PO>) {
                last if m/^msgstr/m;
        }
        close(PO);
        s/"\n"//g;
        my $charset = '';
        if (m/charset=(.*?)\\n/) {
                $charset = ($1 eq 'CHARSET' ? '' : $1);
        }
        tag "unknown-encoding-in-po-file", "debian/po/$file"
                unless length($charset);
	system("msgfmt -o /dev/null debfiles/po/$file 2>/dev/null") == 0
		or tag "invalid-po-file", "debian/po/$file";
}

}

1;

# vim: syntax=perl
