#!/usr/bin/perl
# This script will test with all messages are present in a translation file
# The original messages are allways from english.txt file
#
# Copyright (C) 2004 Alceu Rodrigues de Freitas Jr. (glasswalk3r@yahoo.com.br)
#
#    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., 675 Mass Ave, Cambridge, MA 02139, USA.

use warnings;
use strict;
use Getopt::Std;

my $version = '0.2';
my %opts;
getopts('b:t:hv',\%opts);


if ( ( exists($opts{h}) ) and ($opts{h} == 1) ) {

print <<BLOCK;
Usage: testlang.pl -b <base language file> -t <translated file>
Where:
    -b <base language file> identifies the complete pathname to a file that will be used as 
       base to check messages. Usually this is the english.txt file.
    -t <translated file> identifies the complete pathname to a file that messages will be checked
       against the <base language file>
    -h shows this help message
    -v prints program information
BLOCK
exit;

}

if ( ( exists($opts{v}) ) and ($opts{v} == 1) ) {

print <<BLOCK;
testlang is a translation tool for Viralator (http://viralator.sourceforge.net)

Copyright (C) 2004 Alceu Rodrigues de Freitas Jr. (glasswalk3r\@yahoo.com.br)

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.

testlang is a script that will test all messages present in a translation file
comparing to a base file.

BLOCK
exit;

}

test_file($opts{b},'b');
test_file($opts{t},'t');

my $translationfile = $opts{t};
my $basefile = $opts{b};
my $flag = 0;
my %list;
my %new_messages;

open(SOURCE,"<$basefile") || die "Cannot read source messages from $basefile file: $!\n";

open(FILE,"<$translationfile") || die "Cannot read $translationfile: $!\n";
my @file_content = <FILE>;
close(FILE);

# reading original messages

while (<SOURCE>) {

    next if  /^\#/;
    next unless /=/;

    chomp;
    s/\s+=\s+/=/;
    my @temp = split(/=/,$_);
   
    $list{$temp[0]} = [ '0',$temp[1] ];

}

close(SOURCE);

foreach (@file_content) {

        next if (/^#/);
        next unless (/[\w]+/);
	
        my @temp = split(/=/,$_);
	
        $temp[0] =~ s/\s//g;
	
	unless ( exists( $list{$temp[0]} ) ) {
	
		print "The message $temp[0] is not valid\n";
		$flag++;
		
	} else {

		unless ( $temp[1] =~ /[\w]+/ ) {
		
			print "Message $temp[0] has no value\n";
			$flag++;

# the message exists and have a translated message
		} else {

			$list{$temp[0]}->[0] = 1;

		}
		
	}

}

foreach ( keys(%list) ) {
		

	if ($list{$_}->[0] == 0) {

		print "Message \"$_\" was not found in this translation file\n";
		$flag++;
		$new_messages{$_} = $list{$_}->[1];

	}


}
	
if ($flag == 0) {	
	
	print "No errors found: translation file is OK\n";

} else {

	print "$flag errors where found in the translation file\n";
	print "Do you want to create a new file with the missing messages? (y/n) ";
	my $answer = <STDIN>;
	chomp($answer);
	
	if ($answer eq 'y') {
	
	    rename $translationfile,"$translationfile.old";
	    
	    open(NEW,">$translationfile") || die "Cannot create new translation file $translationfile: $!\n";
	    
	    foreach (@file_content) { print NEW; }
	    
	    my $key;

print NEW <<BLOCK;
##########################
# Inserted by test_lang.pl
##########################
BLOCK
	    
	    foreach $key( keys(%new_messages) ) {
	    
	        print NEW "$key = $new_messages{$key}\n";
	    
	    }
	    
            close(NEW);

            print "New translation file created successfully\n";
	
	}

}

sub test_file {

    my $value = shift;
    my $param = shift;

    if ( defined($value) ) {

        die "You must pass a file as parameter for $param.\n" unless defined($param);
        die "Invalid file name as parameter for $param.\n" unless ($param =~ /^[\w\.\-\_]+$/);
    
    } else {

        die "You must pass a file as parameter for param \"$param\". Execute \"testlang -h\" for more information.\n";
    
    }    

}