#!/usr/bin/perl -w
#
# Registration with aspell dictionary

use strict;

my $class = "aspell";
my $program = "installdeb-$class";
my $debug;

# =======================================================
# Main part
# =======================================================

use Text::Wrap;
$Text::Wrap::columns = 72;

use Debian::Debhelper::Dh_Lib;
use Debian::DictionariesCommon q(:all);
use Devel::DictionariesCommon q(:all);

if ( "$class" eq "wordlist" or "$class" eq "ispell" ){
  init( options => { 'write-elanguages'  => \$dh{WRITE_ELANGUAGES},
		     'no-elanguages'     => \$dh{DISABLE_ELANGUAGES},
		     'no-pre-post'       => \$dh{DISABLE_PREPOST},
		     'no-installdebconf' => \$dh{NO_INSTALLDEBCONF}
		   });
} elsif ( $class eq "aspell" ){
  init(options => { 'aspell-simple' => \$dh{ASPELL_SIMPLE} });
} else {
  init();
}

if ( $dh{NOSCRIPTS} ){
  $dh{DISABLE_PREPOST}++;
  $dh{NO_INSTALLDEBCONF}++;
};

delete $dh{WRITE_ELANGUAGES} if ( $dh{DISABLE_ELANGUAGES} );

foreach my $package (@{$dh{DOPACKAGES}}) {
  my $lib_dir       = tmpdir($package) . getlibdir($class);
  my $usr_lib_dir   = tmpdir($package) . "/usr/lib/$class";
  my $var_lib_dir   = tmpdir($package) . "/var/lib/$class";
  my $usr_share_dir = tmpdir($package) . "/usr/share/$class";
  my %options       = ('class'         => $class,
		       'program'       => $program,
		       'package'       => $package,
		       'lib_dir'       => $lib_dir,
		       'usr_lib_dir'   => $usr_lib_dir,
		       'var_lib_dir'   => $var_lib_dir,
		       'usr_share_dir' => $usr_share_dir
		      );

  # Obsolete "cdbs" snippets called installdeb-* scripts
  # unconditionally in case there was something for them.
  # Do not trigger an error if not found, just try next one.
  my $dicts = dico_process_infofile(\%options)
    or next;

  #
  # Will return undef unless  $dh{ASPELL_SIMPLE} is enabled
  my $dico_aspell_simple =  dico_process_aspell_simple(\%options);
  # 

  # Install debhelper and debhelper-like auto-scripts
  unless ( $dh{NOSCRIPTS} or $dh{DISABLE_PREPOST}) {

    # Install normal autoscripts. While called earlier than autocompat
    # autoscripts, will be placed later in maintainer scripts.
    autoscript ($package, "postinst", "postinst-$class",
		"s/#PACKAGE#/$package/");
    autoscript ($package, "postrm", "postrm-$class",
		"s/#PACKAGE#/$package/");

    if ( $class eq "ispell" || $class eq "aspell" ){
      my %hash_extension   = ("ispell" => "hash", "aspell" => "rws");
      my $autocompat_info  = dico_process_autocompat($dicts,\%options);

      #
      if ( $dh{ASPELL_SIMPLE}
	   && ! %{$autocompat_info->{'auto-compat'}}
	   && defined $dico_aspell_simple->{'lang'} ){
	$autocompat_info = $dico_aspell_simple;
	if ( defined $dico_aspell_simple->{'auto-contents'}
	     && %{$dico_aspell_simple->{'auto-contents'}} ){
	  # Install a contents file in $(DESTDIR) share dir
	  my $contents = join(" ",sort keys %{$dico_aspell_simple->{'auto-hash'}});
	  complex_doit("echo $contents | tr -s ' ' '\n' > $usr_share_dir/$dico_aspell_simple->{'lang'}.contents");
	}
      }
      #

      if ( %{$autocompat_info->{'auto-compat'}} ){
	verbose_print("$program info:");
	if ( defined  $autocompat_info->{'auto-contents'}
	     && %{$autocompat_info->{'auto-contents'}} ){
	  verbose_print(" auto-contents: \"",join(', ',sort keys %{$autocompat_info->{'auto-contents'}}),"\"");
	}
	verbose_print(" auto-compat: \"",join(', ',sort keys %{$autocompat_info->{'auto-compat'}}),"\"");
	verbose_print(" auto-hash: \"",  join(', ',sort keys %{$autocompat_info->{'auto-hash'}}), "\"");
      }

      # Install autocompat autoscripts
      if ( %{$autocompat_info->{'auto-compat'}} ){
	dico_install_autocompat($autocompat_info,\%options);
      }
    }

    # Populate substvars $class:Depends
    dico_populate_substvars(\%options);
  }
  ## -- No debconf handling needed for aspell or hunspell --
}


