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

use warnings;
use strict;

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");

# Define the associative array to hold key/value pairs
my %lang_map = (
                #entries sorted alphabetically
                'ca_ES@euro' => { dict => 'catalan' },
                'da_DK'      => { dict => 'danish'  },
                'de_BE@euro' => { dict => 'ngerman' },
                'de_DE@euro' => { dict => 'ngerman' },
#               'el_GR.UTF-8'=> { dict => 'unknown' }, # aspell-el, no ispell?
                'en_AU'      => { dict => 'australian' },
                'en_CA'      => { dict => 'british' },
                'en_IE@euro' => { dict => 'british' },
                'en_US'      => { dict => 'american' },
                'es_AR'      => { dict => 'spanish' },
                'es_ES@euro' => { dict => 'spanish' },
                'es_MX'      => { dict => 'spanish' },
                'es_PE'      => { dict => 'spanish' },
                'fi_FI@euro' => { dict => 'finnish-large'},             
                'fr_BE@euro' => { dict => 'french'  },
                'fr_CA'      => { dict => 'french'  },
                'fr_FR@euro' => { dict => 'french'  },
                'it_IT@euro' => { dict => 'italian' },
                'ja_JP'      => { dict => 'american' },
# Latvian is unknown
#               'lv_LV'      => { dict => 'unknown' },
# Korean do not have ispell support
#               'ko_KR'      => { dict => 'unknown' },
                'nb_NO'      => { dict => 'bokmål'  },
#               'nds_DE'     => { dict => 'unknown' }, # no aspell dictionary
                'nl_BE@euro' => { dict => 'dutch'   },
                'nl_NL@euro' => { dict => 'dutch'   },
                'no_NO'      => { dict => 'bokmål'  },
                'nn_NO'      => { dict => 'nynorsk' },
                'pl_PL'      => { dict => 'polish'},
                'pt_BR'      => { dict => 'brasileiro'},
# Northern Saami is not yet available
#               'se_NO'      => { dict => 'unknown'  },
                'sv_SE'      => { dict => 'swedish' },
                );

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

# If there is an appropriate locale in the lang map
# proceed to make the corresponding ispell dictionary
# the default one.
# For this a few helper subroutines (defined later in this
# file) are used.
if(defined(my $conf = $lang_map{$lang})) {

    # Get the selected dictionary and check if the file exists
    my $selected = $conf->{dict};
    if ( ! -e "/usr/lib/ispell/$selected.hash" ) {
        log_msg("$0: No dictionary for language $selected installed");
        exit 1;
    }
    
    # Get current default dictionary and demote it, whilst 
    # assigning the previously selected one the default ispell dictionary.
    my $current_default = get_default();
    if ($selected ne $current_default) {
        demote_default($current_default);
        make_default($selected);
    } else {
        log_msg("$0: $current_default is already the default ispell dictionary.");
    }
} else {
    log_msg("$0: No support for language $lang");
    exit 1;
}
exit 0;
# ----------------------------------------------------------------------

# Find the current default dictionary, set to None if none found.

sub get_default {
    my $current_default;
    $current_default = `/usr/sbin/update-alternatives --display ispell-dictionary.hash | grep 999 | sed 's+/usr/lib/ispell/++' | sed 's/\.hash//' | awk '{print \$1}'`;
    chomp $current_default;

    if ( ! $current_default ) {
        $current_default="None";
    }
    return $current_default;
}
# ----------------------------------------------------------------------

# Promote the selected dictionary to be the default.

sub make_default {
    my $selected = shift;
    log_msg("$0:make_default(): Making $selected the default ispell dictionary...");

    system("/usr/sbin/update-alternatives --quiet --install /usr/lib/ispell/default.hash ispell-dictionary.hash /usr/lib/ispell/$selected.hash 999 --slave /usr/lib/ispell/default.aff ispell-dictionary.aff /usr/lib/ispell/$selected.aff > /dev/null");

    log_msg("$0:make_default(): done.");
}

# ----------------------------------------------------------------------

# Demote the old default dictionary.

sub demote_default {
    my $current_default = shift;
    log_msg("$0:demote_default(): Demoting $current_default (old default)...");

    system("/usr/sbin/update-alternatives --quiet --install /usr/lib/ispell/default.hash ispell-dictionary.hash /usr/lib/ispell/$current_default.hash 10 --slave /usr/lib/ispell/default.aff ispell-dictionary.aff /usr/lib/ispell/$current_default.aff > /dev/null");

    log_msg("$0:demote_default(): done.");
}
