#!/usr/bin/perl
# This script configures XFree86 (sarge version 4.3.x)
# The difference of the woody script is in some of the debconf 
# values, otherwise they are similar.

use strict;
use warnings;

# We use preseed() helper subroutine so we have to include
# its definition
require '/usr/lib/localization-config/common/common.pl';
require '/usr/lib/localization-config/common/preseed.pl';

# Define the package name
my $package = "xserver-xfree86";

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

# We define an associative arrays with the debconf keys
# These will be used in combination with the values in the
# next array
my %keynames = ( LAYOUT     => 'xserver-xfree86/config/inputdevice/keyboard/layout',
                 XKBOPTIONS => 'xserver-xfree86/config/inputdevice/keyboard/options',
		 XKBMODEL   => 'xserver-xfree86/config/inputdevice/keyboard/model',
		 XKBVARIANT => 'xserver-xfree86/config/inputdevice/keyboard/variant'
               );

# keyboard layout name
# one of: am, az, be, bg, br, ca, cs, de, dk, dvorak, ee, el, es, fi, 
# fr, gb, hu, il, is, it, la, lt, mk, no, pl, pt, ro, ru, se, si, sr,
# th, tr, ua, us, vn
# The XKBOPTIONS holds the value of the corresponding XkbOptions found
# in the XF86Config-4 file. For example to enable keymap switching for
# the Greek keyboard, one possible option is 'grp:ctrl_shift_toggle'.

