#!/usr/bin/perl
# This script configures dictionaries-common (sarge version > 0.20)
use strict;
use warnings;

use vars qw($lang);

# We use preseed() helper subroutine so we have to include
# its definition
require '/usr/lib/localization-config/common/langmap.pl';
require '/usr/lib/localization-config/common/common.pl';
require '/usr/lib/localization-config/common/log.pl';
require '/usr/lib/localization-config/common/preseed.pl';

# If no locale is given as argument, quit
my $lang = $ARGV[0] or log_die("$0: No language given");

# Define the package name
my $package = "dictionaries-common";

# We define an associative arrays with the debconf keys
# These will be used in combination with the values in the
# next array
my %keynames = ( DICT     => 'dictionaries-common/default-ispell'
               );

# The actual values available.
my %dict_map = (
    'American (US)'  => { DICT => 'american (American English)'},
    'Australian'     => { DICT => 'british (British English)'},
    'Brazilian'      => { DICT => 'portugues brasileiro (Brazilian Portuguese)'},
    'Bulgarian'      => { DICT => 'bulgarian (Bulgarian)'},
    'Catalan'        => { DICT => 'catala8 (Catalan 8 bits)'},
    'Czech'          => { DICT => 'czech (Czech)'},
    'Danish'         => { DICT => 'dansk (Danish)'},
    'Dutch'          => { DICT => 'nederlands (Dutch)'},
    # entries sorted alphabetically
    'Faroe'          => { DICT => 'froyskt (Faroese)' },
    'Finnish'        => { DICT => 'suomi (Finnish Large)'},
    'French'         => { DICT => 'francais GUTenberg (French GUTenberg)'},
    'Galician'       => { DICT => 'galego-minimos (Galician-minimos)' },
    'German (DE)'    => { DICT => 'deutsch (New German 8 bit)'},
# Greek does not use ispell, use aspell instead, defaults to ibritish
    'Greek'          => { DICT => 'british (British English)'},
    'Hungarian'      => { DICT => 'magyar (Hungarian)' },
    'Irish'          => { DICT => 'Gaeilge (Irish)' },
    'Irish (UK)'     => { DICT => 'british (British English)'},
    'Italian'        => { DICT => 'italiano (Italian)'},
# Japanese and Korean do not have ispell support
#   'ja_JP'          => { DICT => ''},
#   'ko_KR'          => { DICT => ''},
    'Lithuanian'     => { DICT => 'lietuvių (Lithuanian)' },
# Latvian does not have ispell support
#   'lv_LV'          => { DICT => 'lv'},
    'Manx Gaelic'    => { DICT => 'Gaelg (Manx Gaelic)' },
    'New Norwegian'  => { DICT => 'nynorsk (New Norwegian)'},
    'Norwegian'      => { DICT => 'bokmål (Bokmal Norwegian)'}, # Same as no_NO
    'Polish'         => { DICT => 'polish (Polish)' },
    'Portuguese'     => { DICT => 'portugues europeu (European Portuguese)' },
    'Russian'        => { DICT => 'russian (Russian koi8-r)' },
# Northern Saami is not yet available
#   'Saami'          => { DICT => 'sapmi'},
    'Scots Gaelic'   => { DICT => 'Gaidhlig (Scots Gaelic)' },
    'Spanish (ES)'   => { DICT => 'castellano8 (Spanish 8 bit)'},
    'Swedish'        => { DICT => 'svenska (Swedish)'},
    'Swiss (DE)'     => { DICT => 'deutsch (Swiss German 8 bit)' },
    'Swiss (FR)'     => { DICT => 'francais GUTenberg (French GUTenberg)'},
    'Ukrainian'      => { DICT => 'ukrainian (Ukrainian)' }
);

my %language_map = get_lang_map();

# Print the supported locale entries.
if ($lang eq "supported") {
    for $lang (sort keys %dict_map) {
        print "$lang\n";
    }
    exit 0;
}

if ($lang eq "missing") {
    for my $entry (sort keys %dict_map) {
        my $success = 0;
        for my $lng (sort keys %language_map) {
            if ($language_map{$lng} eq $entry) {
                $success = 1;
            }
        }
        if ($success == 0) {
            log_msg("$0: Not defined : ".$entry);
        }
    }
    exit 0;
}

# Show all available console keymaps
if ($lang eq "showdicts") {
    for my $entry (sort keys %language_map) {
        print STDERR $entry." (".$language_map{$entry}.") :\n";
        my %sel = get_selected_dict_from_locale($language_map{$entry}, \%dict_map, \%keynames);
        for my $key (sort keys %sel) {
            print STDERR "\t".$keynames{$key}.":\t".$sel{$key}."\n";
        }
    }
    exit 0;
}

my $language = choose_language($lang, \%language_map);
log_msg("$0: Locale: ". $language);
my %sel = get_selected_dict_from_locale($language, \%dict_map, \%keynames);
for my $key (sort keys %sel) {
    log_msg("$0:\t".$keynames{$key}.":\t".$sel{$key});
}
# Call the preseed() subroutine to set the package debconf values.
preseed($package, \%sel, \%keynames);

exit 0;

sub get_selected_dict_from_locale {
    my ($lng, $dict_lang_map, $debconf_keys) = @_;
    
    my %selected;

    for my $key (sort keys %$debconf_keys) {
        if (defined($dict_lang_map->{$lng}{$key})) {
            $selected{$key} = $dict_lang_map->{$lng}{$key};
        }
    }
    
    return %selected;
}

1;
