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

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

use Debian::Debhelper::Dh_Lib;
init();

use Debian::DictionariesCommon q(:all);

my $dictdir = "/usr/share/dictionaries-common/debconf";
my $class = "ispell";

sub mydie {
  my $msg = shift;
  my $see = shift;
  die "$msg\nPlease see $see.\n";
}

foreach $package (@{$dh{DOPACKAGES}}) {

  # Process the debian/info-ispell file
  my $infofile = "";
  if (not ($infofile = pkgfile ($package, "info-$class"))) {
    mydie ("There is no debian/info-$class file for package $package.",
           "the dictionaries-common Policy");
  }
  # Parse the debian/info-ispell file
  my $dicts = parseinfo ($infofile);

  # If we get here, the parseinfo call was successful.  Install the
  # file in the dictionaries-common lib dir.
  my $lib_dir = tmpdir ($package) . getlibdir ($class);
  doit ("install", "-d", $lib_dir);
  doit ("install", "-m644", $infofile, "$lib_dir/$package");

  if (! $dh{NOSCRIPTS}) {
    autoscript ($package, "postinst", "postinst-$class",
      "s/#PACKAGE#/$package/");
    autoscript ($package, "postrm", "postrm-$class",
      "s/#PACKAGE#/$package/");
  }

 
 # -----------------------------------------------------------------------
 # Test if debian/[package].{config,templates} exist and trigger an error 
 # if it is the case.
 # -----------------------------------------------------------------------

  if (pkgfile ($package, "config")) {
    mydie ("debian/[package.]config file exists.  You should probably move
it to debian/[package.]config.in", "installdeb-$class(1)");
  }
  if (pkgfile ($package, "templates")) {
    mydie ("debian/[package.]templates file exists.  You should probably move
it to debian/[package.]templates.in", "installdeb-$class(1)");
  }

 # -----------------------------------------------------------------------
 # We are okay.  Generate the config file
 # -----------------------------------------------------------------------

  open (CONFIG, "> debian/$package.config");

  # Get the Policy compliant config code
  my $config = `cat $dictdir/config-$class`;

  # Check if there is a debian/[package.]config.in file
  if (my $infile = pkgfile ($package, "config.in")) {
    open (IN, "< $infile");
    # Check if it is a Bourne shell or Perl script
    (my $buff = <IN>) =~ m{^\#!\s*(/bin/sh|/usr/bin/perl)};
    if (not $1) {
      mydie ("The $infile file must be either a shell or Perl script.",
	     "installdeb-$class(1)");
    }
    $isshell = ($1 eq "/bin/sh");
    # Okay, look now for a the valid #DEBHELPER# string
    close IN;
    if (not (($buff = `cat $infile`) =~ /\n\#DEBHELPER\#\s*\n/)) {
      mydie ("No valid #DEBHELPER# string in $infile.",
	     "installdeb-$class(1)");
    }
    # The valid #DEBHELPER# string was found, so replace it by the
    # Policy specified code, taking care if it is a Bourne shell or
    # Perl script.
    if ($isshell) {
      $config = qq(tmp=`tempfile`
cat > \$tmp <<EOF
$config
EOF
perl \$tmp
rm -f \$tmp
);
      }
    $buff =~ s/\#DEBHELPER\#/$config/;
    # Save the whole thing in the config file
    print CONFIG $buff;

  }
  else {
    # Put the Policy compliant compliant code in creating a Perl script
    print CONFIG "#!/usr/bin/perl -w\n$config";
  }
  close CONFIG;

 # -----------------------------------------------------------------------
 # Generate the templates file
 # -----------------------------------------------------------------------
 
  my @tmp_languages = (); 
  my @tmp_defaults  = (); 
  foreach (keys %{$dicts}){
      my $lang = $dicts->{$_};
      push (@tmp_languages,$_) unless
	  (exists $lang->{'debconf-display'}
	   &&  lc($lang->{'debconf-display'}) eq "no");
      if (exists $lang->{'debconf-default'}
	  &&  lc($lang->{'debconf-default'}) eq "yes"){
	  push (@tmp_defaults,$_);
      }
  }
  my $languages   = join (', ', @tmp_languages);

  my $buff = `cat $dictdir/templates-$class`;
  $buff =~ s/\#LANGUAGES\#/$languages/;
  $buff =~ s/\#PACKAGE\#/$package/;

  {
      my $orig='';
      my $infile='';
      if ($infile = pkgfile ($package, "po-master.templates")) {
	  #$orig = `po2debconf $infile`;
	  $orig = `cat $infile`;
      } elsif ($infile = pkgfile ($package, "templates.in")) {
	  print STDERR " ** Warning: $infile use is deprecated.\n" . 
	      "      Please base your template localization in po-debconf\n";
	  $orig = `cat $infile`;
      }
      if ($orig){
	  $orig =~ s/^\s*//;
	  $buff .= "\n\n$orig";
      }
  }

 if ( scalar(@tmp_defaults) > 0 ){
     my $thedefaults = join (', ', @tmp_defaults);
     $buff .= "
Template: $package/defaults
Type: text
Default: $thedefaults
Description: 
";
 }

  open (TEMPLATES, "> debian/$package.templates");
  print TEMPLATES $buff;
  close TEMPLATES;

 # ---------------------------------------------------------------------
 # Call dh_installdebconf and do the clean up
 # ---------------------------------------------------------------------

  doit ("dh_installdebconf", "-p$package");
  doit ("rm", "-f", "debian/$package.config", "debian/$package.templates");

}


__END__

=head1 NAME

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

=head1 SYNOPSIS

 installdeb-ispell [debhelper options]

=head1 DESCRIPTION

B<installdeb-ispell> is a debhelper like program that is
responsible for installing appropriate debconf config and templates 
files, appropiate pspell pwli files (after generating them from 
the Language info file)  and debhelper snippets in 
an ispell dictionary package,
according to the Debian Spell Dictionaries and Tools Policy.

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

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

=over

=item Maintainer Scripts

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

=item Language info file

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

=item Debconf files

B<installdeb-ispell> installs the Policy compliant
Debconf files from the information contained in the
F<info-ispell> file.  These files are created as 
F<debian/config> (or F<debian/package.config>) and F<debian/templates>
(or F<debian/package.templates>).  No intervention is needed here,
since B<installdeb-ispell> will make a call to
dh_installdebconf(1).

If the package needs to have special code in the F<config> file, the maintainer
should supply files called F<debian/config.in> (or
F<debian/package.config.in>). In the F<config.in> file, the string
"#DEBHELPER#" must appear alone in one line and start at the first
column.  B<installdeb-ispell> will replace that
token with the necessary Policy compliant code (this works with
F<config.in> is either a Bourne shell or Perl script). 

If the package needs to define its own questions via the F<templates>
file the maintainer should either supply files F<debian/po-master.templates> 
(or F<debian/package.po-master.templates>) together with the appropiate 
po files if the package handles template localization through po-debconf, 
or F<debian/templates.in> (or F<debian/package.templates.in>) otherwise. 
See the po-debconf(7) manual page for more details and remember that the 
master templates name is now different. 
 
The templates defined in the F<templates.in> or F<po-master.templates> files 
are merged into the Policy compliant templates by 
installdeb-ispell and a call to dh_installdebconf(1) 
is internally done. 


=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 ispell dictionary packages using debhelper in its
building.

=head1 SEE ALSO

debhelper(1)

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

=head1 AUTHORS

Rafael Laboissiere

=cut
