#!/usr/bin/perl -w
#
# Registration with Openoffice.org dictionary list

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

use Debian::Debhelper::Dh_Lib;

my $srcdir_flag = "srcdir";

# Hijacks the --${srcdir_flag} option from the command line, before 
# debhelper's parseopt can see it. 
my $srcdir = (reverse (grep {/^--$srcdir_flag=/} @ARGV))[0];
if (defined $srcdir) {
    $srcdir =~ s/--$srcdir_flag=//;
    @ARGV = grep {!/^--$srcdir_flag=/} @ARGV;
}

init();

my $class = "myspell";
my $oooinfodir="/usr/share/myspell/infos/ooo";
my $dictdir = "/usr/share/myspell/dicts";

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

foreach $package (@{$dh{DOPACKAGES}}) {
    if (! $dh{NOSCRIPTS}) {
	autoscript ($package, "postinst", "postinst-$class",
		    "s/#PACKAGE#/$package/");
	autoscript ($package, "postrm", "postrm-$class",
		    "s/#PACKAGE#/$package/");
    }
    
    # Process the debian/info-aspell 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");
    }
    
    # If we get here, the parseinfo call was successful.  Install the
    # file in the dictionaries-common lib dir.
    my $lib_dir = tmpdir ($package) . $oooinfodir;
    doit ("install", "-d", $lib_dir);
    doit ("install", "-m644", $infofile, "$lib_dir/$package");  
    
    # Get language name from info file
    my $language = (reverse (split (/\s+/, `cat $infofile`)))[0];
    
    # Install .aff and .dic in dicts dir, as well all the symlinks if 
    # the language name contains an underscore
    $lib_dir = tmpdir ($package) . $dictdir;
    doit ("install", "-d", $lib_dir);
    
    for my $ext ("aff", "dic") {
	my $file = "$language.$ext";
	mydie ("There is no $file file here")
            if not -f $file;
	doit ("install", "-m644", "$srcdir/$file", $lib_dir)
	    if defined $srcdir;
	if ($file =~ /_/) {
	    my $link = $file;
	    $link =~ tr/_/-/;
	    doit ("ln", "-fs", $file, "$lib_dir/$link");
	}
    }

}

__END__

=head1 NAME

B<installdeb-myspell> - debhelper-like utility for
maintainers of Openoffice.org myspell dictionary Debian packages

=head1 SYNOPSIS

 installdeb-myspell [debhelper options]

=head1 DESCRIPTION

B<installdeb-myspell> is a debhelper like program that is
responsible for installing appropriate debhelper snippets in 
an myspell dictionary package for use under Openoffice.org,
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-myspell> are the
following:

=over

=item Maintainer Scripts

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

=item Language info file

B<installdeb-aspell> also a file containing
myspell dictionary information, called
F<debian/info-myspell> or
F<debian/package.info-myspell>.  If this file is
successfully parsed, it is installed in the
F<[tmpdir]/usr/share/myspell/infos/ooo> directory.

=item Debconf files
 

As opposed to B<installdeb-ispell> and B<installdeb-wordlist>, B<installdeb-myspell> 
does nothing related to debconf files, that are not needed for myspell. For that 
reason if you need to add debconf stuff with debhelper 
to your myspell dictionary package do it in the usual way and call 
dh_installdebconf(1) as for any other package.

=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 Openoffice.org myspell 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 Openoffice.org myspell dictionaries.  
See the documentation under /usr/share/doc/dictionaries-common-dev.

=head1 AUTHORS

Rafael Laboissiere

=cut
