home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / dictionaries-common / dc-debconf-select.pl < prev    next >
Encoding:
Text File  |  2011-01-12  |  7.4 KB  |  204 lines

  1. # ---------------------------------------------------------------------------
  2. # dc-debconf-select.pl:
  3. #  This file will be added to end of dictionaries-common.config-base
  4. #  to make dictionaries-common.config, as well as installed under
  5. #  /usr/share/dictionaries-common for single ispell dicts/wordlists use
  6. # ---------------------------------------------------------------------------
  7.  
  8. sub dico_get_packages (){
  9.   # Get list of packages sharing the question
  10.   my $class    = shift;
  11.   my $question = "shared/packages-$class";
  12.  
  13.   my ($errorcode,$packages) = metaget ($question, "owners");
  14.   return [ split (/\s*,\s*/, $packages) ] unless $errorcode;
  15. }
  16.  
  17. sub dico_parse_languages (){
  18.   # Get a hash reference of package -> list of (e)languages provided by package
  19.   my $class    = shift;
  20.   my $variant  = shift;
  21.   my $packages = shift;
  22.   my %tmphash  = ();
  23.  
  24.   die "No variant (languages|elanguages) string supplied\n" unless $variant;
  25.  
  26.   $packages = &dico_get_packages($class) unless $packages;
  27.  
  28.   foreach $pkg ( @$packages ){
  29.     my ($errorcode, $entry ) = metaget("$pkg/$variant", "default");
  30.     unless ( $errorcode ){
  31.       $entry =~ s/^\s+//;
  32.       $entry =~ s/\s+$//;
  33.       $tmphash{$pkg} = $entry;
  34.     }
  35.   }
  36.   return \%tmphash;
  37. }
  38.  
  39. sub dico_get_all_choices (){
  40.   # Get $choices and $echoices parallel lists sorted after $echoices and formatted for debconf
  41.   my $class       = shift;
  42.   my $languages   = shift;
  43.   my $debug       = 1 if exists $ENV{'DICT_COMMON_DEBUG'};
  44.   my %mappinghash = ();
  45.   my $debug_prefix = "[$class,dico_get_all_choices]";
  46.  
  47.   $languages   = &dico_parse_languages($class,"languages") unless $languages;
  48.  
  49.   my $elanguages  = &dico_parse_languages($class,"elanguages",[ keys %$languages ]);
  50.  
  51.   if ( $debug ){
  52.     print STDERR "-------- $debug_prefix start --------\n";
  53.     my $langlist  = join(', ',sort keys %{$languages});
  54.     my $elanglist = join(', ',sort keys %{$elanguages});
  55.     print STDERR " * Packages with languages: $langlist\n"  if $debug;
  56.     print STDERR " * Packages with elanguages: $elanglist\n" if $debug;
  57.   }
  58.  
  59.   foreach $pkg ( keys %$languages ){
  60.     my @langs  = split(/\s*,\s*/, $languages->{$pkg});
  61.     my @elangs = @langs;
  62.     if ( exists $elanguages->{$pkg} ){
  63.       my @tmp = split(/\s*,\s*/, $elanguages->{$pkg});
  64.       if ( $debug ){
  65.     print STDERR " langs: $#langs, "  . join(', ',@langs)  . "\n";
  66.     print STDERR " tmp:   $#tmp, "    . join(', ',@tmp)    . "\n";
  67.       }
  68.       @elangs = @tmp if ( $#langs == $#tmp );
  69.     }
  70.     foreach $index ( 0 .. $#langs ){
  71.       $mappinghash{$langs[$index]} = $elangs[$index];
  72.     }
  73.   }
  74.   my $echoices = join(', ', sort {lc($a) cmp lc($b)} values %mappinghash);
  75.   my $choices  = join(', ',
  76.               sort {lc($mappinghash{$a}) cmp lc($mappinghash{$b})}
  77.               keys %mappinghash);
  78.   if ( $debug ){
  79.     print STDERR "- Choices:\n[$choices]\n";
  80.     print STDERR "- Echoices:\n[$echoices]\n";
  81.     print STDERR "-------- $debug_prefix end --------\n";
  82.   }
  83.   return $choices, $echoices;
  84. }
  85.  
  86. sub dc_debconf_select (){
  87.   my $class       = shift;
  88.   my $priority    = shift;
  89.   my $question    = "dictionaries-common/default-$class";
  90.   my $packages    = &dico_get_packages($class);
  91.   my $debug       = 1 if exists $ENV{'DICT_COMMON_DEBUG'};
  92.   my $reconfigure = 1 if exists $ENV{'DEBCONF_RECONFIGURE'};
  93.   my $flagdir     = "/var/cache/dictionaries-common";
  94.   my $newflag     = "$flagdir/flag-$class-new";
  95.   my $langscript  = "/usr/share/dictionaries-common/dc-debconf-default-value.pl";
  96.   my $echoices;
  97.   my @oldchoices  = ();
  98.   my %newchoices  = ();
  99.   my %title       = ('ispell'   => "Dictionaries-common: Ispell dictionary",
  100.              'wordlist' => "Dictionaries-common: Wordlist dictionary"
  101.              );
  102.   my $debug_prefix = "[$class,dc_debconf_select]";
  103.  
  104.   return unless $packages;
  105.  
  106.   print STDERR "----- $debug_prefix start -----------\n" if $debug;
  107.  
  108.   # Get new base list of provided languages
  109.   my $languages = &dico_parse_languages($class,"languages",$packages);
  110.   foreach $pkg ( keys %$languages ) {
  111.     foreach $lang ( split(/\s*,\s*/, $languages->{$pkg}) ){
  112.       $newchoices{$lang}++;
  113.     }
  114.   }
  115.   my $choices = join (', ', sort {lc($a) cmp lc($b)} keys %newchoices);
  116.  
  117.   # Get old list of provided languages
  118.   @oldchoices = split(/\s*,\s*/,metaget ($question, "choices-c"));
  119.   pop @oldchoices;            # Remove the manual entry
  120.   my $oldchoices = join (', ', sort {lc($a) cmp lc($b)} @oldchoices);
  121.  
  122.   # If dictionaries-common is already installed (-r $langscript),
  123.   # there are elements for this chass to be installed (%newchoices)
  124.   # and there were none before (! $oldchoices), means that we are installing
  125.   # for the first time elements in this class, with dictionaries-common
  126.   # already installed. Try getting a reasonable default value
  127.   if ( -r $langscript && %newchoices && ! $oldchoices ){
  128.     print STDERR "$debug_prefix: Configuring $class class for the first time\n\n" if $debug;
  129.     require $langscript;
  130.     &dc_set_default_value_for_class($class);
  131.   }
  132.  
  133.   # Read current value of default ispell dict / wordlist.
  134.   my $curval  = get ($question) || "undefined";
  135.  
  136.   if ( scalar %newchoices ) {
  137.     # If $priority is set &dc_set_default_value_for_class found something.
  138.     # This will usually be as much "medium", so honour it.
  139.     unless ( $priority ){
  140.       if ( $curval =~ /^Manual.*/ or exists $newchoices{$curval} ){
  141.     # Use priority "medium" if current value is in the new list or mode is set to manual.
  142.     $priority = "medium";     #
  143.       } else {
  144.     # Otherwise we have a wrong value with no associated entry.
  145.     # This is an *error* that needs to be signalled and acted upon.
  146.     # For this reason priority must be higher than the standard one.
  147.     # We leave it as "high" instead of "critical" so question can be
  148.     # overriden in special cases until underlying bug is fixed.
  149.     print STDERR "$debug_prefix error: [$curval] does not correspond to any package\n";
  150.     $priority = "high";
  151.       }
  152.     }
  153.   } else {
  154.     $priority = "low";
  155.     print STDERR "$debug_prefix info: No elements in given class.\n" if $debug;
  156.   }
  157.  
  158.   print STDERR
  159.     "$debug_prefix:\n" .
  160.     " Class: $class, Priority: $priority\n" .
  161.     " Question: $question, Previous or guessed value: $curval\n" .
  162.     " New choices:[$choices]\n" .
  163.     " Old choices:[$oldchoices]\n" if $debug;
  164.  
  165.   # May ask question if there is no match
  166.   if ( scalar %newchoices ) {
  167.     if ( $choices ne $oldchoices) {
  168.       fset ($question, "seen", "false");
  169.       # Let future processes in this apt run know that a new $class element is to be installed
  170.       if ( -d $flagdir ) {
  171.     open ($FLAG,"> $newflag")
  172.       or die "Could not open $newflag for write. Aborting ...\n";
  173.     print $FLAG "1\n";
  174.     close $FLAG;
  175.       }
  176.     }
  177.     my ( $errorcode, $seen ) = fget($question, "seen");
  178.     if ( $seen eq "false" or $reconfigure ){
  179.       ($choices, $echoices ) = &dico_get_all_choices($class,$languages);
  180.       subst ($question, "choices", $choices);
  181.       subst ($question, "echoices", $echoices);
  182.     }
  183.     input ($priority, $question);
  184.     title ($title{$class});
  185.     go ();
  186.     subst ($question, "echoices", $choices); # Be backwards consistent
  187.   }
  188.  
  189.   # If called from dictionaries-common.config, check actual values in debug mode
  190.   if ( $debug && $fromdcconfig ){
  191.     print STDERR "dictionaries-common.config: Checking really set values for $question\n";
  192.     print STDERR " Choices-C string: " . metaget ($question, "choices-c") . "\n";
  193.     print STDERR " Really set value: " . get ($question) . "\n";
  194.   }
  195.   print STDERR "----- $debug_prefix end -----------\n" if $debug;
  196. }
  197.  
  198. # Local Variables:
  199. # perl-indent-level: 2
  200. # End:
  201.  
  202. 1;
  203.  
  204.