#!/usr/bin/perl
# The LearningOnline Network with CAPA
# $Id: checkuntranslated.pl,v 1.1 2009/11/25 13:36:56 bisitz Exp $

# 25.11.2009 Stefan Bisitz

use strict;
use warnings;

my $man = "
checkuntranslated - Checks if a translation file contains untranslated phrases. All untranslated phrases are listed.


SYNOPSIS:\tcheckuntranslated -h 
\t\tcheckuntranslated FILE

OPTIONS:
-h\t\tDisplay this help and exit.

";

my $filename; 
die "Use option -h for help.\n" unless exists $ARGV[0];
#analyze options
if ( $ARGV[0] =~ m/^\s*-h/ ) {
	print $man;
	exit();
} else {
	$filename = ($ARGV[0]);
	die "$filename is not a file.\n" unless -f $ARGV[0];
}


# ----------------------------------------------------------------
# Start Analysis
print "checkuntranslated is searching for untranslated phrases in $filename...\n";

# Read translation file row by row and try to find matching keys and values.
# It is assumed that all keys are followed by a value using the following syntax:
#    'key'
# => 'value',
#
# Optional comment rows and/or comments at the end of each row
# and white spaces are allowed and will be ignored.
#
# Compare key and value: identical? -> untranslated!
my $counter = 0;
my $line;
my $key = '';
my $mode = 'key';
open( FH, "<", $filename ) or die "$filename cannot be opened\n";
while ( !eof(FH) ) {
    $line = readline(FH);
    # Ignore comments
    next if $line=~/^\s*#/;
    # Key?
    if ($mode eq 'key') {
        # Search for key
        if ($line =~ m/^\s+["'](.*)["']/) {
            $key = $1;
            $mode = 'value';
        }
    # Value?
    } else { # $mode eq 'value'
        # Search for value
        if ($line =~ m/^\s*=>\s*["'](.*)["']/) {
            if ($key eq $1) { # key = value?
                print $key."\n";
                $counter++;
            }
            $mode = 'key';
        }
    }
}
close(FH);


# Display summary message
if ($counter == 0) {
    print "Be happy - No untranslated phrases found.\n";
} else {
    print "Found $counter untranslated phrases in $filename.\n";
    print "Please ignore all phrases which should have the same translation as the English phrase.\n";
}

# ----------------------------------------------------------------

