#!/usr/bin/perl
#
#
# Utility to check strings which need translation.
#
# Note that Cyrillic languages (be, bg, mk, sr, and uk) are not checked.
#
#

print <<EOF;
---------------------------------------------
Now checking translation for each language
EOF

#--------------------------------
# check strings in 'set-language-env'
#--------------------------------
sub printf_ ($@) {
	$msgid{$_[0]} = "set-language-env: $num";
}

open(FILE, "./set-language-env") || die "CANNOT OPEN set-language-env.\n";
$num = 0;
while($line=<FILE>) {
	$num++;
	if ($line !~ /&printf_/) {next;}
	
	$a = $line;
	if ($line !~ /\);\s$/) {
		while ($line = <FILE>) {
			$num++;
			$a .= $line;
			if ($line =~ /\);\s$/) {last;}
		}
	}
	eval($a);
	if ($@) {printf STDERR "$a\n...\n$@";}
}
close(FILE);

#---------------------------------------
# build a list of all support.*.pl files
#---------------------------------------
opendir(DIR, ".") || die "Cannot open this directory.\n";
@filelist1 = readdir(DIR);
closedir(DIR);
@filelist = sort(@filelist1);
foreach $filename (@filelist) {
	if ($filename !~ /^support\.(.*)\.pl.*$/) {next;}
	push(@supports,$filename);
}

#-----------------------------
# check all support.*.pl files
#-----------------------------
foreach $s (@supports) {
	%messages = undef;
	open(FILE, $s) || die "CANNOT OPEN $s\n";
	@file1 = <FILE>; close(FILE); $file = join("", @file1);
	eval($file);

	print "Checking $s ...";

	if ($file1[0] =~ /!([^!]+)!/) {
		$langname = $1;
		if ($langname =~ /([\x80-\xff]+)/) {
			print "\n*** error ***: non-ASCII character for ".
			"language name \"$langname\" (bad character is ".
			"\"$1\"). in the first line\n";
			exit(1);
		}
	}

	foreach $m (keys %msgid) {
		if (exists $messages{$m}) {
			if ($messages{$m} =~ /([^\000]*)\000([^\000]*)/) {
				if ($1 =~ /([\x80-\xff]+)/) {
					print "\n*** error ***: non-ASCII".
					" character before \\000" .
					" in \"$messages{$m}\"\n".
					"bad character is \"$1\".\n";
					exit(1);
				}
			}
			next;
		}
		print "\n*** error ***: no translation for: $msgid{$m}\n";
		print "message = \"$m\"\n";
		exit(1);
	}
	print " done\n";
}

#------------------
# check other items
#------------------
open(FILE, "debian/docs") || die "CANNOT OPEN debian/docs\n";
@docs = <FILE>; close(FILE);
open(FILE, "set-language-env.1") || die "CANNOT OPEN set-language-env.1\n";
@manual = <FILE>; close(FILE);

foreach $s (@supports) {
	if ($s !~ /^support\.([^\.]+)\.pl$/) {next;}
	$lang = $1;

	if (grep(/README\.$lang/, @filelist)) {
		if (grep(/README\.$lang/, @docs) == 0) {
			print "*** error ***: README.$lang is not listed in" .
			" debian/docs.\n";
			exit(1);
		}
	} else {
		print "*** error ***: README.$lang is not supplied\n";
		exit(1);
	}
	if (grep(/\.B\s+$lang/, @manual) == 0) {
		print "*** error ***: $lang is not mentioned in manual.\n";
		exit(1);
	}
}		
