#!/usr/bin/perl

use Debconf::Client::ConfModule ':all';
use Debconf::Log;

sub my_warn { 
   # Don't dump warnings into STDERR, as it will choke the return
   # of results from debconf to install-keymap. Instead put the 
   # warnings into debconf's debugging channel.
   Debconf::Log::debug developer => "getkmapchoice warning: ".join(" ",@_);
}

sub my_die {
    # Don't die horribly, as install-keymap will then crash.
    # Instead, dump the warning into debconf's logs,
    # and return NONE, which hopefully will Do The Right Thing.
    my_warn @_;
    print STDERR "NONE";
    exit 0;
}

#####COMMON#####

sub readfiles($) {
  my ($path) = @_;

  opendir (DIR, $path) or my_die "Can't open directory \`$path': $!";
  my @files = grep { ! /^\./ && -f "$path/$_" } readdir(DIR);
  closedir DIR;

  foreach my $file (@files) {
#    print STDERR "Loading $defsdir/$file\n";
    require "$defsdir/$file";
  }
}

($ret, $policy) = get ('console-data/keymap/policy');
if ($policy eq "Don't touch keymap") {
  # No keymap
  $filename = 'NONE';
} elsif ($policy eq 'Select keymap from full list') {
  ($ret, $filename) = get ('console-data/keymap/full');
  if (! $filename) {
      $filename = 'NONE';
  }
} elsif ($policy eq 'Keep kernel keymap') {
  # No keymap
  $filename = 'KERNEL';
} else {
  # Get infos about selected keymap
  ($ret, $family) = get ('console-data/keymap/family');
  $cfamily = correctname $family;
  my_die "No keyboard family selected: $family" unless $ret == 0;

  # This should only occur when no keymap is available
  if ($family eq '') {
    # Tell other programs (eg. install-keymap) not to do anything
    print STDERR "NONE";
    exit 0;
  }

  ($ret, $layout) = get ("console-data/keymap/$cfamily/layout");
  $clayout = correctname $layout;
  if ($ret != 0) {
    # Oops. mssing entry. recover if possible,
    my_warn "No keyboard layout selected: $layout";
    if ( ! $FILE ) {
      my_die "No keymap selected" ;
    } else { 
      print STDERR $FILE; 
      exit 0;
    }
  }
  ($ret, $kbdvariant) = get ("console-data/keymap/$cfamily/$clayout/variant");
  $ckbdvariant = correctname $kbdvariant;
 if ($ret != 0) {
     # Oops. mssing entry. recover if possible,
     my_warn "No keyboard variant selected: $kbdvariant";
     if ( ! $FILE ) {
        my_die "No keymap selected" ;
     } else {
        print STDERR $FILE;
        exit 0;
     }
 }
  ($ret, $mapvariant) = get ("console-data/keymap/$cfamily/$clayout/$ckbdvariant/keymap");
  if ($ret != 0) {
    # Oops. mssing entry. recover if possible,
    my_warn "No keymap variant selected: $mapvariant";
    if ( ! $FILE ) {
      my_die "No keymap selected" ;
    } else {
      print STDERR $FILE;
      exit 0;
    }
  }


  # Get list of keymap definitions
  $defsdir = "/usr/share/console/lists/keymaps";
  $keymaps = {};
  readfiles ($defsdir);


  # Find the right one
  foreach my $kbdarch (@{$keymap_defs->{archsets}->{guess_arch($keymap_defs)}}) {
    my $maps = $keymaps->{$kbdarch};

    $maps = $maps->{$family};
    unless (defined $maps) {
      my_warn "Family not found ($family)"; 
      $maps = $keymaps->{$kbdarch};
      $maps = $maps->{Unknown};
      if (defined $maps) {
        my_warn "Using $maps instead";
      }
      next;
    }

    # 'Norwegian' had a typo in a previous version thus some people still
    # have this string in their debconf database.
    $layout =~ s/Norvegian/Norwegian/;

    $maps = $maps->{$layout};
    unless (defined $maps) {
      my_warn "Layout not found ($layout)"; next;
    }

    $maps = $maps->{$kbdvariant};
    unless (defined $maps) {
      my_warn "Keyboard variant not found ($kbdvariant)"; next;
    }

    $filename = $maps->{$mapvariant};
    unless (defined $filename) {
      my_warn "Keymap variant not found ($mapvariant)"; next;
    }
  }
}

my_die "No matching map found" unless defined $filename;

# output result
print STDERR "$filename\n";
