#! /usr/bin/perl

# This proggie takes a (possibly outdated) locale file as its (only)
# argument, and creates a new version, with `-work` appended. The thus
# created tile contains all keys of the master file, and is intended
# to, after manual editing, replace the outdated locale file.

# Written by Barf on 2005-12-10.

$masterfilename = "english.locale";

$#ARGV == 0 || die("Usage: create-locals-work file.locale.");

$no_errors = 0;
$last_was_ok = 1;
$localefilename = @ARGV[0];
$outfilename = $localefilename . "-work";

open(masterfile, $masterfilename) || die("Could not open master file");
open(localefile, $localefilename) || die("Could not open locale file");
open(outfile, ">" . $outfilename) || die("Could not open output file");

%master = {};
%locale = {};

while (<masterfile>) {
    # master hash
    $line = $_;
    ($key) = /([^ ]+)/;
    ($junk, $text) = /([^ ]+)[ ]+([^\n]+)/;
    $master{$key} = $text;
}

while (<localefile>) {
    # locale hash
    $line = $_;
    ($key) = /([^ ]+)/;
    ($junk, $text) = /([^ ]+)[ ]+([^\n]+)/;
    $locale{$key} = $text;
}

foreach $term (sort keys %master) {
    if (exists $locale{$term}) {
        print outfile $term,  " ", $locale{$term}, "\n";
    } else {
        # not found
        $no_errors++;
        print outfile $term,  " TRANSLATE ", $master{$term}, "\n";
    }
}

if ($no_errors == 0) {}
    unlink($outfilename);
} else {
    print "There were ", $no_errors, " error(s) in ", $localefilename, ".\n";
}
