#!/usr/bin/perl -w
#
# Libreoffice/ooo dict/thesauri/hyphenation file installation.

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

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

my $srcdir;
my $do_bdic;
my $no_dic;
my $no_hyphen;
my $no_thesaurus;
my $bdic_only;
my $dc_keep;
my $do_altlinks = 1;

# Initialize debhelper with extra installdeb-myspell specific options

init(options => { 'dico-debug'        => \$dh{VERBOSE},
		  'dico-alt-links'    => \$do_altlinks,
		  'dico-no-alt-links' => sub { $do_altlinks = undef },
		  'o2compat'          => sub { warning("Ignoring obsolete \"--o2compat\" option. o2compat support has been removed.") },
		  'no-o2compat'       => sub { warning("Ignoring obsolete \"--no-o2compat\" option. o2compat support has been removed.") },
		  'bdic'              => \$do_bdic,
		  'bdic-only'         => \$bdic_only,
		  'no-dic'            => \$no_dic,
		  'no-hyphen'         => \$no_hyphen,
		  'no-thesaurus'      => \$no_thesaurus,
		  'srcdir=s'          => \$srcdir,
		  'dc-keep'           => \$dc_keep
		});

# and do the real work

my $old_destdir;
my $class          = "myspell";
my %installdir     = ( "hunspell"      => "/usr/share/hunspell",
		       "hunspell-bdic" => "/usr/share/hunspell-bdic",
		       "hyphenation"   => "/usr/share/hyphen",
		       "thesauri"      => "/usr/share/mythes");
my @bdic_convert_tools  = ( "/usr/bin/convert-bdic",
			    "/usr/lib/qt6/libexec/qwebengine_convert_dict",
			    "/usr/lib/qt5/bin/qwebengine_convert_dict");
my $bdic_convert_tool;

if ( $bdic_only ){
  $do_bdic++;
  $no_dic++;
  $no_hyphen++;
  $no_thesaurus++;
}

# Check for available bdic creation tool
if ( $do_bdic ){
  foreach my $tool (@bdic_convert_tools ){
    if ( -x $tool ){
      $bdic_convert_tool = $tool;
      verbose_print("bdic_convert_tool: $bdic_convert_tool");
      last;
    }
  }

  die "$0: No bdic creation tool found. Forgot Build-Dep on qt6-webengine-dev-tools or a supersedind package?  Aborting ...\n"
      unless $bdic_convert_tool;
}

