##########################################################################
# localemap.pl -- Build locale map based on source data files
#
# Copyright (c) 2005, PostgreSQL Global Development Group
#
# $PostgreSQL$
##########################################################################
#
# The file iso639 contains ISO-639 language codes, cut-and-paste from
# http://www.w3.org/WAI/ER/IG/ert/iso639.htm.
#
# The file iso3166 contains ISO-3166 country codes, cut-and-paste from
# http://www.oasis-open.org/cover/country3166.html,
#

open(I,"iso639") || die "Could not open iso639\n";
while (<I>) {
	chomp;
	die "Malformatted line in iso639" unless /^([A-Z]{2}) \"(.+)\"$/;
	$code = lc $1;
	$lang = lc $2;
	if ($lang =~ /^(.+)\" /) { $lang = $1; }
	$iso639{$lang}=$code;
}
close(I);

open(I,"iso3166") || die "Could not open iso3166\n";
while (<I>) {
	chomp;
	die "Malformatted line in iso3166" unless /^([A-Z]{2})\s+(.+)$/;
	$code = lc $1;
	$country = lc $2;
	$iso3166{$country} = $code;
}
close(I);

print <<EOF;
typedef struct {
	char *key;
	char *val;
} _keyval;

static _keyval iso639[] = {
EOF
for $k (sort keys %iso639) {
	print " {\"$k\",\"$iso639{$k}\"},\n";
}
print " {NULL,NULL}\n};\n\n";

print "static _keyval iso3166[] = {\n";
for $k (sort keys %iso3166) {
	print " {\"$k\",\"$iso3166{$k}\"},\n";
}

print " {NULL,NULL}\n};\n\n";
