#!/usr/bin/perl
#
# Takes a list of locales like /usr/share/i18n/SUPPORTED on standard input
# or on the command line, and writes out a locale.alias format file for all
# listed locales on standard output.
#
my %locales;
# whether or not locales should always specify charset or not.
my $always_charset = 0;

chdir("/usr/share/i18n/locales") or die "can't find /usr/share/i18n/locales";
while (<>) {
        my $language;
        my $territory;
        chomp;
        @line = split / /;
        ($file = $line[0]) =~ s/\..*$//;
        open F, $file or die "can't open $file\n";
        while (<F>) {
                chomp;
                $title = $1 if /^title\s+"(.*)"/;
                $language = $1 if /^language\s+"(.*)"/;
                $territory = $1 if /^territory\s+"(.*)"/;
                last if /^END LC_IDENTIFICATION/;
        }
        close F;

        if ($always_charset and $line[1] and $line[0] =~ s/(\@.*)$//) {
                my $suffix = $1;
                $line[1] =~ s/$/$suffix/;
        }

        $language =~ s/ /_/g;
        $territory =~ s/ /_/g;
        if (!$always_charset or $line[0] =~ /\./) {
                print "$language($territory)\t$line[0]\n";
        } else {
                print "$language($territory)\t$line[0].$line[1]\n";
        }
}
