#!/usr/bin/perl
# This script configures links (woody version)

use strict;
use warnings;

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

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

# Define the associative array to hold key/value pairs
my %lang_map = (
                #entries sorted alphabetically
                'ca_ES@euro' => { LANGUAGE => 'Catalan' },
                'da_DK'      => { LANGUAGE => 'Danish' },
                'de_BE@euro' => { LANGUAGE => 'German' },
                'de_DE@euro' => { LANGUAGE => 'German' },
                'el_GR.UTF-8'=> { LANGUAGE => 'Greek' },
                'es_ES@euro' => { LANGUAGE => 'Spanish' },
                'es_MX'      => { LANGUAGE => 'Spanish' },
                'fi_FI@euro' => { LANGUAGE => 'Finnish' },
                'fr_BE@euro' => { LANGUAGE => 'French' },
                'fr_CA'      => { LANGUAGE => 'French' },
                'fr_FR@euro' => { LANGUAGE => 'French' },
                'it_IT@euro' => { LANGUAGE => 'Italian' },
#               'lv_LV'     => { LANGUAGE => 'lv' }, # not supported by links
                'nb_NO'      => { LANGUAGE => 'Norwegian' }, # Same as no_NO
#                'nds_DE'    => { LANGUAGE => ''},
                'nl_BE@euro' => { LANGUAGE => 'Dutch' },
                'nl_NL@euro' => { LANGUAGE => 'Dutch' },
                'nn_NO'      => { LANGUAGE => 'Norwegian' },
                'no_NO'      => { LANGUAGE => 'Norwegian' }, # Same as nb_NO
                'pl_PL'      => { LANGUAGE => 'Polish' },
                'pt_BR'      => { LANGUAGE => 'Portuguese' },
                'se_NO'      => { LANGUAGE => 'Norwegian' }, # Should use saami
                'sv_SE'      => { LANGUAGE => 'Swedish' },
                'tr_TR'      => { LANGUAGE => 'Turkish' },
                );

# 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 'Language' entry in the links config file.
if(defined(my $conf = $lang_map{$lang})) {
    my $links_lang = $conf->{'LANGUAGE'};
    log_msg("$0: Links language: $links_lang");
    UpdateCfgFile($file, $links_lang)
            or log_die("$0: Can't save file: $!");
} else {
    log_msg("$0: No support for language $lang");
}

# 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 = "language \"$value\"\n";
    my $updated;

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

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