| 1 |
#!/usr/bin/env perl |
| 2 |
use strict; |
| 3 |
$^W=1; # turn warning on |
| 4 |
# |
| 5 |
# pdfatfi.pl |
| 6 |
# |
| 7 |
# Copyright (C) 2005-2010, 2012 Heiko Oberdiek. |
| 8 |
# |
| 9 |
# This work may be distributed and/or modified under the |
| 10 |
# conditions of the LaTeX Project Public License, either |
| 11 |
# version 1.3c of this license or (at your option) any later |
| 12 |
# version. This version of this license is in |
| 13 |
# https://www.latex-project.org/lppl/lppl-1-3c.txt |
| 14 |
# and the latest version of this license is in |
| 15 |
# https://www.latex-project.org/lppl.txt |
| 16 |
# and version 1.3 or later is part of all distributions of |
| 17 |
# LaTeX version 2005/12/01 or later. |
| 18 |
# |
| 19 |
# This work has the LPPL maintenance status "maintained". |
| 20 |
# |
| 21 |
# The Current Maintainers of this work are |
| 22 |
# Heiko Oberdiek and the Oberdiek Package Support Group |
| 23 |
# https://github.com/ho-tex/attachfile2/issues |
| 24 |
# |
| 25 |
# See file "attachfile2.pdf" for a list of files that belong to |
| 26 |
# this project. |
| 27 |
# |
| 28 |
# This file "pdfatfi.pl" may be renamed to "pdfatfi" |
| 29 |
# for installation purposes. |
| 30 |
# |
| 31 |
my $prj = 'pdfatfi'; |
| 32 |
my $file = "$prj.pl"; |
| 33 |
my $program = uc($&) if $file =~ /^\w+/; |
| 34 |
my $version = "2.9"; |
| 35 |
my $date = "2019/11/26"; |
| 36 |
my $author = "Heiko Oberdiek"; |
| 37 |
my $copyright = "Copyright (c) 2005-2010, 2012 by $author."; |
| 38 |
# |
| 39 |
# History: |
| 40 |
# 2005/05/21 v1.0: First release. |
| 41 |
# 2006/08/16 v2.2: Included in DTX file of attachfile2.dtx. |
| 42 |
# 2010/09/27 v2.6: Keys ModDateTZ and CreationDateTZ added. |
| 43 |
# 2016/05/16 v2.8: Option --version added. |
| 44 |
# 2019/11/24 v2.10: adapted version to attachfile2 version. |
| 45 |
# |
| 46 |
|
| 47 |
use POSIX qw(strftime); # %z is used (GNU) |
| 48 |
use Digest::MD5; |
| 49 |
|
| 50 |
### program identification |
| 51 |
my $title = "$program $version, $date - $copyright\n"; |
| 52 |
|
| 53 |
### error strings |
| 54 |
my $Error = "!!! Error:"; # error prefix |
| 55 |
|
| 56 |
### variables |
| 57 |
my $atfifile; |
| 58 |
|
| 59 |
### option variables |
| 60 |
my @bool = ("false", "true"); |
| 61 |
$::opt_help = 0; |
| 62 |
$::opt_quiet = 0; |
| 63 |
$::opt_debug = 0; |
| 64 |
$::opt_verbose = 0; |
| 65 |
$::opt_version = 0; |
| 66 |
|
| 67 |
my $usage = <<"END_OF_USAGE"; |
| 68 |
${title}Syntax: \L$program\E [options] <file[.atfi]> |
| 69 |
Function: Help program for LaTeX package "attachfile2". |
| 70 |
Options: (defaults:) |
| 71 |
--help print usage |
| 72 |
--version print version number |
| 73 |
--(no)quiet suppress messages ($bool[$::opt_quiet]) |
| 74 |
--(no)verbose verbose printing ($bool[$::opt_verbose]) |
| 75 |
--(no)debug debug informations ($bool[$::opt_debug]) |
| 76 |
END_OF_USAGE |
| 77 |
|
| 78 |
### process options |
| 79 |
my @OrgArgv = @ARGV; |
| 80 |
use Getopt::Long; |
| 81 |
GetOptions( |
| 82 |
"help!", |
| 83 |
"version!", |
| 84 |
"quiet!", |
| 85 |
"debug!", |
| 86 |
"verbose!", |
| 87 |
) or die $usage; |
| 88 |
!$::opt_help or die $usage; |
| 89 |
if ($::opt_version) { |
| 90 |
print "$prj $date v$version\n"; |
| 91 |
exit(0); |
| 92 |
} |
| 93 |
@ARGV == 1 or die "$usage$Error Missing jobname!\n"; |
| 94 |
|
| 95 |
$::opt_quiet = 0 if $::opt_verbose; |
| 96 |
|
| 97 |
print $title unless $::opt_quiet; |
| 98 |
|
| 99 |
### get jobname |
| 100 |
$atfifile = $ARGV[0]; |
| 101 |
if (!-f $atfifile && -f "$atfifile.atfi") { |
| 102 |
$atfifile .= ".atfi"; |
| 103 |
} |
| 104 |
-f $atfifile or die "$Error File `$atfifile' not found!\n"; |
| 105 |
|
| 106 |
print "* job file = $atfifile\n" if $::opt_verbose; |
| 107 |
|
| 108 |
if ($::opt_debug) { |
| 109 |
print <<"END_DEB"; |
| 110 |
* OSNAME: $^O |
| 111 |
* PERL_VERSION: $] |
| 112 |
* ARGV: @OrgArgv |
| 113 |
END_DEB |
| 114 |
} |
| 115 |
|
| 116 |
my $tmpfile = $atfifile . ".tmp"; |
| 117 |
|
| 118 |
my $timezone = strftime "%z", localtime; |
| 119 |
|
| 120 |
sub gettz ($) { |
| 121 |
my $time = shift; |
| 122 |
my $tz = strftime "%z", localtime($time); |
| 123 |
return '' unless $tz; |
| 124 |
return 'Z' if $tz eq '+0000'; |
| 125 |
$tz =~ s/^([+\-]\d\d)(\d\d)$/$1'$2'/; |
| 126 |
return $tz; |
| 127 |
} |
| 128 |
|
| 129 |
open(IN, $atfifile) or die "$Error Cannot open `$atfifile'!\n"; |
| 130 |
open(OUT, ">$tmpfile") or die "$Error Cannot write `$tmpfile'!\n"; |
| 131 |
|
| 132 |
while(<IN>) { |
| 133 |
# timezone |
| 134 |
if (s/^(\\attachfile\@timezone\{).*(\})$/$1$timezone$2/) { |
| 135 |
print "* timezone = $timezone\n" if $::opt_verbose; |
| 136 |
} |
| 137 |
|
| 138 |
# file entry |
| 139 |
if (/^\\attachfile\@file\[[^\]]*\]\{(.*)\}$/) { |
| 140 |
my $hexfile = $1; |
| 141 |
my $file = pack('H*', $hexfile); |
| 142 |
my @s = stat($file); |
| 143 |
if (@s == 0) { |
| 144 |
print "!!! Warning: File `$file' not found!\n"; |
| 145 |
} |
| 146 |
else { |
| 147 |
my $size = $s[7]; |
| 148 |
my $mtime = $s[9]; |
| 149 |
my $ctime = $s[10]; # inode change time |
| 150 |
|
| 151 |
my ($sec, $min, $hour, $mday, $mon, $year) = |
| 152 |
localtime($mtime); |
| 153 |
my $moddate = sprintf("%04d%02d%02d%02d%02d%02d", |
| 154 |
$year + 1900, $mon + 1, $mday, |
| 155 |
$hour, $min, $sec); |
| 156 |
my $moddatetz = 'D:' . $moddate . gettz($mtime); |
| 157 |
|
| 158 |
# Manual page "perlport" says that "ctime" is creation |
| 159 |
# time instead of inode change time for "Win32" and |
| 160 |
# "Mac OS", but it is unsupported for "Mac OS X". |
| 161 |
my $creationdate = ""; |
| 162 |
my $creationdatetz = ""; |
| 163 |
if ($^O eq 'MSWin32') { # cygwin? |
| 164 |
($sec, $min, $hour, $mday, $mon, $year) = |
| 165 |
localtime($ctime); |
| 166 |
$creationdate = sprintf("%04d%02d%02d%02d%02d%02d", |
| 167 |
$year + 1900, $mon + 1, $mday, |
| 168 |
$hour, $min, $sec); |
| 169 |
$creationdatetz = |
| 170 |
'D:' . $creationdate . gettz($ctime); |
| 171 |
} |
| 172 |
|
| 173 |
# md5 checksum |
| 174 |
my $checksum = ""; |
| 175 |
my $ctx = Digest::MD5->new; |
| 176 |
if (open(FILE, $file)) { |
| 177 |
$ctx->addfile(*FILE); |
| 178 |
$checksum = $ctx->hexdigest; |
| 179 |
close(FILE); |
| 180 |
} |
| 181 |
else { |
| 182 |
print "!!! Warning: File `$file' cannot be read," |
| 183 |
. " dropping checksum!\n"; |
| 184 |
} |
| 185 |
|
| 186 |
$_ = "\\attachfile\@file[" |
| 187 |
. "ModDateTZ=$moddatetz" |
| 188 |
. ",Size=$size" |
| 189 |
. ($checksum ? ",CheckSum=$checksum" : "") |
| 190 |
. ($creationdatetz ? |
| 191 |
",CreationDateTZ=$creationdatetz" : |
| 192 |
($creationdate ? |
| 193 |
",CreationDate=$creationdate" : "")) |
| 194 |
. "]{$hexfile}\n"; |
| 195 |
if ($::opt_verbose) { |
| 196 |
print "* file entry = $file\n"; |
| 197 |
print " size = $size\n"; |
| 198 |
print " moddate = $moddate\n"; |
| 199 |
print " creationdate = $creationdate\n" |
| 200 |
if $creationdate; |
| 201 |
print " checksum = $checksum\n" if $checksum; |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
print OUT $_; |
| 207 |
} |
| 208 |
|
| 209 |
close(IN); |
| 210 |
close(OUT); |
| 211 |
|
| 212 |
unlink($atfifile) or die "$Error Cannot delete old `$atfifile'!\n"; |
| 213 |
rename $tmpfile, $atfifile |
| 214 |
or die "$Error Cannot move `$tmpfile' to `$atfifile'!\n"; |
| 215 |
|
| 216 |
print "*** ready. ***\n" unless $::opt_quiet; |
| 217 |
|
| 218 |
__END__ |