#!/usr/bin/perl
# This script configures mozilla (all versions)
# TODO Also modify 'intl.font_charset'

use strict;
use warnings;

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

my $file = '/etc/mozilla/prefs.js';

# Define the associative array to hold key/value pairs
my %lang_map = (
                #entries sorted alphabetically
                'ca_ES@euro' => { LANGUAGE => 'ca,en' },
                'da_DK'      => { LANGUAGE => 'da,en' },
                'de_BE@euro' => { LANGUAGE => 'de,en' },
                'de_DE@euro' => { LANGUAGE => 'de,en' },
                'el_GR.UTF-8'=> { LANGUAGE => 'el,en' },
                'es_AR'      => { LANGUAGE => 'es,en' },
                'es_DO'      => { LANGUAGE => 'es,en' },
                'es_ES'      => { LANGUAGE => 'es,en' },
                'es_ES@euro' => { LANGUAGE => 'es,en' },
                'es_GT'      => { LANGUAGE => 'es,en' },
                'es_HN'      => { LANGUAGE => 'es,en' },
                'es_MX'      => { LANGUAGE => 'es,en' },
                'es_PA'      => { LANGUAGE => 'es,en' },
                'es_PE'      => { LANGUAGE => 'es,en' },
                'es_SV'      => { LANGUAGE => 'es,en' },
                'fi_FI@euro' => { LANGUAGE => 'fi,en' },
                'fr_BE@euro' => { LANGUAGE => 'fr,en' },
                'fr_CA'      => { LANGUAGE => 'fr,en' },
                'fr_FR@euro' => { LANGUAGE => 'fr,en' },
                'it_IT@euro' => { LANGUAGE => 'it,en' },
                'lv_LV'      => { LANGUAGE => 'lv,en' }, 
                'nb_NO'      => { LANGUAGE => 'no,en' }, 
                'nl_BE@euro' => { LANGUAGE => 'nl,en' },
                'nl_NL@euro' => { LANGUAGE => 'nl,en' },
                'nn_NO'      => { LANGUAGE => 'no,en' },
                'no_NO'      => { LANGUAGE => 'no,en' }, 
                'pl_PL'      => { LANGUAGE => 'po,en' },
                'pt_BR'      => { LANGUAGE => 'pt,en' },
                'se_NO'      => { LANGUAGE => 'se,en' }, 
                'sv_SE'      => { LANGUAGE => 'sv,en' },
                'tr_TR'      => { LANGUAGE => 'tr,en' },
                'uk_UA'      => { LANGUAGE => 'uk,en' },
                );

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

# List supported locales
if ("supported" eq $lang) {
    for $lang (sort keys %lang_map) {
        print "$lang\n";
    }
    exit 0;
}

# If there is a corresponding locale, update
# the 'PREFERRED_LANGUAGE' entry in the links config file.
my %mozilla;
my $mozilla_lang;
if(defined(my $conf = $lang_map{$lang})) {
    $mozilla_lang = $conf->{'LANGUAGE'};
    $mozilla{'useragent'} = "$mozilla_lang";
    $mozilla{'useragent'} =~ s/,.*$//g;
    $mozilla{'accept'} = "$mozilla_lang";
} else {
# We just work around this by defining it ourselves, just
# use the locale as "XX_XX" and change to "XX-XX,en"
# This is probably a bit ugly, but it avoids having everything
# in a database when the transform function is always the same...
    if ( $lang =~ /^..$/ || $lang =~ /^.._..$/ || $lang =~ /^.._..\@.*?$/ ) {
    	$mozilla_lang = $lang;
    # Remove charset
	$mozilla_lang =~ s/\@.*$//g;
    # Fix '_'
	$mozilla_lang =~ s/_/-/g;
# Useragent
	$mozilla{'useragent'} = "$mozilla_lang";
# Append 'en' to accepted languages
	$mozilla{'accept'} = "$mozilla_lang,en";
    } else {
        log_msg("$0: No support for language $lang");
        exit;
    }
}

log_msg("$0: Lynx language: $mozilla_lang");

# Set UserAgent
my $findtext;
my $newtext;
$findtext= "pref(\"general.useragent.locale\"";
$newtext = "pref(\"general.useragent.locale\", \"$mozilla{useragent}\");\n";
UpdateCfgFile( $file, $findtext, $newtext )
       or log_msg("$0: Can't save file: $!");
$findtext= "pref(\"intl.accept_languages\"";
$newtext = "pref(\"intl.accept_languages\", \"$mozilla{accept}\");\n";
UpdateCfgFile( $file, $findtext, $newtext )
       or log_die("$0: Can't save file: $!");

# This is the helper subroutine to change the value of the variable
# This should eventually be changed to a more generic version
# and used throughout the rest of the scripts.
sub UpdateCfgFile {
    my ($file, $findtext, $newline) = @_;    

    my $updated;

    my @lines;
    if (open(FILE, "<$file")) {
        @lines = <FILE>;
        close(FILE);
    }
    
    my $i = 0;
    foreach (@lines) {
        if ($lines[$i]=~/^\Q${findtext}\E/) {
            $lines[$i] = $newline;
            $updated=1;
        }  
        $i++;
    }

    # If !updated the option was not found.
    # So we append it at the end of the file.
    if (!$updated) {
        $lines[$i] = $newline;
        $updated=1;
    }

    if ($updated) {
        log_msg("$0:UpdateCfgFile(): Updating $file");
        open(FILE, ">$file.new") or log_die("$0:UpdateCfgFile(): Unable to write to $file.new");
        print FILE @lines;
        close(FILE);
        rename "$file.new", $file;
    }
    return $updated;
}
