#! /usr/bin/perl -w
use strict;

my $iso3166tab = 'debian/iso_3166.tab';

# Get mapping of country names to country codes
my %iso3166rev;
open(ISO3166TAB, "< $iso3166tab") || die "Unable to read $iso3166tab";
while (<ISO3166TAB>) {
    /^([A-Z]+)\t(.*)$/ or next;
    $iso3166rev{$2} = $1;
}
close ISO3166TAB;

# Stringtable output is easier to handle.
open SOURCESTRINGS, '-|:utf8', 'msgcat', '--stringtable-output', '-'
    or die "open msgcat for source: $!";
open TARGETSTRINGS, '|-:utf8', 'msgcat', '--stringtable-input', '-s', '-'
    or die "open msgcat for target: $!";
while (<SOURCESTRINGS>) {
    if (/^"(.+?)" = "(.*?)";$/) {
	if (exists $iso3166rev{$1}) {
	    print TARGETSTRINGS qq{"$iso3166rev{$1}" = "$2";\n};
	}
    } else {
	print TARGETSTRINGS;
    }
}
close SOURCESTRINGS;
close TARGETSTRINGS or die "close msgcat for target: $!";
