#!/usr/bin/perl
# This script configures lynx (all versions)
# TODO: Should also set PREFERRED_CHARSET

use strict;
use warnings;

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

my $file = '/etc/lynx.cfg';

# 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 $links_lang = "";
if(defined(my $conf = $lang_map{$lang})) {
    $links_lang = $conf->{'LANGUAGE'};
} 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 =~ /^.._..\@.*?$/ ) {
    	$links_lang = $lang;
    # Remove charset
	$links_lang =~ s/\@.*$//g;
    # Fix '_'
	$links_lang =~ s/_/-/g;
    # Append 'en'
	$links_lang = "$links_lang,en";
    } else {
        log_msg("$0: No support for language $lang");
        exit;
    }
}

log_msg("$0: Lynx language: $links_lang");
UpdateCfgFile($file, $links_lang)
       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, $value) = @_;    
    my $newline = "PREFERRED_LANGUAGE:$value";
    my $updated;

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

    # If !updated the "PREFERRED_LANGUAGE" 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: Unable to write to $file.new");
        print FILE @lines;
        close(FILE);
        rename "$file.new", $file;
    }
    return $updated;
}