__END__

=head1 NAME

B<installdeb-aspell> - debhelper-like utility for
maintainers of aspell dictionary Debian packages

=head1 SYNOPSIS

 installdeb-aspell [debhelper options] 

=head1 DESCRIPTION

B<installdeb-aspell> is a
debhelper like program that is responsible for installing appropriate
debhelper snippets in
an aspell dictionary
package, according to the Debian Spell Dictionaries and Tools Policy.

For more details, see
 /usr/share/doc/dictionaries-common-dev/dsdt-policy.txt.gz

The actions executed by
B<installdeb-aspell>
are the following:

=over

=item * Maintainer Scripts

B<installdeb-aspell>
installs the necessary scraps of code in the
F<postinst> and F<postrm> scripts.

=item * Language info file

B<installdeb-aspell>
also checks a file containing
aspell dictionary
information, called
F<debian/info-aspell>
or
F<debian/package.info-aspell>.
If this file is successfully parsed, it is installed in the
F</var/lib/dictionaries-common/aspell> directory.

=item * Substvars substitutions

This script will populate a F<substvars> file, so Depends line in
control file can contain
${aspell:Depends} to
automatically care for dictionaries-common dependencies.

This requires C<Build-Depends> on at least dictionaries-common-dev
1.23.0 and is highly recommended.

=item * Debconf files

As opposed to B<installdeb-ispell> and B<installdeb-wordlist>,
neither B<installdeb-aspell> nor B<installdeb-hunspell> do anything
related to debconf files, since both aspell and hunspell rely on the
environment variable C<LANG> for default dictionary selection
instead of using symlinks set after a debconf question on
dictionary installation.
For that reason if you need to add debconf stuff with debhelper
to your aspell or hunspell dictionary package do it in the usual
way and call dh_installdebconf(1) as for any other package.


=item * Extra installdeb-aspell options in Language info file

For benefit of aspell dictionaries using B<aspell-autobuildhash>,
B<installdeb-aspell> script will look for C<'auto-compat'> and
C<'auto-contents'> fields in F<$lang.info-aspell>, containing base
name(s) of your compat and, if needed, contents file(s).
If C<'auto-compat'> entry is found two debhelper snippets are added,
one to create/reset compatfile(s) in postinst and the other to remove
files in a removal list (initially containing compat files) from
postrm.
If C<'auto-contents'> entry is found, contents file at
F<debian/> will be parsed and its contents added to the removal list,
otherwise a hash with the same basename of the compatfile is expected
and added to the removal list.
If you use a contents file and enable it in C<'auto-contents'>
you usually will need only the same base name
enabled in C<'auto-compat'>.

If you are interested in a simple install of a pristine aspell
dictionary with a minimal debian/rules please look at
L<dh_aspell-simple(1)>.
You will still need an aspell-info file, but compat/contents stuff
will be guessed if not explicitly provided in the aspell-info file.

F</usr/lib/aspell/$dict.rws -E<gt> /var/lib/aspell/$dict.rws>
symlinks are no longer created automatically by this script.
They are now created by new aspell-autobuildhash together with the
relevant hashes.
Make sure to depend on at least dictionaries-common 1.23
(See F<dictionaries-common.checklist>).



=back

=head1 OPTIONS

The usual dephelper(1) options are accepted. 

=head1 NOTES

This program is not part of debhelper, although it is intended to be used
in aspell dictionary packages using debhelper in its
building.

=head1 SEE ALSO

debhelper(1),
dictionaries-common.checklist,
dsdt-policy.txt

This program is part of the dictionaries-common-dev package.
It is intended to be used by maintainers of
aspell dictionary
packages for Debian. See the documentation
under /usr/share/doc/dictionaries-common-dev.

=head1 AUTHORS

Rafael Laboissiere, Agustin Martin

=cut

# Local Variables:
#  mode: flyspell-prog
#  mode: perl
#  ispell-local-dictionary: "american"
# End:

#  LocalWords:  aspell ispell wordlist debconf debhelper Debian config postrm
#  LocalWords:  debian elanguages installdeb dephelper installdebconf Elanguage
#  LocalWords:  Laboissiere
