#!/usr/bin/perl
# This script configures KDE 2
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/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/kde2/system.kdeglobals';
my $kdmrc = '/etc/kde2/kdm/kdmrc';

# These are X settings -> see /usr/X11R6/lib/locale for charset names
my %lang_map = (
        #entries sorted alphabetically
	'bs_BA.ISO8859-2'  => { 'Locale/Country'  => 'ba',
                                'Locale/Language' => 'bs_BA',
                                'Locale/Charset'  => 'iso8859-2',
                         },
        'ca_ES@euro'  => { 'Locale/Country'  => 'es',
                           'Locale/Language' => 'ca_ES',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'da_DK'       => { 'Locale/Country'   => 'dk',
                           'Locale/Language' => 'da_DK',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'de_BE@euro'  => { 'Locale/Country' => 'be',
                           'Locale/Language' => 'de_BE',
                           'Locale/Charset'  => 'iso8859-15',
                         },   
        'de_DE@euro'  => { 'Locale/Country' => 'de',
                           'Locale/Language' => 'de',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'el_GR.UTF-8' => { 'Locale/Country' => 'gr',
                           'Locale/Language' => 'el',
                           'Locale/Charset'  => 'iso10646-1',
                           'General/StandardFont' => 'FreeSans,12,-1,5,50,0,0,0,0,0',
                           'General/activeFont'   => 'FreeSans,12,-1,5,75,0,0,0,0,0',
                           'General/fixed'        => 'courier,12,-1,5,50,0,0,0,0,0',
                           'General/font'         => 'FreeSans,12,-1,5,50,0,0,0,0,0',
                           'General/menuFont'     => 'FreeSans,12,-1,5,50,0,0,0,0,0',
                           'General/taskbarFont'  => 'FreeSans,11,-1,5,50,0,0,0,0,0',
                           'General/toolBarFont'  => 'FreeSans,10,-1,5,50,0,0,0,0,0',
                         },
        'en_AU'       => { 'Locale/Country' => 'au',
                           'Locale/Language' => 'en',
                           'Locale/Charset'  => 'iso8859-1',
                        },
        'en_CA'       => { 'Locale/Country' => 'ca',
                           'Locale/Language' => 'en',
                           'Locale/Charset'  => 'iso8859-1',
                         },
        'en_IE@euro'  => { 'Locale/Country' => 'ie',
                           'Locale/Language' => 'en',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'en_US'       => { 'Locale/Country' => 'us',
                           'Locale/Language' => 'en',
                           'Locale/Charset'  => 'iso8859-1',
                         },
        'es_AR'       => { 'Locale/Country' => 'ar',
                           'Locale/Language' => 'es_AR',
                           'Locale/Charset'  => 'iso8859-1',
                        },
        'es_ES@euro'  => { 'Locale/Country' => 'es',
                           'Locale/Language' => 'es_ES',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'es_MX'       => { 'Locale/Country' => 'mx',
                           'Locale/Language' => 'es_MX',
                           'Locale/Charset' => 'iso8859-15',
                         },
        'es_PE'       => { 'Locale/Country' => 'pe',
                           'Locale/Language' => 'es_PE',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'fi_FI@euro'  => { 'Locale/Country' => 'fi',
                           'Locale/Language' => 'fi_FI@euro',
                           'Locale/Charset' => 'iso8859-15',
                         },
        'fr_BE@euro'  => { 'Locale/Country' => 'be',
                           'Locale/Language' => 'fr_BE',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'fr_CA'       => { 'Locale/Country' => 'ca',
                           'Locale/Language' => 'fr',
                           'Locale/Charset'  => 'iso8859-1',
                         },
        'fr_FR@euro'  => { 'Locale/Country' => 'fr',
                           'Locale/Language' => 'fr_FR',
                           'Locale/Charset'  => 'iso8859-15',
                          },
        'it_IT@euro'  => { 'Locale/Country'   => 'it',
                           'Locale/Language' => 'it_IT',
                           'Locale/Charset' => 'iso8859-15',
                         },
        'ja_JP'       => { 'Locale/Country'   => 'jp',
                           'Locale/Language' => 'ja_JP',
                           'Locale/Charset' => 'EUC-JP',
                         },
        'lv_LV'       => { 'Locale/Country'   => 'lv',
                           'Locale/Language'  => 'lv',
                           'Locale/Charset'   => 'iso8859-13',
                         },
        'ko_KR'       => { 'Locale/Country'   => 'kr',
                           'Locale/Language'  => 'ko',
                           'Locale/Charset' => 'EUC-KR',
                         },
        'nb_NO'       => { 'Locale/Country'   => 'no', # no_NO and nb_NO is the same
                           'Locale/Language'  => 'no',
                           'Locale/Charset' => 'iso8859-1',
                         },
#       'nds_DE@euro  => { 'Locale/Country'  => 'de',
#                          'Locale/Language' => 'nds',
#                          'Locale/Charset'  => 'iso8859-15',
#                        },           
        'nl_BE@euro'  => { 'Locale/Country'   => 'be',
                           'Locale/Language' => 'nl_BE',
                           'Locale/Charset'  => 'iso8859-15',
                         },
        'nl_NL@euro'  => { 'Locale/Country'   => 'nl',
                           'Locale/Language'  => 'nl_NL',
                           'Locale/Charset'   => 'iso8859-15',
                         },
        'nn_NO'       => { 'Locale/Country'   => 'no',
                           'Locale/Language'  => 'no_NY',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'no_NO'       => { 'Locale/Country'   => 'no',
                           'Locale/Language'  => 'no',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'pl_PL'       => { 'Locale/Country' => 'pl',
                           'Locale/Language' => 'pl_PL',
                           'Locale/Charset' => 'iso8859-2',
                         },
        'pt_BR'       => { 'Locale/Country' => 'br',
                           'Locale/Language' => 'pt_BR',
                           'Locale/Charset' => 'iso8859-1',
                         },
        'se_NO'       => { 'Locale/Country'   => 'no',
                           'Locale/Language'  => 'se',
                           'Locale/Charset'  => 'iso10646-1',
                         },
#       'sv_SE'       => { 'Locale/Country'  => 'se',
#                          'Locale/Language' => 'sv',
#                          'Locale/Charset'  => '',
#                        },
        'tr_TR'       => { 'Locale/Country' => 'tr',
                           'Locale/Language' => 'tr_TR',
                           'Locale/Charset' => 'iso8859-9'
			 },
             );

# 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{$lang} = {'X-*-Greeter/Language', $lang_map{$lang}{'Locale/Language'}};
}

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

# Call postconfig() with the appropriate locale/keyboard pair
if(defined(my $conf = $lang_map{$lang})) {    
    postconfig('true', $global, $conf);
} else {
    log_msg("$0: No support for language $lang");
}

# And once more for KDM, but only if it exists
exit unless -e $kdmrc;
if(defined(my $conf = $kdmlang_map{$lang})) {    
    postconfig('false', $kdmrc, $conf);
} else {
    log_msg("$0: No support for language $lang");
}
