#!/usr/bin/perl -w

use Debian::DictionariesCommon q(:all);
use Debconf::Client::ConfModule q(:all);

die "$0: You must run this as root.\n" if ($> != 0);

die "Usage: $0 package-name\n" if (scalar @ARGV != 1);

version ('2.0');

sub get_choices (){
  my $class      = shift;
  my $question   = "shared/packages-$class";
  my @choices = ();
  
  my ($errorcode,$pkgowners) = metaget ($question, "owners");
  return if $errorcode;
  
  foreach (split (/\s*,\s*/, $pkgowners)){
    my $entry = metaget ("$_/languages", "default");
    for ( $entry ){           # Trim leading/trailing whitespaces
      s/^\s+//;
      s/\s+$//;
    }
    push (@choices, split(/\s*,\s*/, $entry));
  }
  return sort {lc $a cmp lc $b} @choices;
}

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

my $class    = "ispell";
my $question = "dictionaries-common/default-$class";

($ret, $value) = get $question;

if ($ret == 0) {

  updatedb ($class);

  my $package      = $ARGV[0];
  my $dictionaries = loaddb ($class);
  my $languages    = metaget ("$package/languages", "default");
  my @newchoices   = ();
  my %langsinpkg   = ();

  for ( $languages ){ # Trim leading/trailing whitespaces
    s/^\s+//;
    s/\s+$//;
  }
  
  foreach ( split (/\s*,\s*/, $languages) ){
    $langsinpkg{$_}++;
  }
  
  foreach $choice ( &get_choices($class) ){
    push (@newchoices, $choice) unless exists $langsinpkg{$choice};
  }
  
  subst ($question, "choices", join (', ', @newchoices));
  
  if ( exists $langsinpkg{$value} ) {
    system "select-default-$class --rebuild";
  } else {
    system "update-default-$class --rebuild";
  }
}

#Local Variables:
#perl-indent-level: 2
#End:

__END__

=head1 NAME

remove-default-ispell - remove default ispell dictionary

=head1 SYNOPSIS

 remove-default-ispell <package>

=head1 DESCRIPTION

WARNING: Not to be used from the command line unless you know very well what you are doing.

When called from package postrm, this program will take care of removing the entries 
associated to a ispell package from the dictionaries-common database 
and call for the new selection if it was the default one.

=head1 SEE ALSO

The dictionaries-common policy document

=head1 AUTHORS

Rafael Laboissiere

=cut

#  LocalWords:  ispell wordlist
