#!/usr/bin/perl -w
#
# ooo dict/thesauri/hyphenation file installation with optional ooo2
# compatible symlinks and registration with Openoffice.org dictionary list.

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

use Debian::Debhelper::Dh_Lib;

my $srcdir      = '';
my $o2compat    = '';
my $dico_debug  = '';
my $do_altlinks = 1;

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

sub mywarn {
  my $msg = shift;
  my $see = shift;
  warn "$msg\nPlease see $see.\n";
  exit 0;
}

sub dico_parse_options {
  my @options = ();
  foreach my $opt ( @_ ){
    if ( $opt eq "--dico-debug" ){
      $dico_debug = 1;
    } elsif ( $opt =~ s/^--srcdir=// ){
      $srcdir = $opt;
    } elsif ( $opt eq "--dico-alt-links" ){
      $do_altlinks = 1;
    } elsif ( $opt eq "--dico-no-alt-links" ){
      $do_altlinks = '';
    } elsif ( $opt eq "--o2compat" ){
      $o2compat    = 1;
    } elsif ( $opt eq "--no-o2compat" ){
      $o2compat    = '';
    } else {
      push @options, $opt;
    }
  }
  return @options;
}

# Hijack installdeb-myspell specific options from the
# command line, before debhelper's parseopt can see them.

@ARGV = dico_parse_options @ARGV;

# And now initialize debhelper

init();

# and do the real work

