| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
$^W=1; |
| 4 |
|
| 5 |
# Copyright (C) 2008, 2011, 2012 Heiko Oberdiek |
| 6 |
# |
| 7 |
# This program is free software; you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation; either version 2 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This program is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this program; if not, write to the Free Software |
| 19 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, |
| 20 |
# MA 02110-1301 USA |
| 21 |
# |
| 22 |
# This file is part of PDFAnnotExtractor. See README. |
| 23 |
|
| 24 |
my $name = 'PDFAnnotExtractor'; |
| 25 |
my $program = "\L$name\E"; |
| 26 |
my $file = "$program.pl"; |
| 27 |
my $version = "0.1l"; |
| 28 |
my $date = "2012/04/18"; |
| 29 |
my $author = "Heiko Oberdiek"; |
| 30 |
my $copyright = "Copyright (c) 2008, 2011, 2012 by $author."; |
| 31 |
|
| 32 |
# History: |
| 33 |
# 2008/10/01 v0.1i: First version of the wrapper script. |
| 34 |
# 2012/04/18 v0.1l: Option --version added. |
| 35 |
|
| 36 |
my $title = "$name $version, $date - $copyright\n"; |
| 37 |
my $usage = <<"END_OF_USAGE"; |
| 38 |
${title}Syntax: $program [options] <PDF files[.pdf]> |
| 39 |
Options: |
| 40 |
--help print usage |
| 41 |
--version print version number |
| 42 |
--debug debug informations |
| 43 |
END_OF_USAGE |
| 44 |
|
| 45 |
my $help = 0; |
| 46 |
my $debug = 0; |
| 47 |
my $opt_version = 0; |
| 48 |
use Getopt::Long; |
| 49 |
GetOptions( |
| 50 |
'debug!' => \$debug, |
| 51 |
'help!' => \$help, |
| 52 |
'version!' => \$opt_version, |
| 53 |
) or die $usage; |
| 54 |
!$help or die $usage; |
| 55 |
if ($opt_version) { |
| 56 |
print "$name $date v$version\n"; |
| 57 |
exit(0); |
| 58 |
} |
| 59 |
(@ARGV >= 1 or die $usage); |
| 60 |
|
| 61 |
print $title; |
| 62 |
|
| 63 |
my $error = '!!! Error:'; |
| 64 |
my $pdfbox = 'PDFBox'; |
| 65 |
my $prg_kpsewhich = 'kpsewhich'; |
| 66 |
my $prg_java = 'java'; |
| 67 |
my %prg; |
| 68 |
|
| 69 |
my $jar_pax = 'pax.jar'; |
| 70 |
my $main_class = 'pax.PDFAnnotExtractor'; |
| 71 |
my $jar_pdfbox = 'pdfbox.jar'; |
| 72 |
my @jar_pdfbox = qw[ |
| 73 |
pdfbox.jar |
| 74 |
PDFBox.jar |
| 75 |
pdfbox-0.7.3.jar |
| 76 |
PDFBox-0.7.3.jar |
| 77 |
pdfbox-0.7.2.jar |
| 78 |
PDFBox-0.7.2.jar |
| 79 |
]; |
| 80 |
my @dir_jar = qw[ |
| 81 |
/usr/share/java |
| 82 |
/usr/local/share/java |
| 83 |
]; |
| 84 |
my $path_jar_pax = ''; |
| 85 |
my $path_jar_pdfbox = ''; |
| 86 |
my $classpath = defined $ENV{'CLASSPATH'} ? $ENV{'CLASSPATH'} : ''; |
| 87 |
debug('CLASSPATH', $classpath); |
| 88 |
my $pdfbox_in_classpath = $classpath =~ /PDFBox/ ? 1 : 0; |
| 89 |
|
| 90 |
my $is_win = 0; |
| 91 |
$is_win = 1 if $^O =~ /^MSWin(32|64)/i |
| 92 |
or $^O =~ /^dos/i |
| 93 |
or $^O =~ /^os2/i; |
| 94 |
debug('is_win', $is_win); |
| 95 |
|
| 96 |
use File::Which; |
| 97 |
|
| 98 |
sub debug ($$) { |
| 99 |
my $key = shift; |
| 100 |
my $value = shift; |
| 101 |
print "* $key: [$value]\n" if $debug; |
| 102 |
} |
| 103 |
|
| 104 |
sub check_prg ($$) { |
| 105 |
my $prg = shift; |
| 106 |
my $die = shift; |
| 107 |
return 1 if $prg{$prg}; |
| 108 |
my $path = which($prg); |
| 109 |
if ($path) { |
| 110 |
$prg{$prg} = $path; |
| 111 |
debug "Which $prg", $path; |
| 112 |
return 1; |
| 113 |
} |
| 114 |
debug "Which $prg", '<not found>'; |
| 115 |
if ($die) { |
| 116 |
die "$error Cannot find program `$prg'!\n"; |
| 117 |
} |
| 118 |
return 0; |
| 119 |
} |
| 120 |
|
| 121 |
sub find_jar ($) { |
| 122 |
my $jar_name = shift; |
| 123 |
|
| 124 |
check_prg $prg_kpsewhich, 1; |
| 125 |
my $cmd = "kpsewhich" |
| 126 |
. " --progname $program" |
| 127 |
. " --format texmfscripts" |
| 128 |
. " $jar_name"; |
| 129 |
debug 'Backticks', $cmd; |
| 130 |
my $path = `$cmd`; |
| 131 |
if ($? == 0) { |
| 132 |
chomp $path; |
| 133 |
debug 'Exit code', '0/success'; |
| 134 |
debug $jar_name, $path; |
| 135 |
return $path; |
| 136 |
} |
| 137 |
if ($? == -1) { |
| 138 |
die "!!! Error: Cannot execute `$prg_kpsewhich' ($!)!\n"; |
| 139 |
} |
| 140 |
if ($? & 127) { |
| 141 |
die "!!! Error: `$prg_kpsewhich' died with signal " . ($? & 127) |
| 142 |
. (($? & 128) ? ' with coredump' : '') . "!\n"; |
| 143 |
} |
| 144 |
debug 'Exit code', ($? >> 8); |
| 145 |
return ''; |
| 146 |
} |
| 147 |
|
| 148 |
sub find_jar_pax () { |
| 149 |
return if $path_jar_pax; |
| 150 |
foreach my $dir (@dir_jar) { |
| 151 |
my $path = "$dir/$jar_pax"; |
| 152 |
if (-f $path) { |
| 153 |
$path_jar_pax = $path; |
| 154 |
debug $jar_pax, $path_jar_pax; |
| 155 |
return; |
| 156 |
} |
| 157 |
} |
| 158 |
$path_jar_pax = find_jar $jar_pax; |
| 159 |
if (!$path_jar_pax) { |
| 160 |
die "$error Cannot find `$jar_pax'!\n"; |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
sub launch_pax () { |
| 166 |
check_prg $prg_java, 1; |
| 167 |
my @cmd = ($prg_java); |
| 168 |
push @cmd, '-cp'; |
| 169 |
push @cmd, $path_jar_pax; |
| 170 |
push @cmd, 'pax.PDFAnnotExtractor'; |
| 171 |
push @cmd, @ARGV; |
| 172 |
debug 'System', "@cmd"; |
| 173 |
system @cmd; |
| 174 |
if ($? == 0) { |
| 175 |
debug 'Result', 'ok'; |
| 176 |
return 0; |
| 177 |
} |
| 178 |
if ($? == -1) { |
| 179 |
die "$error Cannot execute `$prg_java' ($!)!\n"; |
| 180 |
} |
| 181 |
if ($? & 127) { |
| 182 |
die "$error `$prg_java' died with signal " . ($? & 127) |
| 183 |
. (($? & 128) ? ' with coredump' : '') . "!\n"; |
| 184 |
} |
| 185 |
my $exit_code = $? >> 8; |
| 186 |
debug 'Exit code', $exit_code; |
| 187 |
return $exit_code; |
| 188 |
} |
| 189 |
|
| 190 |
# main program |
| 191 |
|
| 192 |
my $ret = 0; |
| 193 |
find_jar_pax; |
| 194 |
exit launch_pax; |
| 195 |
|
| 196 |
__END__ |