summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/source/latex/latex-tds/lib/adjust_checksum.pl
blob: b3866f0512ceeb0af8f0a8859e6e181aa73c0ef1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}' && eval 'exec perl -S $0 $argv:q'
  if 0;
use strict;
$^W=1; # turn warning on
#
# adjust_checksum.pl
#
# Copyright (C) 2006-2013 by Heiko Oberdiek <heiko.oberdiek at googlemail.com>
#
# This work may be distributed and/or modified under the
# conditions of the LaTeX Project Public License, either
# version 1.3 of this license or (at your option) any later
# version. The latest version of this license is in
#    http://www.latex-project.org/lppl.txt
# and version 1.3 or later is part of all distributions of
# LaTeX version 2005/12/01 or later.
#
# This work has the LPPL maintenance status "maintained".
#
# This Current Maintainer of this work is Heiko Oberdiek.
#
# This work consists of this file.
#
# This file "adjust_checksum.pl" may be renamed to "adjust_checksum"
# for installation purposes.
#
my $file        = "adjust_checksum.pl";
my $program     = uc($&) if $file =~ /^\w+/;
my $version     = "1.6";
my $date        = "2013/01/24";
my $author      = "Heiko Oberdiek";
my $copyright   = "Copyright (c) 2002, 2006-2013 by $author.";
#
# Reqirements: Perl5, Linux
# History:
#   2002/03/15 v1.0: First release.
#   2006/02/12 v1.1: Fix for \Checksum{0}.
#   2007/06/15 v1.2: -draftmode added.
#   2007/09/25 v1.3: ltxdoc.cfg added to speed up.
#   2008/07/16 v1.4: Use of package `syntonly' for speed.
#   2011/04/15 v1.5: Email address updated.
#   2013/01/26 v1.6: License updated.
#

my $prg_latex = 'latex -draftmode';

my $tempdir = "tmp_\L$program\E_$$";

### program identification
my $title = "$program $version, $date - $copyright\n";

my $usage = <<"END_OF_USAGE";
${title}Syntax: \L$program\E <file.dtx>
Function: Correction of "\\Checksum{...}" entry in <file.dtx>.
END_OF_USAGE

### error strings
my $Error = "!!! Error:"; # error prefix

### parse command line arguments
@ARGV == 1 or die $usage;
my $dtxfile = $ARGV[0];

print $title;

### check dtx file
-f $dtxfile or die "$Error File not found: '$dtxfile'!\n";

### signals
$SIG{__DIE__} = \&clean;
$SIG{'HUP'}   = \&clean;
$SIG{'INT'}   = \&clean;
$SIG{'QUIT'}  = \&clean;
$SIG{'TERM'}  = \&clean;

### make temp dir
mkdir $tempdir;
-d $tempdir or die "$Error Cannot create directory '$tempdir'!\n";

### copy dtx file
my $tempdtxfile = $dtxfile;
$tempdtxfile =~ s/.*\///;
my $latexfile = $tempdtxfile;
$tempdtxfile = $tempdir . "/" . $tempdtxfile;

system("cp $dtxfile $tempdtxfile");
-f $tempdtxfile or die "$Error Cannot copy dtx file!\n";

### create ltxdoc.cfg
my $ltxdocfile = $tempdir . "/" . 'ltxdoc.cfg';
open(OUT, '>', $ltxdocfile)
        or die "!!! Error: Cannot open file `$ltxdocfile'!\n";
print OUT <<'__END__LTXDOC_CFG__';
\typeout{* version for adjust_checksum}
\AtEndOfClass{%
  \DontCheckModules
  \DisableCrossrefs
  \def\DisableCrossrefs{\@bsphack\@esphack}%
  \let\EnableCrossrefs\DisableCrossrefs
  \let\CodelineIndex\relax
  \let\PageIndex\relax
  \let\CodelineNumbered\relax
  \let\PrintChanges\relax
  \let\PrintIndex\relax
  \let\tableofcontents\relax
  \PassOptionsToPackage{hyperref}{bookmarks=false}%
  \PassOptionsToPackage{hypdoc}{numbered}%
  \expandafter\xdef\csname ver@hypdoc.sty\endcsname{}%
  \expandafter\xdef\csname ver@bmhydoc.sty\endcsname{}%
  \nofiles
  \hfuzz\maxdimen
  \pretolerance10000 %
  \tolerance10000 %
}
\endinput
__END__LTXDOC_CFG__
close(OUT);

### run latex
print "*** Running LaTeX ...\n";
my $argcode = "'\\AtBeginDocument{\\usepackage{syntonly}\\syntaxonly}"
        . "\\input{$latexfile}'";
system("(cd $tempdir; $prg_latex --interaction=batchmode $argcode)");

my $logfile = $tempdtxfile;
$logfile =~ s/\.[^\.]+$//;
$logfile .= ".log";
-f $logfile or die "$Error Cannot find log file '$logfile'!\n";

### parse log file for CheckSum
print "*** Looking for checksum statement ...\n";
my $found = 0;
my $changed = 0;
my $old = 0;
my $new = 0;
open(LOG, $logfile) or die "$Error Cannot open log file '$logfile'!\n";
while (<LOG>) {
  if (/\* Checksum passed \*/) {
    $found = 1;
    $changed = 0;
    print "==> Checksum passed.\n";
    last;
  }
  if (/Checksum not passed \((\d+)<>(\d+)\)/) {
    $found = 1;
    $changed = 1;
    $old = $1;
    $new = $2;
    last;
  }
  if (/The checksum should be (\d+)!/) {
    $found = 1;
    $changed = 1;
    $old = 0;
    $new = $1;
    last;
  }
}
close(LOG);

$found or die "$Error Checksum statement not found in log file!\n";

if ($changed) {
  print "==> Checksum not passed ($old<>$new).\n";

  ### write changed dtx file
  print "*** Fixing Checksum ...\n";
  my $fixed = 0;
  open(IN, $tempdtxfile) or die "$Error Cannot open '$tempdtxfile'!\n";
  open(OUT, ">$dtxfile") or die "$Error Cannot write '$dtxfile'!\n";
  while (<IN>) {
    if (s/\\CheckSum\{\d+\}/\\CheckSum{$new}/) {
      $fixed++;
      print "==> Checksum fixed:\n$_";
    }
    print OUT;
  }
  close(IN);
  close(OUT);

  $fixed > 0 or
      die "$Error: \"\\CheckSum{...}\" not found!\n";
  $fixed == 1 or
      die "$Error: More than one \"\\CheckSum\" command found!\n";
}

### cleaning
sub clean {
  if (-d $tempdir) {
    unlink glob("$tempdir/*");
    rmdir $tempdir;
  }
}

clean();

print "*** Ready.\n";

__END__