my $old_destdir;
my $class          = "myspell";
my $oooinfodir     = "/usr/share/myspell/infos/ooo";
my $old_installdir = "/usr/share/myspell/dicts";
my %installdir     = ( "hunspell"    => "/usr/share/hunspell",
		       "hyphenation" => "/usr/share/hyphen",
		       "thesauri"    => "/usr/share/mythes");


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

  my %datahash    = ();

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

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

  if ( $o2compat ) {
    # Install the info file in the openoffice2 info dir if requested.
    my $old_infodir = tmpdir ($package) . $oooinfodir;
    doit ("install", "-d", $old_infodir);
    doit ("install", "-m644", $infofile, "$old_infodir/$package");

    # Make sure the old myspell dict/hyphen/thesauri dir exists if requested
    $old_destdir = tmpdir ($package) . $old_installdir;
    doit ("install", "-d", $old_destdir);
  }

  # Parse info file for dict, hyphen, thesauri, lang and country data..
  open INFOFILE, $infofile;
  while (<INFOFILE>){
    next if m/^\s*\#/;
    chomp;
    s/\#.*$//;
    print STDERR $_ . "\n" if $dico_debug;
    my ( $type, $lang, $country, $file) = split(' ',$_);
    $datahash{$type}{$file}{"$lang\_$country"}++;
  }
  close INFOFILE;

  # If --srcdir is passed, install .aff and .dic in dicts dir as
  # well as mozilla symlinks if it contains an underscore.
  if ( $srcdir && defined $datahash{'DICT'} ){
    foreach my $dictionary ( keys %{$datahash{'DICT'}} ) {
      my $dictdir = $installdir{"hunspell"};
      my $destdir = tmpdir ($package) . $dictdir;
      my @alternatives = keys %{$datahash{'DICT'}{$dictionary}};
      print STDERR "Installing DICT $dictionary files in $destdir\n";
      print STDERR " Alternatives: " . join(', ',@alternatives) . "\n"
	if $dico_debug;
      doit ("install", "-d", $destdir);
      $dictdir =~ s/.*\///;
      for my $ext ("aff", "dic") {
	my $basefrom = my $basedest = "$dictionary.$ext";
	my $srcfile  = "$srcdir/$basefrom";

	die ("There is no $srcfile file here\n")
	  unless -f "$srcfile";

	# Install base dict in destination directory.
	$basedest =~ tr/-/_/;
	if ( $basefrom ne $basedest ){
	  print STDERR " Warning: $basefrom renamed to $basedest\n"
	}
	doit ("install", "-m644", "$srcfile", "$destdir/$basedest");

	# Install symlink to base dict in old destdir if in o2compat mode.
	# If name has been changed (tr/-/_/) it is in Mozilla form in
	# dictionaries.lst, so use original name for link origin.
	if ( $o2compat ){
	  my $lbasefrom = ( $basedest eq $basefrom ) ? $basedest : $basefrom;
	  my $ltarget  = "../../$dictdir/$basedest";
	  print STDERR " Installing symlink olddir/$lbasefrom to $ltarget\n"
	    if $dico_debug;
	  doit ("ln", "-fs", "$ltarget", "$old_destdir/$lbasefrom");
	}

	# Install Mozilla symlinks in old destdir if in o2compat mode.
	if ( $o2compat && $basedest =~ /_/ ) {
	  my $mozlink =  $basedest;
	  $mozlink    =~ tr/_/-/;
	  my $ltarget = "../../$dictdir/$basedest";
	  print STDERR " Installing Mozilla alternative symlink olddir/$mozlink to $ltarget\n"
	    if $dico_debug;
	  doit ("ln", "-fs", "$ltarget", "$old_destdir/$mozlink");
	}

	# Install normal and Mozilla symlinks for alternative names if needed.
	if ( $do_altlinks ){
	  foreach my $altlink ( @alternatives) {
	    my $newlink = "$altlink.$ext";
	    unless ( $newlink eq $basedest ){
	      print STDERR " Installing alternative symlink $newlink to $basedest\n"
		if $dico_debug;
	      doit ("ln", "-fs", $basedest, "$destdir/$newlink");
	      if ( $o2compat && $newlink =~ /_/ ){
		my $ltarget = "../../$dictdir/$basedest";
		my $mozlink =  $newlink;
		$mozlink    =~ tr/_/-/;
		print STDERR " Installing Mozilla alternative symlink olddir/$mozlink to $ltarget\n"
		  if $dico_debug;
		doit ("ln", "-fs", "$ltarget", "$old_destdir/$mozlink");
	      }
	    }
	  }
	}
      }
    }
  }

  # If --srcdir is passed, install Openoffice hyphenation files
  # after data in info file
  if ( $srcdir && defined $datahash{'HYPH'} ){
    foreach my $hyphenation ( keys %{$datahash{'HYPH'}} ) {
      my $hyphdir  = $installdir{"hyphenation"};
      my $destdir  = tmpdir ($package) . $hyphdir;
      print STDERR "Installing HYPH $hyphenation files in $destdir\n";
      my $basefile = "$hyphenation.dic";
      my $srcfile  = "$srcdir/$basefile";
      $hyphdir =~ s/.*\///;
      my $ltarget  = "../../$hyphdir/$basefile";
      die ("There is no $srcfile file here\n")
	if not -f "$srcfile";
      doit ("install", "-d", $destdir);
      doit ("install", "-m644", "$srcfile", $destdir);
      doit ("ln", "-fs", "$ltarget", "$old_destdir/$basefile")
	if $o2compat;
    }
  }

  # If --srcdir is passed, install Openoffice thesaurus files
  # after data in info file
  if ( $srcdir && defined $datahash{'THES'} ){
    foreach my $thesaurus ( keys %{$datahash{'THES'}} ) {
      my $thesdir = $installdir{"thesauri"};
      my $destdir = tmpdir ($package) . $thesdir;
      print STDERR "Installing THES $thesaurus files in $destdir\n";
      $thesdir =~ s/.*\///;
      doit ("install", "-d", $destdir);
      foreach $ext ("dat","idx"){
	my $basefile = "$thesaurus.$ext";
	my $srcfile  = "$srcdir/$basefile";
	my $ltarget  = "../../$thesdir/$basefile";
	die ("There is no $srcfile file here\n")
	  if not -f "$srcfile";
	doit ("install", "-m644", "$srcfile", $destdir);
	doit ("ln", "-fs", "$ltarget", "$old_destdir/$basefile")
	  if $o2compat;
      }
    }
  }
}

__END__

=head1 NAME

B<installdeb-myspell> - debhelper-like helper for Debian packages
containing myspell/hunspell dictionaries or Openoffice.org
thesauri/hyphenation files.

=head1 SYNOPSIS

 installdeb-myspell [--srcdir=dir] [--dico-debug] [--{no-}o2compat] [debhelper options]

=head1 DESCRIPTION

B<installdeb-myspell> is a debhelper like program to help
installing myspell/hunspell dicts as well as Openoffice.org
thesauri or hyphenation files, after contents of an
F<info-myspell> file whose format is that for old
Openoffice.org 2. This needs the B<--srcdir> option enabled.

If in o2 compatibility mode, it will also install appropriate
debhelper snippets, info file and compatibility symlinks
in a way a package containing myspell/hunspell dictionaries
or Openoffice.org thesauri or hyphenation files can be used
in Debian lenny and former under Openoffice.org 2 and Mozilla*
according to the dictionaries policy valid at that time.

Note that unless B<--srcdir> option is used or o2compat mode
is enabled, this program will do nothing.

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

