#!/usr/bin/env perl use POSIX; use File::Basename; sub getpattendir () { # Extract pattern directory from Makefile, in the same directory as # this script. A complete interpretation of the Makefiles would be # too complicated, so only ${prefix} is read, which is (hopefully) # defined as constant. my $mf = (dirname $0) . "/Makefile", $prefix = ""; open MF, $mf or die "Cannot open $mf for reading: $!"; while () { if (/^\s*prefix\s*=\s*(.*)\s*$/) { $prefix = $1; } } close MF; if ($prefix eq "") { die "prefix not defined in $mf"; } return "$prefix/lib/dillo/hyphenation"; } sub printhelp { print " Syntax: install-hyphenation [ ...]\n"; print "\n"; print " If there are multiple pattern files for a language, and so the file name\n"; print " is not \"hyph-.tex\", you can specify the origin by using a colon:\n"; print " \":\", eg. \"de-1996:de\". If both are identical, the\n"; print " colon can be committed; \"ru\" is equivalent to \"ru:ru\".\n"; } sub trpatfile { # Extract the data needed by dillo from the TeX file, based on two # rules: # # 1. Extract the argument of the "\pattern" sequence, i. e. # anything between "\pattern{" and the next "}". # 2. As an exception, comments are always preserved. my $url = $_[0], $in = $_[1], $out = $_[2]; open IN, $in or die die "Cannot open $in for reading: $!"; open OUT, "> $out" or die "Cannot open $out for writing: $!"; printf OUT "%% The original file was downloaded from\n"; printf OUT "%%\n"; printf OUT "%% $url\n"; printf OUT "%%\n"; printf OUT "%% and automatically translated into this format. The original comments,\n"; printf OUT "%% including the original copyright notice, are preserved.\n"; printf OUT "%%\n"; printf OUT "%% -----------------------------------------------------------------------------\n"; printf OUT "%%\n"; $inpatterns = 0; while () { if (/^%/) { # Adopt all comments print OUT; } elsif (!$inpatterns) { if (/\\patterns\s*{/) { $inpatterns = 1; } } else { if (/}/) { $inpatterns = 0; } else { print OUT; } } } close IN; close OUT; printf "Wrote pattern file: $out\n"; } sub dlpatfile { my $lang = $_[0], $destdir = $_[1], $lang1, $lang2; if ($lang =~ /(.*):(.*)/) { $lang1 = $1; $lang2 = $2; } else { $lang1 = $lang2 = $lang; } my $url = "ftp://ftp.mpi-sb.mpg.de/pub/tex/mirror/ftp.dante.de/pub/tex/language/hyph-utf8/tex/generic/hyph-utf8/patterns/tex/hyph-$lang1.tex"; my $tmp = tmpnam(); if (system("wget -O $tmp $url") == 0) { my $out = "$destdir/$lang2.pat"; trpatfile $url, $tmp, $out; unlink $tmp; } else { print "Error downloading patterns for language $lang1. See messages above for details.\n" } } $destdir = getpattendir; foreach $lang (@ARGV) { if ($lang eq '-?' || $lang eq '--?' || $lang eq '-h' || $lang eq '--h' || $lang eq '-help' || $lang eq '--help') { printhelp; } else { dlpatfile $lang, $destdir; } }