#!/usr/bin/perl -w
# 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.

use strict;

use lib "$ENV{'LINTIAN_ROOT'}/lib";
use Util;

($#ARGV == 1) or fail("syntax: po-debconf <pkg> <type>");
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" ) {
	print "W: $pkg $type: not-using-po-debconf\n";
}

#   Exit if package does not seem to use po-debconf
-d "debfiles/po" or exit(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,^\.\./,;
		print "W: $pkg $type: missing-file-from-potfiles-in $_\n"
			 unless -f "debfiles/$_";
	}
	close(POTFILES);
} else {
	print "W: $pkg $type: missing-potfiles-in\n";
}
if (! -f "debfiles/po/templates.pot") {
	print "W: $pkg $type: missing-templates-pot\n";
}

opendir(DEBIAN, 'debfiles/po')
        or fail("Can't open debfiles/po directory.");
if (! -x "/usr/bin/msgfmt" ) {
    print "N: Warning: msgfmt (package gettext) not available, not\n";
    print "N: going to check *.po syntax\n";
}
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);
        }
        print "W: $pkg $type: unknown-charset-in-po-file debian/po/$file\n"
                unless length($charset);
	if (-x "/usr/bin/msgfmt" ) {
	    system("msgfmt -o /dev/null debfiles/po/$file 2>/dev/null") == 0
                or print "W: $pkg $type: invalid-po-file debian/po/$file\n";
	}
}

exit(0);