The actions executed by B<installdeb-myspell> are the
following (only some of them are done if not in o2
compatibility mode):

=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-myspell> will look for a file named F<debian/info-myspell>
or F<debian/package.info-myspell>. That file contains myspell/hunspell
dictionary, OOO thesauri or hyphenation information with lines like

TYPE LANG COUNTRY NAME

If this file is successfully parsed and the o2compat option is enabled,
it is installed in the F<[tmpdir]/usr/share/myspell/infos/ooo> directory.

A typical F<info-myspell> file for a myspell/hunspell dictionary will
contain something like

 # Spanish variants
 DICT es ES es_ES
 DICT es AR es_ES
 ...

while will, for a typical hyphenation file, be something like

 # Danish hyphenation
 HYPH da DK hyph_da_DK

or for a sample thesaurus,

 THES en US th_en_US

all with no leading whitespace. Commented lines are allowed.

=item Dictionary, thesaurus and hyphenation files installation

If the B<--srcdir=dir> option is set B<installdeb-myspell> will look for
the F<.aff/.dic> files in the directory specified by dir and install them
in the default target directory (F<[tmpdir]/usr/share/hunspell>).
Base name will be extracted from the F<info-myspell> file (last string in
the line). If target dict uses the ancient Mozilla hyphen form, it will
be renamed to the lowbar form on installation.
Same (but the renaming) for hyphenation and thesaurus files, to be installed in
F<[tmpdir]/usr/share/hyphen> and F<[tmpdir]/usr/share/mythes>.
If o2compat option is enabled, I<lenny> compatibility symlinks will be
installed as well.

=item Dictionaries alternative symlinks creation

B<installdeb-myspell> will, according to info extracted from the F<info-myspell>,
automatically set B<lang_COUNTRY> symlinks to the dict files installed by
B<installdeb-myspell>. This will only be done when the B<--srcdir> option is
used.

If B<--srcdir> option is used and the F<info-myspell> file contains something
like

 # Spanish variants
 DICT es ES es
 DICT es AR es
 ...

B<installdeb-myspell> will automatically set B<es_ES> and B<es_AR> symlinks

=item Mozilla spellchecker compatibility

For myspell/hunspell dictionaries, Mozilla had a B<lang{,-COUNTRY}> to names
translation table using hyphens as separators (and for some languages not using
country part at all) instead of lowbars.

Shortly, that will no longer be the case. Mozilla will also accept
B<lang_COUNTRY> format for that translation table, no special things
will be needed. In particular, do not duplicate entries in both lowbar
and hyphen forms. Use lowbar.

=item Debconf files

As opposed to B<installdeb-ispell> and B<installdeb-wordlist>, B<installdeb-myspell>
does nothing related to debconf files, not needed for myspell/hunspell dicts and
OOO thesauri and hyphenation files. If you need to add debconf stuff with debhelper
proceed in the usual way and call dh_installdebconf(1) as for any other package.

=back

=head1 OPTIONS

--dico-debug  Show some B<installdeb-myspell> specific debugging info.
              Does not enable debhelper debug, you need to enable
              it separately.

--srcdir=dir  Will look for F<.aff/.dic> files in the specified directory
              for myspell/hunspell dict packages, for F<.dic> files in
              Openoffice.org hyphenation packages and for F<.dat/.idx>
              files in Openoffice.org thesaurus packages, installing
              them if present in the default target directory. Base
              name will be extracted from the info-myspell file. If
              this option is specified and files are not present an
              error will appear.

--{no-}o2compat Disable/enable openoffice2 compatibility. If enabled,
              info file will be installed in
              F<[tmpdir]/usr/share/myspell/infos/ooo> and compatibility
              symlinks will be set from the old
              F<[tmpdir]/usr/share/myspell/dicts location>. This option
              is disabled by default.

--dico-{no-}alt-links {Process/Do not process} alternative symlinks
              according to info found in the myspell info file.

The usual debhelper(7) options are accepted.

=head1 NOTES

This program is not part of debhelper, although depends on and is
intended to work together with it.

=head1 SEE ALSO

debhelper(7)

This program is part of the dictionaries-common-dev package.  It is
intended for use by maintainers of packages containing myspell/hunspell
dictionaries or Openoffice.org thesauri/hyphenation files.
See the documentation under /usr/share/doc/dictionaries-common-dev.

=head1 AUTHORS

Rafael Laboissiere <rafael@debian.org>,
Agustin Martin <agmartin@debian.org>

=cut

#  LocalWords:  debhelper Debian myspell Openoffice
