Debian Codegraph @

texmf-dist/source/latex/proposal/bin/checksum

raw
1 #!/usr/bin/perl -w
2 #######################################################################################
3 # A tool for updating .dtx files with a correspodning checksum counter #
4 # Copyright (c) 2010, Deyan Ginev, released under the Gnu General Public License (GPL)#
5 # see http://www.gnu.org/copyleft/gpl.html #
6 # $URL: https://svn.kwarc.info/repos/stex/trunk/bin/checksum$ #
7 #######################################################################################
8
9 use strict;
10
11 use Getopt::Long;
12 use Modparse;
13 use Pod::Usage;
14 use Cwd qw(abs_path);
15
16 ####### start of program #######
17 my $mode = "update"; #Default is update
18 my $action = { "update" => \&update, "disable" => \&disable, "enable"=>\&enable };
19 GetOptions("disable" => sub { $mode="disable"; },
20 "enable" => sub { $mode="enable"; },
21 "update" => sub { $mode="update"; });
22
23
24 my ($path) = @ARGV;
25 $path = abs_path($path);
26 my ($volume,$dir,$file) = File::Spec->splitpath( $path );
27 my @lines = `cd $dir; pdflatex "\\nonstopmode\\input{$file}"` if ($mode eq "update");
28 my ($checksum) = map {$_=~/^\* The checksum should be (\d+)!/; $1;} grep ($_ =~ /^\* The checksum should be (\d+)!/,@lines) if @lines;
29 if (!$checksum) {
30 #One more possible error message:
31 ($checksum) = map {$_=~/^! Package doc Error: Checksum not passed \((\d+)<>(\d+)\)\./; $2;} grep ($_ =~ /^! Package doc Error: Checksum not passed \((\d+)<>(\d+)\)\./,@lines) if @lines;
32 }
33 open(IN,"<$path") or die "Cannot open DTX source: $path\n";
34 @lines = ();
35 while (<IN>) {
36 push @lines, &{$$action{$mode}}($_,$checksum);
37 }
38 close(IN);
39 open(OUT,">$path") or die "Cannot write to DTX target: $path\n";
40 print OUT join("",@lines);
41 close(OUT);
42 ######### Subroutines ############
43 sub update {
44 my $checksum=$_[1];
45 $_[0]=~s/\\CheckSum\{\d*\}/\\CheckSum{$checksum}/ if $checksum;
46 $_[0];
47 }
48
49 sub disable {
50 $_[0]=~s/\\CheckSum\{(\d*)\}/\\iffalse\\CheckSum\{$1}\\fi/ unless $_[0]=~/\\iffalse\\CheckSum\{(\d*)\}\\fi/;
51 $_[0];
52 }
53
54 sub enable {
55 $_[0]=~s/\\iffalse\\CheckSum\{(\d*)\}\\fi/\\CheckSum{$1}/;
56 $_[0];
57 }
58
59
60 __END__
61
62
63 =head1 SYNOPSIS
64
65 checksum <input filename> --update|enable|disable
66
67 Purpose:
68 Update, enable or disable the \CheckSum macro for a given .dtx source
69
70 Example:
71 checksum omd.dtx --update