#!/usr/bin/perl
# This script configures KDE 3
use strict;
use warnings;

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

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

# The config filenames 
# KDM does not honour the global KDE settings,
# we need to edit kdmrc too
my $global = '/etc/kde3/system.kdeglobals';
my $kdmrc = '/etc/kde3/kdm/kdmrc';

# These are X settings -> see /usr/X11R6/lib/locale for charset names
my %lang_map = (
        #entries sorted alphabetically
        'Australian'    => { 'Locale/Country' => 'au',
                           'Locale/Language' => 'en',
                           'Locale/Charset'  => 'iso8859-1',
                        },
        'American (US)' => { 'Locale/Country' => 'us',
                           'Locale/Language' => 'en',
                           'Locale/Charset'  => 'iso8859-1',
                         },
        'Bosnian'       => { 'Locale/Country'  => 'ba',
                                'Locale/Language' => 'bs_BA',
                                'Locale/Charset'  => 'iso8859-2',
                         },
        'Brazilian'     => { 'Locale/Country' => 'br',
                           'Locale/Language' => 'pt_BR',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'Canadian'      => { 'Locale/Country' => 'ca',
                           'Locale/Language' => 'en',
                           'Locale/Charset'  => 'iso8859-1',
                         },
        'Catalan'       => { 'Locale/Country'  => 'es',
                           'Locale/Language' => 'ca_ES',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'Danish'        => { 'Locale/Country'   => 'dk',
                           'Locale/Language' => 'da_DK',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'Dutch (BE)'    => { 'Locale/Country' => 'be',
                           'Locale/Language' => 'nl_BE',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'Dutch'         => { 'Locale/Country'   => 'nl',
                           'Locale/Language'  => 'nl_NL',
                           'Locale/Charset'   => 'iso8859-15',
                         },
        'German (BE)'   => { 'Locale/Country' => 'be',
                           'Locale/Language' => 'de_BE',
                           'Locale/Charset'  => 'iso8859-15',
                         },   
        'German (DE)'        => { 'Locale/Country' => 'de',
                           'Locale/Language' => 'de',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'Greek'         => { 'Locale/Country' => 'gr',
                           'Locale/Language' => 'el',
                           'Locale/Charset'  => 'iso10646-1',
                           'General/StandardFont' => 'MgOpen Modata,12,-1,5,50,0,0,0,0,0',
                           'General/activeFont'   => 'MgOpen Modata,12,-1,5,75,0,0,0,0,0',
                           'General/fixed'        => 'courier,12,-1,5,50,0,0,0,0,0',
                           'General/font'         => 'MgOpen Modata,12,-1,5,50,0,0,0,0,0',
                           'General/menuFont'     => 'MgOpen Modata,12,-1,5,50,0,0,0,0,0',
                           'General/taskbarFont'  => 'MgOpen Modata,11,-1,5,50,0,0,0,0,0',
                           'General/toolBarFont'  => 'MgOpen Modata,10,-1,5,50,0,0,0,0,0',
                         },
	'Hebrew'        => { 'Locale/Country' => 'il',
	                     'Locale/Language' => 'he_IL',
			     'Locale/Charset' => 'iso-8859-8',
			 },
        'Irish (UK)'    => { 'Locale/Country' => 'ie',
                           'Locale/Language' => 'en',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'Spanish'       => { 'Locale/Country' => 'es',
                           'Locale/Language' => 'es_ES',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'Spanish (AR)'  => { 'Locale/Country' => 'ar',
                           'Locale/Language' => 'es_AR',
                           'Locale/Charset'  => 'iso8859-1',
                        },
        'Spanish (MX)'  => { 'Locale/Country' => 'mx',
                           'Locale/Language' => 'es_MX',
                           'Locale/Charset' => 'iso8859-15',
                         },
        'Spanish (PE)'  => { 'Locale/Country' => 'pe',
                           'Locale/Language' => 'es_PE',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'Finnish'       => { 'Locale/Country' => 'fi',
                           'Locale/Language' => 'fi_FI@euro',
                           'Locale/Charset' => 'iso8859-15',
                         },
        'French (BE)'    => { 'Locale/Country' => 'be',
                           'Locale/Language' => 'fr_BE',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'French (CA)'   => { 'Locale/Country' => 'ca',
                           'Locale/Language' => 'fr',
                           'Locale/Charset'  => 'iso8859-1',
                         },
        'French'        => { 'Locale/Country' => 'fr',
                           'Locale/Language' => 'fr_FR',
                           'Locale/Charset'  => 'iso8859-15',
                          },
        'Italian'       => { 'Locale/Country'   => 'it',
                           'Locale/Language' => 'it_IT',
                           'Locale/Charset' => 'iso8859-15',
                         },
        'Japanese'      => { 'Locale/Country'   => 'jp',
                           'Locale/Language' => 'ja_JP',
                           'Locale/Charset' => 'EUC-JP',
                         },
        'Latvian'       => { 'Locale/Country'   => 'lv',
                           'Locale/Language'  => 'lv',
                           'Locale/Charset'   => 'iso8859-13',
                         },
        'Korean'        => { 'Locale/Country'   => 'kr',
                           'Locale/Language'  => 'ko',
                           'Locale/Charset' => 'EUC-KR',
                         },
        'Norwegian'     => { 'Locale/Country'   => 'no', # no_NO and nb_NO is the same
                           'Locale/Language'  => 'nb',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'New Norwegian' => { 'Locale/Country'   => 'no',
                           'Locale/Language'  => 'nn',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'Polish'        => { 'Locale/Country' => 'pl',
                           'Locale/Language' => 'pl_PL',
                           'Locale/Charset' => 'iso8859-2',
                         },
        'Saami'         => { 'Locale/Country'   => 'no',
                           'Locale/Language'  => 'se',
                           'Locale/Charset'  => 'iso10646-1',
                         },
#       'sv_SE'       => { 'Locale/Country'  => 'se',
#                          'Locale/Language' => 'sv',
#                          'Locale/Charset'  => '',
#                        },
        'Turkish'       => { 'Locale/Country' => 'tr',
                           'Locale/Language' => 'tr_TR',
                           'Locale/Charset' => 'iso8859-9'
			 },
        'Swedish'       => { 'Locale/Country'   => 'se',
                           'Locale/Language'  => 'sv',
                           'Locale/Charset'  => 'iso8859-1',
                         },
             );

my %lang_locale_map = get_lang_map();
my $language = choose_language($lang, \%lang_locale_map);
log_msg("$0: Language: $language");
            
# We construct a similar associative array for use with kdmrc.
# We copy only the values ''Locale/Language'' from the %lang_map
my %kdmlang_map;
for my $lang (sort keys %lang_map) {
    $kdmlang_map{'X-*-Greeter/Language'} = $lang_map{$language}{'Locale/Language'};
}

for my $key (sort keys %kdmlang_map) {
    if (defined($kdmlang_map{$key})) {
        log_msg("$0: ".$key.":".$kdmlang_map{$key});
    }
}

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

## Not needed for lenny as correct settings are chosen automatically
# Call postconfig() with the appropriate locale/keyboard pair
#if(defined(my $conf = $lang_map{$language})) {
#    for my $key (sort keys %$conf) {
#        log_msg("$0: ".$key.":".$conf->{$key});
#    }
#    postconfig('true', $global, $conf);
#} else {
#    log_msg("$0: No support for language $lang");
#    exit;
#}

# And once more for KDM, but only if it exists
if (-e $kdmrc ) {
    if (defined($kdmlang_map{'X-*-Greeter/Language'})) {
        postconfig('false', $kdmrc, \%kdmlang_map);
    }
} else { 
  log_msg("$0: $kdmrc does not exist");
  exit;
}
