#!/usr/bin/env perl use POSIX; use File::Basename; use Net::FTP; use Getopt::Long; $host = "mirrors.dotsrc.org"; $basesourcedir = "/ctan"; $sourcedir = ""; # Determine ${prefix}, for the default target directory for pattern # files. Two different strategies ... $makefile = (dirname $0) . "/Makefile"; if (-e $makefile) { # 1. Makefile exists in the same directory, so it can be assumed # that this script is not yet installed, but called from the source # directory. Search Makefile for prexix. open MF, $makefile or die "Cannot open $makefile: $!"; while () { if (/^\s*prefix\s*=\s*(.*)\s*$/) { $prefix = $1; } } close MF; if ($prefix eq "") { die "Prefix not defined in $makefile."; } } else { # 2. Assume that this is script is installed, so its path contains # the prefix. if ($0 =~ /(.*)\/bin\/[^\/]+$/) { $prefix = $1; } else { die "Failed to determine prefix from $0."; } } $targetdir = "$prefix/lib/dillo/hyphenation"; if (!GetOptions ("host=s" => \$host, "basesourcedir=s" => \$basesourcedir, "sourcedir=s" => \$sourcedir, "targetdir=s" => \$targetdir) || @ARGV == 0) { print "Usage: $0 [OPTIONS] LANG [LANG ...]\n\n"; print <new($host,Timeout=>240) or die "Cannot connect to $host: $!"; $ftp->login() or die "Cannot login: $!"; $ftp->cwd($sourcedir) or die "Cannot change to directory $sourcedir: $!"; @files = $ftp->ls or die "Cannot read directory: $!"; # Finally, read pattern files. foreach $arg (@ARGV) { if ($arg =~ /^([a-z]+)-.*$/) { # More files per language, e. g. "en-gb". $lang = $1; } else { # One file per language, e. g. "ru". $lang = $arg; } # First, donwload the pattern file to a temporary file. $tmppat = tmpnam(); if ($ftp->get ("hyph-$arg.pat.txt", $tmppat)) { printf ("Successfully downloaded pattern file for \"$arg\".\n"); # Search for a licence file. (Only a warning, when it does # not exist.) $tmplic = tmpnam(); $licfound = 0; if ($ftp->get ("hyph-$arg.lic.txt", $tmplic)) { $licfound = 1; } else { print "Warning: Cannot download license file for \"$arg\": $!\n"; } # Combine both, licence and pattern, to the final pattern # file. $outfile = "$targetdir/$lang.pat"; open OUT, "> $outfile" or die "Cannot open $outfile: $!"; if ($licfound) { print OUT "% Licence from ftp://$host$sourcedir/hyph-$arg.lic.txt\n"; print OUT "%\n"; open IN, $tmplic or die "Cannot open $tmplic: $!"; while () { # Each line from the licence file must be a comment. if (!/^%/) { print OUT "% "; } print OUT; } close IN; unlink $tmplic; print OUT "%\n"; } print OUT "% Patterns from ftp://$host$sourcedir/hyph-$arg.pat.txt\n"; print OUT "%\n"; open IN, $tmppat or die "Cannot open $tmppat: $!"; while () { print OUT; } close IN; unlink $tmppat; close OUT; } else { # Not found. If a single language was specified (e. g. "en"), # search for possibilities. print "Error: Cannot download pattern file for \"$arg\": $!\n"; if ($lang eq $arg) { print "Try one of these:\n"; foreach(@files) { if (/^hyph-($lang-.*)\.pat\.txt$/) { print " $1\n"; } } } } } $ftp->quit; }