my %lang_map = (
                    # entries sorted alphabetically
		    'bs_BA.ISO8859-2' => { LAYOUT => 'us,bs', XKBOPTIONS => 'grp:alt_shift_toggle', XKBMODEL => 'pc105' },
                    'bg_BG'        => { LAYOUT => 'us,bg', XKBVARIANT => ',bds', XKBOPTIONS => 'grp:alt_shift_toggle,grp_led:scroll' },
                    'ca_ES@euro'   => { LAYOUT => 'es', XKBOPTIONS => '', XKBMODEL => 'pc105' },
		    'cs_CZ'        => { LAYOUT => 'us,cz_qwerty', XKBOPTIONS => 'grp:shift_toggle,grp_led:scroll' },
                    'da_DK'        => { LAYOUT => 'dk', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'de_BE@euro'   => { LAYOUT => 'de', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'de_CH'        => { LAYOUT => 'de_CH', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'de_DE@euro'   => { LAYOUT => 'de', XKBOPTIONS => 'nodeadkeys', XKBMODEL => 'pc105' },
                    'el_GR.UTF-8'  => { LAYOUT => 'us,el', XKBOPTIONS => 'grp:alt_shift_toggle', XKBMODEL => 'pc105' },
                    'en_AU'        => { LAYOUT => 'us', XKBOPTIONS => '' },
                    'en_CA'        => { LAYOUT => 'us', XKBOPTIONS => '' },
                    'en_IE@euro'   => { LAYOUT => 'gb', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'ga_IE@euro'   => { LAYOUT => 'gb', XKBOPTIONS => '', XKBMODEL => 'pc105' },
		    'en_GB'        => { LAYOUT => 'gb', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'gd_GB'        => { LAYOUT => 'gb', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'gv_GB'        => { LAYOUT => 'gb', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'kw_GB'        => { LAYOUT => 'gb', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'en_US'        => { LAYOUT => 'us', XKBOPTIONS => '' },
                    'es_AR'        => { LAYOUT => 'es', XKBOPTIONS => '' },
                    'es_ES@euro'   => { LAYOUT => 'es', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'es_MX'        => { LAYOUT => 'la', XKBOPTIONS => '' },
                    'es_PE'        => { LAYOUT => 'es_la', XKBOPTIONS => '' },
                    'fa_IR.UTF-8'  => { LAYOUT => 'us,fa', XKBOPTIONS => 'grp:ctrl_shift_toggle,grp_led:scroll', XKBMODEL => 'pc105' },
                    'fi_FI'        => { LAYOUT => 'fi', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'fi_FI@euro'   => { LAYOUT => 'fi', XKBOPTIONS => '', XKBMODEL => 'pc105' },
		    'fr_BE'        => { LAYOUT => 'be', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'fr_BE@euro'   => { LAYOUT => 'be', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'fr_CA'        => { LAYOUT => 'us', XKBOPTIONS => '' },
		    'fr_CH'        => { LAYOUT => 'fr_CH', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'fr_FR@euro'   => { LAYOUT => 'fr', XKBOPTIONS => '', XKBMODEL => 'pc105' },
		    'he_IL'        => { LAYOUT => 'us,il', XKBOPTIONS => 'grp:alt_shift_toggle' },
                    'it_IT@euro'   => { LAYOUT => 'it', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'ja_JP.EUC-JP' => { LAYOUT => 'jp', XKBOPTIONS => '', XKBMODEL => 'jp106' },
                    'ko_KR'        => { LAYOUT => 'us', XKBOPTIONS => '' },
                    'lv_LV'        => { LAYOUT => 'lv', XKBOPTIONS => '' },
                    'lt_LT'        => { LAYOUT => 'us,lt', XKBOPTIONS => 'grp:ctrl_shift_toggle', XKBMODEL => 'pc105' },
                    'nb_NO'        => { LAYOUT => 'no', XKBOPTIONS => '', XKBMODEL => 'pc105' }, # Same as no_NO
                    'nds_DE'       => { LAYOUT => 'de', XKBOPTIONS => '' },
                    'nl_BE@euro'   => { LAYOUT => 'be', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'nl_NL@euro'   => { LAYOUT => 'us', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'nn_NO'        => { LAYOUT => 'no', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'no_NO'        => { LAYOUT => 'no', XKBOPTIONS => '', XKBMODEL => 'pc105' }, # Same as nb_NO
                    'pl_PL'        => { LAYOUT => 'pl', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'pt_BR'        => { LAYOUT => 'br', XKBOPTIONS => '', XKBMODEL => 'abnt2', XKBVARIANT => 'abnt2' },
                    'ru_RU'        => { LAYOUT => 'us,ru', XKBOPTIONS => 'grp:ctrl_shift_toggle,grp_led:scroll', XKBMODEL => 'pc105' },
                    'se_NO'        => { LAYOUT => 'sapmi', XKBOPTIONS => '' },
		    'sk_SK'        => { LAYOUT => 'us,sk_qwerty', XKBOPTIONS => 'grp:shift_toggle,grp_led:scroll' },
		    'sl_SI'        => { LAYOUT => 'si,us', XKBOPTIONS => 'grp:alt_shift_toggle', XKBMODEL => 'pc105' },
                    'sv_SE'        => { LAYOUT => 'se', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'sv_FI'        => { LAYOUT => 'fi', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'sv_FI@euro'   => { LAYOUT => 'fi', XKBOPTIONS => '', XKBMODEL => 'pc105' },
                    'tr_TR'        => { LAYOUT => 'tr', XKBOPTIONS => 'caps:shift', XKBMODEL => 'pc105' },
                    'uk_UA'        => { LAYOUT => 'us,ua', XKBOPTIONS => 'grp_led:caps,grp:ctrl_shift_toggle', XKBMODEL => 'pc105'},
                   ) ;

sub get_lang_map() {
    return %lang_map;
}


# Define the subarch map. Subarches, esp. in the case of powerpc need
# special treatment. In particular, PowerMacs use 'macintosh' XKbModel,
# while PReP/CHRP use plain pc104/5. If our case we have a powermac/apus,
# then we don't set the XkbModel, but rather let X configuration to set
# it. If on the other hand, we have a PReP/CHRP system we can safely
# preseed the values according to the locale chosen.
my %subarch_map = (  'ppcbug'               => { SET_XKBMODEL => 'true' },
                     'prep'                 => { SET_XKBMODEL => 'true' },
                     'chrp'                 => { SET_XKBMODEL => 'true' },
                     'amiga'                => { SET_XKBMODEL => 'false' },
                     'pmac_newworld'        => { SET_XKBMODEL => 'false' },
                     'pmac_oldworld'        => { SET_XKBMODEL => 'false' },
		     'none'                 => { SET_XKBMODEL => 'true' }
		     );

# Get the subarch
my $subarch = get_subarch();

# If SET_XKBMODEL is set to false, then remove the XKBMODEL from the keynames
# associative array.
if ($subarch_map{$subarch}{'SET_XKBMODEL'} eq 'false') {
    delete $keynames{'XKBMODEL'};
}

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

# Call the preseed() subroutine to set the package debconf values.
preseed($package, $lang, \%lang_map, \%keynames);

1;
