o Made several changes in response to suggestions in Wiki.
[project-aon.git] / scripts / addcorr.pl
1 #!/usr/bin/perl -w
2 #
3 # addcorr.pl
4 #
5 # addcorr.pl [inputCorrections HTMLfile(s)]
6 #
7 # Combines the operations of corrtohtml.pl, sortcorrhtml.pl, and
8 # mergecorrhtml.pl into one simple command line. Anything that needs
9 # to do anything more complex than this should use the three utilities
10 # separately.
11 #
12 # The book code will be obtained from the input HTML filename. E.g.
13 # 01fftd-changes.html provides the book code 01fftd. If for some reason
14 # the input HTML filename doesn't hold the book code in the first
15 # characters before the hyphen, use the three separate utilities.
16 #
17 # Anything that isn't specified on the command line will be prompted for.
18 #
19 # This program uses the most used pattern of using these three utilities.
20 #
21 # corrtohtml.pl -v -b bookCode inputCorrections
22 #   | sortcorrhtml.pl -v -s -b bookCode
23 #   | mergecorrhtml.pl inputHTML
24 #   > outputHTML
25 #
26 # This utility also has the side effect of creating a backup copy of
27 # the inputHTML file.
28 #
29 ######################################################################
30 use strict;
31
32 my $programName = "addcorr";
33 my $usage = "$programName [inputCorrections HTMLfile(s)]\n";
34
35 unless( -d $ENV{AONPATH} ) { die "\$AONPATH environment variable doesn't point to a directory"; }
36
37 my $convert = $ENV{AONPATH} . "/bin/corrtohtml.pl";
38 die( "Cannot find executable file \"$convert\"" ) unless( -x $convert );
39 my $sort = $ENV{AONPATH} . "/bin/sortcorrhtml.pl";
40 die( "Cannot find executable file \"$sort\"" ) unless( -x $sort );
41 my $merge = $ENV{AONPATH} . "/bin/mergecorrhtml.pl"; 
42 die( "Cannot find executable file \"$merge\"" ) unless( -x $merge );
43 my $copy = "/bin/cp";
44 die( "Cannot find executable file \"$copy\"" ) unless( -x $copy );
45
46 my $optsProcessed = 0;
47 my $bookCode = "";
48 my $inCorr = "";
49 my $inHTML = "";
50 my $outHTML = "";
51 my $verbose = 0;
52
53 my %books = (
54     '01fftd' => 1,
55     '02fotw' => 1,
56     '03tcok' => 1,
57     '04tcod' => 1,
58     '05sots' => 1,
59     '06tkot' => 1,
60     '07cd' => 1,
61     '08tjoh' => 1,
62     '09tcof' => 1,
63     '10tdot' => 1,
64     '11tpot' => 1,
65     '12tmod' => 1,
66     '13tplor' => 1,
67     '14tcok' => 1,
68     '15tdc' => 1,
69     '16tlov' => 1,
70     '17tdoi' => 1,
71     '18dotd' => 1,
72     '19wb' => 1,
73     '20tcon' => 1,
74     '21votm' => 1,
75     '22tbos' => 1,
76     '23mh' => 1,
77     '24rw' => 1,
78     '25totw' => 1,
79     '26tfobm' => 1,
80     '27v' => 1,
81     '28thos' => 1,
82     '01gstw' => 1,
83     '02tfc' => 1,
84     '03btng' => 1,
85     '04wotw' => 1,
86     '01hh' => 1,
87     '02smr' => 1,
88     '03toz' => 1,
89     '04cc' => 1,
90     'tmc' => 1,
91     'rh' => 1
92 );
93
94 ######################################################################
95
96 while( $#ARGV > -1 && not $optsProcessed ) {
97   my $commandLineItem = shift @ARGV;
98   if( $commandLineItem eq "--help" ) {
99     print $usage and exit;
100   }
101   elsif( $commandLineItem eq "-v" ) {
102     $verbose = 1;
103   }
104   else {
105     unshift @ARGV, $commandLineItem;
106     $optsProcessed = 1;
107   }
108 }
109
110 if( $#ARGV > -1 ) {
111     $inCorr = shift @ARGV or die "Couldn't get input corrections\n$usage";
112     $inHTML = shift @ARGV or die "Couldn't get input HTML\n$usage";
113 }
114 else {
115     while( $inCorr eq "" ) {
116         print "Corrections File: ";
117         $inCorr = <>;
118         chomp( $inCorr );
119     }
120     while( $inHTML eq "" ) {
121         print "Input HTML File: ";
122         $inHTML = <>;
123         chomp( $inHTML );
124     }
125 }
126
127 while( $inHTML ne "" ) {
128     die( "Cannot find corrections file \"$inCorr\"" ) unless( -f $inCorr );
129     die( "Cannot read corrections file \"$inCorr\"" ) unless( -r $inCorr );
130     die( "Cannot find HTML file \"$inHTML\"" ) unless( -f $inHTML );
131     die( "Cannot read HTML file \"$inHTML\"" ) unless( -r $inHTML );
132     die( "Cannot write to HTML file \"$inHTML\"" ) if( -f $inHTML && ! -w $inHTML );
133
134     $bookCode = $inHTML;
135     $bookCode =~ s{^([[:lower:][:digit:]]+)-.*$}{$1};
136     die( "Unknown book code \"$bookCode\" (obtained from \"$inHTML\")" ) unless( exists $books{ $bookCode } );
137
138     print "Bookcode: $bookCode\n" if( $verbose );
139     # leave backup untouched while putting output in original filename
140     print qx{$copy $inHTML $inHTML.backup};
141     $outHTML = $inHTML;
142     $inHTML = "$inHTML.backup";
143
144     print qx{ $convert -v -b $bookCode $inCorr | $sort -v -s -b $bookCode | $merge $inHTML >$outHTML };
145
146     $inHTML = "";
147     $inHTML = shift @ARGV if( $#ARGV > -1 );
148 }