home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / sbin / ispell-autobuildhash < prev    next >
Encoding:
Text File  |  2006-12-19  |  9.3 KB  |  291 lines

  1. #!/usr/bin/perl -w
  2. # $Id: ispell-autobuildhash,v 1.14 2004/07/05 11:48:20 agmartin Exp $
  3. #
  4. #  script for ispell hash autorebuild in Debian systems
  5. #
  6. # (c) 2003-2004 Agustin Martin Domingo <agmartin@debian.org>
  7. #
  8. #
  9. # This program is free software; you can redistribute it and/or modify
  10. # it under the terms of the GNU General Public License as published by
  11. # the Free Software Foundation; either version 2 of the License, or
  12. # (at your option) any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. # GNU General Public License for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License
  20. # along with this program; if not, write to the Free Software
  21. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22. #
  23.  
  24. sub usage {
  25.     print STDERR "\nUsage:\tispell-autobuildhash [--debug] [--force]\n"
  26.     . "\n"
  27.     . "Options:\n"
  28.     . "\t--debug         Show debugging information\n"
  29.     . "\t--force         Do the job regardless of versions comparisons\n";
  30. }
  31.  
  32. sub debugprint {
  33.     print STDERR "@_\n" if $debug;
  34. }
  35.  
  36. sub mymessage{
  37.     my $message  = join(" ",@_);
  38.     my $question = "dictionaries-common/ispell-autobuildhash-message";
  39.     my $hashfile = '';
  40.     
  41.     if ( $dict ) {
  42.     $hashfile = "$dict.hash";
  43.     } else {
  44.     $hashfile = "dictionaries-common";
  45.     }
  46.  
  47.     subst($question,"xxpell","ispell");
  48.     subst($question,"XXpell","Ispell");
  49.     subst($question,"hashfile",$hashfile);
  50.     subst($question,"errormsg",$message);
  51.     fset ($question,"seen","false");
  52.     title("dictionaries-common: Running ispell-autobuildhash");
  53.     input("critical",$question);
  54.     go ();
  55. }
  56.  
  57. sub old_myerror {
  58.     print "
  59. ** Error: @_
  60.  
  61. This error was caused by package providing hash $dict, although it
  62. can be made evident during other package postinst. Please complain 
  63. to the maintainer of package providing hash $dict. In the meantime
  64. you might need to remove package providing $dict.hash\n";
  65.     myclean();
  66.     exit 1;
  67. }
  68.  
  69. sub myerror {
  70.     mymessage @_;
  71.     $build_status = "error";
  72. #    myclean();
  73. #    exit 1;
  74. }
  75.  
  76. sub myclean {
  77.     unlink <$tempfile*>;
  78.     rmdir $tmpdir;
  79. }
  80.  
  81.  
  82. # ---------------------------------------------------------------------
  83. #      What to do if a signal is trapped
  84. # ---------------------------------------------------------------------
  85.  
  86. sub trap {
  87.     myclean();
  88.     exit 1
  89. }
  90.  
  91. # ---------------------------------------------------------------------
  92. #      Handle autorebuilding
  93. # ---------------------------------------------------------------------
  94.  
  95. sub autorebuild {
  96.     my $dict = shift ||                          # The dictionary name
  97.     myerror "No argument passed to function autorebuild";
  98.     my $zmwl = "/usr/share/ispell/$dict.mwl.gz"; # the compressed munched wordlist  
  99.     my $aff  = "/usr/lib/ispell/$dict.aff";      # the aff file
  100.     my $hash = "/var/lib/ispell/$dict.hash";     # the hash file
  101.     my $desc = "/usr/share/ispell/$dict.desc";   # the .desc file
  102.  
  103.     print STDERR "Processing dict: $dict\n";
  104.  
  105.     if ( -e $aff ){
  106.     if ( -e $zmwl ){
  107.         system ("gzip -dc $zmwl > $tempfile") == 0
  108.         or myerror "Could not gunzip the munched wordlist for $dict";
  109.         system ("buildhash -s $tempfile $aff $hash") == 0
  110.         or myerror "Could not build the hash file for $dict" ;
  111.  
  112.     } elsif ( -e $desc ) {
  113.         if ( -x "/usr/sbin/update-ispell-hash" ) {
  114.         system ("update-ispell-hash -d $dict > /dev/null") == 0
  115.             or myerror "Problems running update-ispell-hash for $dict";
  116.         debugprint "Found and run update-ispell-hash";
  117.         } elsif ( -x "/usr/sbin/update-ipolish-hash" ) {
  118.         system ("update-ipolish-hash -d $dict > /dev/null") == 0
  119.             or myerror "Problems running update-ipolish-hash for $dict";
  120.         debugprint "Found and run update-ipolish-hash";
  121.         } else {
  122.         myerror "Could not find any of update-ispell-hash, update-ipolish-hash"
  123.         }
  124.     } else {
  125.         myerror "Could not find $zmwl";
  126.     }
  127.     } else {
  128.     myerror "Could not find affix file $aff";
  129.     }
  130. }
  131.  
  132. # ---------------------------------------------------------------------
  133. #                   Get ispell compat version
  134. # ---------------------------------------------------------------------
  135.  
  136. sub get_ispell_compat {
  137.     my @internal;
  138.     my $ispell_compat;
  139.     if ( -e $ispellcompatfile ){
  140.     open (COMPAT,"$ispellcompatfile");
  141.     chomp ( $ispell_compat = <COMPAT> );
  142.     close COMPAT;
  143.     } elsif ( -e "/usr/bin/ispell" ){
  144. #    $ispell_compat = `ispell -v | head -n 1 | sed 's/.*Version \([^\ ]*\).*/\1/g'`
  145.     $ispell_compat = `ispell -v | head -n 1`;
  146.     @internal = split(" ",$ispell_compat);
  147.     $ispell_compat = $internal[4];
  148.     } else {
  149.     myerror "ispell is not installed";
  150.     }
  151.     return $ispell_compat;
  152. }
  153.  
  154. # ---------------------------------------------------------------------
  155. #                        The main program
  156. # ---------------------------------------------------------------------
  157.  
  158. die "$0: You must run this as root.\n" if ($> != 0);
  159.  
  160. use Debconf::Client::ConfModule q(:all);
  161. use Getopt::Long;
  162.  
  163. $SIG{INT}  = \&trap;
  164. $SIG{KILL} = \&trap;
  165. $SIG{TERM} = \&trap;
  166.  
  167. $tmpdir           = "/tmp/ispell-auto-$$";
  168. $tempfile         = "$tmpdir/ispellhash"; 
  169. $compatdir        = "/var/lib/ispell";
  170. $ispellcompatfile = "/usr/share/ispell/ispell.compat";
  171.  
  172. $force        = '';
  173. $debug        = '';
  174. $build_status = '';
  175.  
  176. GetOptions ('debug' => \$debug, 
  177.         'force' => \$force) or usage();
  178.  
  179. mkdir($tmpdir,0700)        || die "can't mkdir $tmpdir: $!";
  180.  
  181. if ( -x "/usr/bin/ispell" ){
  182.     $ispell_compat = get_ispell_compat();
  183.     foreach $dict ( <$compatdir/*.compat> ){
  184.     $build_status   = '';
  185.     my $dict_compat = '';
  186.     $dict =~ s/\.compat$//;
  187.     $dict =~ s/.*\///;
  188.     if ( $force ) {
  189.         $dict_compat = "forced";
  190.         autorebuild $dict;
  191.     } else {
  192.         open (COMPAT,"$compatdir/$dict.compat");
  193.         $dict_compat = <COMPAT>;
  194.         close COMPAT;
  195.         $dict_compat = 0 if not $dict_compat;
  196.         chomp $dict_compat;
  197.         autorebuild $dict if ($ispell_compat ne $dict_compat);
  198.     }
  199.     debugprint "$dict => ispell_compat: [$ispell_compat]; dict_compat: [$dict_compat]";
  200.     if ( not $build_status ){
  201.         debugprint " +++ Updating $compatdir/$dict.compat";
  202.         open (COMPAT,">","$compatdir/$dict.compat");
  203.         print COMPAT "$ispell_compat\n";
  204.         close COMPAT;
  205.     } else {
  206.         debugprint " --- $compatdir/$dict.compat not updated because of an error";
  207.     }
  208.     
  209.     unlink <$tempfile*>;
  210.     }
  211. } else {
  212.     debugprint " ispell is not installed. Doing nothing";
  213. }
  214.  
  215. myclean ();
  216.  
  217. __END__
  218.  
  219. =head1 NAME
  220.  
  221. B<ispell-autobuildhash> - Autobuilding the ispell hash file for some dicts
  222.  
  223. =head1 SYNOPSIS
  224.  
  225.  ispell-autobuildhash [--force]
  226.  
  227.    Options:
  228.     --debug      Show some extra information.
  229.     --force      Rebuild the hash file for all dicts providing a 
  230.                  compat file skipping the test.
  231.  
  232. =head1 DESCRIPTION
  233.  
  234. B<ispell-autobuildhash> is a script that will manage ispell hash files 
  235. autobuild, intended to be called from the dictionaries-common tools. 
  236. Depending on the ispell 
  237. compatibility level and on the compatibility level used for the hash file 
  238. if present, will decide whether it must be rebuilt or not. This script will 
  239. only work on ispell packages prepared to use it, it will do nothing for other 
  240. ispell dict packages.
  241.  
  242. =head1 OPTIONS
  243.  
  244. --debug      Show some extra information.
  245. --force      Rebuild the hash file for all dicts providing a compat 
  246.              file regardless of the compatibility levels found.
  247.  
  248. =head1 PACKAGE MAINTAINERS
  249.  
  250. To use this system, just provide a F<$dict.compat> file in F</var/lib/ispell> 
  251. (I<$dict> stands or the hash or affix file basename). Put a "0" in it or just 
  252. create an empty one with touch. The way things are done depends on whether 
  253. your dict has a single source or a multiple source like ipolish has.
  254.  
  255. The most common case is having a single source. In this case you must put 
  256. the F<$dict.aff> file at F</usr/lib/$dict.aff> as usual and the munched 
  257. wordlist compressed with gzip at F</usr/share/ispell/$dict.mwl.gz>.
  258.  
  259. This script supports the way ipolish allows selection of subdictionaries 
  260. to be put in the hash file. It will detect the presence of  the main 
  261. language .desc file and call update-ipolish-hash as appropriate.
  262.  
  263. Although this script looks for the existence of a update-ispell-hash script 
  264. with supposedly similar functionality to update-ipolish-hash, this latter 
  265. does not really exists, although it might be added at some time to ispell or 
  266. to the dictionaries-common system, if enough demand is found.
  267.  
  268. If your package provides more than one ispell dictionary you will need to 
  269. do the steps above for each dictionary.
  270.  
  271. Dictionaries-common scripts will call internally this script and create a 
  272. hash file at F</var/lib/ispell/$dict.hash>. You must set a symlink to that 
  273. file from F</usr/lib/ispell/$dict.hash>. You are also suggested to create 
  274. an empty file at F</var/lib/ispell/$dict.hash> in the install target of 
  275. your package build process. This empty file will be overwritten when the 
  276. real hash is created, but will make the hash be removed at package 
  277. removal without any magic being done in the postrm and will also help to
  278. keep track about which package owns that file.
  279.  
  280. B<ispell> maintainer should also call this script from package postinst. 
  281. When comparing versions it will get the ispell version from file
  282. F</usr/share/ispell/ispell.compat>, or if it does not exists, from the 
  283. upstream version as given in the first line of 'B<ispell -vv> output.
  284.  
  285. =head1 AUTHORS
  286.  
  287. Agustin Martin <agmartin@debian.org>
  288.  
  289. =cut
  290.  
  291.