#!/usr/bin/perl
# This script configures opera6

use strict;
use warnings;
use Config::IniFiles;

require '/usr/lib/localization-config/common/log.pl';

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

my $file = '/etc/opera6rc';

# The associative array holds key/value pairs with locale as key.
my %lang_map = (
        #entries sorted alphabetically
        'ca_ES@euro'   => { code => 'ca',
                            accept => 'ca-ES,ca,es,en',
                          },
        'da_DK'        => { code   => 'da',
                            accept => 'da,en',
                          },
        'de_BE@euro'   => { code => 'de',
                            accept => 'de,en,fr,nl',
                          },
        'de_DE@euro'   => { code => 'de',
                            accept => 'de,en',
                          },
        'el_GR.UTF-8'  => { code => 'el',
                            accept => 'el,en',
                          },
        'es_ES@euro'   => { code => 'es',
                            accept => 'es-ES,es,en',
                          },
        'es_MX'        => { code => 'es',
                            accept => 'es-MX,es,en',
                          },
#       'fi_FI@euro'   => { code => 'fi',
#                           accept => 'fi,en',
#                         },  
        'fr_BE@euro'   => { code => 'fr',
                            accept => 'fr,nl,en',
                          },
        'fr_CA'        => { code => 'fr',
                            accept => 'fr,en',
                          },
        'fr_FR@euro'   => { code => 'fr',
                            accept => 'fr,en',
                          },
#       'it_IT@euro'   => { code => 'it',
#                           accept => 'it,en',
#                         },  
        'lv_LV'        => { code   => 'lv',
                            accept => 'lv,ru,en',
                          },
        'nb_NO'        => { code   => 'nb', # nb_NO and no_NO is the same
                            accept => 'nb,no,nn,en',
                          },
        'nds_DE'       => { code   => 'nds',
                            accept => 'nds,de,en',
                          },
        'nl_BE@euro'   => { code => 'nl',
                            accept => 'nl,fr,en',
                          },
        'nl_NL@euro'   => { code => 'nl',
                            accept => 'nl,en',
                          },
        'nn_NO'        => { code   => 'nn',
                            accept => 'nn,no,nb,en',
                          },
        'no_NO'        => { code   => 'nb',
                            accept => 'nb,no,nn,en',
                          },
        'pl_PL'        => { code => 'pl',
                            accept => 'pl,en',
                          },
        'pt_BR'        => { code => 'pt-BR',
                            accept => 'pt-BR,pt,en',
                          },
        'se_NO'        => { code   => 'nb',
                            accept => 'se,nb,no,nn,en',
                          },
#       'sv_SE'        => { code => 'sv',
#                           accept => 'sv,en',
#                         },  
        'tr_TR'        => { code   => 'tr',
                            accept => 'tr,en',
                          },
             );

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

# We need the configuration file, exit otherwise
exit unless -e $file;

# This part configures the opera6 locale configurations
# This should eventually be rewritten to use one of the
# helper subroutines...
if(defined(my $conf = $lang_map{$lang})) {
    my $ini = new Config::IniFiles( -file => $file )
        or log_die("$0: Can't open $file: $!");

    $ini->newval('User Prefs',
                 'Language Files Directory',
                 '/usr/share/opera/locale/');
    $ini->newval('User Prefs',
                 'Language File',
                 '/usr/share/opera/locale/'.$conf->{code}.'.qm');
    $ini->newval('Adv User Prefs',
                 'HTTP Accept Language',
                 $conf->{accept});
    $ini->RewriteConfig
        or log_die("$0: Can't save $file: $!");
} else {
    log_msg("$0: No support for language $lang");
    exit;
}