# Process different entries
foreach my $package (@{$dh{DOPACKAGES}}) {

  my %datahash    = ();

  # Process the debian/info-myspell file. This is not the same kind of
  # info file as info-{wordlist,ispell,aspell,hunspell}.
  # dico_process_infofile cannot be used here.
  my $infofile = "";
  unless( $infofile = pkgfile ($package, "info-$class")) {
    dico_warning ("There is no debian/info-$class file for package $package.",
		  "the dictionaries-common Policy");
    exit;
  }

  # Parse info file for dict, hyphen, thesauri, lang and country data..
  my $INFOFILE;
  open $INFOFILE, $infofile;
  while (<$INFOFILE>){
    chomp;
    next if m/^\s*$/;        # Skip empty lines
    next if m/^\s*\#/;       # Skip comment lines
    s/\#.*$//;               # Remove comments after dict info
    verbose_print("info line: $_");
    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'}} ) {
      last if $no_dic;
      my $dictdir = $installdir{"hunspell"};
      my $destdir = tmpdir ($package) . $dictdir;
      my @alternatives = keys %{$datahash{'DICT'}{$dictionary}};

      print STDERR "Installing DICT $dictionary files in $destdir\n";
      verbose_print(" Alternatives: " . join(', ',@alternatives));
      doit ("install", "-d", $destdir);
      $dictdir =~ s/.*\///;
      for my $ext ("aff", "dic") {
	my $basefrom = my $basedest = "$dictionary.$ext";
	my $srcfile  = "$srcdir/$basefrom";

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

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

	# 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 ){
	      verbose_print("Installing alternative symlink $newlink to $basedest");
	      doit ("ln", "-fs", $basedest, "$destdir/$newlink");
	    }
	  }
	}
      }
    }
  }

  # If --srcdir is passed, install Openoffice hyphenation files
  # after data in info file
  if ( $srcdir && defined $datahash{'HYPH'} ){
    foreach my $hyphenation ( keys %{$datahash{'HYPH'}} ) {
      last if $no_hyphen;
      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";
      error("There is no $srcfile file here\n")
	unless ( -f "$srcfile" );
      doit ("install", "-d", $destdir);
      doit ("install", "-m644", "$srcfile", $destdir);
    }
  }

  # If --srcdir is passed, install Openoffice thesaurus files
  # after data in info file
  if ( $srcdir && defined $datahash{'THES'} ){
    foreach my $thesaurus ( keys %{$datahash{'THES'}} ) {
      last if $no_thesaurus;
      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 my $ext ("dat","idx"){
	my $basefile = "$thesaurus.$ext";
	my $srcfile  = "$srcdir/$basefile";
	my $ltarget  = "../../$thesdir/$basefile";
	error("There is no $srcfile file here\n")
	  unless ( -f "$srcfile" );
	doit ("install", "-m644", "$srcfile", $destdir);
      }
    }
  }

  # Create and install .bdic files
  if ( $srcdir && defined $datahash{'DICT'} ){
    my $dictdir = $installdir{"hunspell-bdic"};
    my $destdir = tmpdir ($package) . $dictdir;

    foreach my $dictionary ( keys %{$datahash{'DICT'}} ) {
      last unless $do_bdic;
      my @alternatives = keys %{$datahash{'DICT'}{$dictionary}};

      print STDERR "Creating and installing BDIC $dictionary files in $destdir\n";
      verbose_print(" Alternatives: " . join(', ',@alternatives));
      doit ("install", "-d", $destdir);
      $dictdir =~ s/.*\///;

      my $srcfile    = "$srcdir/$dictionary.dic";
      my $targetfile = "$srcdir/$dictionary.bdic";
      doit ("$bdic_convert_tool","$srcfile",$targetfile);
      doit ("install", "-m644", "$targetfile", $destdir);
      unlink $targetfile unless $dc_keep;

      # Install normal and Mozilla symlinks for alternative names if needed.
      if ( $do_altlinks ){
	foreach my $altlink ( @alternatives) {
	  my $newlink  = "$altlink.bdic";
	  my $origfile = "$dictionary.bdic";
	  unless ( $newlink eq $origfile ){
	    verbose_print("Installing alternative symlink $newlink to $origfile");
	    doit ("ln", "-fs", "$origfile", "$destdir/$newlink");
	  }
	}
      }
    }
  }
}

__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] [options] [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 program may also create and install dictionaries in
Chromium hunspell binary dic format (F<.bdic>) if B<--bdic> option
is set.
All this needs the B<--srcdir> option enabled.

Note that unless B<--srcdir> option this program will do nothing.

For more details, see F</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.
No snippets are currently installed.

=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

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 B<--bdic> option is set, dicts in Chromium hunspell binary
dictionary format will also be created and installed.
This requires /usr/lib/qt5/bin/qwebengine_convert_dict
(from qt6-webengine-dev-tools package) be available.

If B<--bdic-only> option is set, only dicts in Chromium hunspell
binary dictionary format
(F<.bdic>)
will be handled and dic, hyphen
and thesaurus disabled
(even if they have entries in F<info-myspell> file).
Equivalent to B<--bdic --no-dic --no-hyphen --no-thesaurus>.

B<--no-{dic,hyphen,thesaurus}> options are provided to disable
handling of those files even if they have entries in
F<info-myspell> file.
Mostly useful for testing.

If B<--dc-keep> is set, temporary files will be preserved once
intallation is complete.
Currently this only affects .bdic file.

B<--bdic>, B<--bdic-only> and B<--no-{dic,hyphen,thesaurus}>
options require at least dictionaries-common-dev 1.29.0.
B<--dc-clean> requires at least dictionaries-common-dev 1.29.3.

=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.

If F<.bdic> is enabled, associated symlinks will be set in the same way.

=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.

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

--bdic        Enable bdic handling.

--bdic-only   Only enable .bdic handling, disable
              {dic,hyphen,thesaurus} installation.

--no-{dic,hyphen,thesaurus} Disable {dic,hyphen,thesaurus} handling.

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

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

#  LocalWords:  debhelper Debian myspell Openoffice
