summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/scripts/jmlr/makejmlrbook
blob: 77669bbff3aaea15220e3a4ec3131c9e17dcd7dd (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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#!/usr/bin/env perl
# File          : makejmlrbook
# Author        : Nicola L C Talbot
# Date          : 22nd March 2010
# Last Modified : 24nd March 2010
# Version       : 0.1
# Description   : Given the name of a document using the
#                 jmlrbook class file, this script runs
#                 pdflatex (and possibly bibtex) on the
#                 imported articles and the main document.
# http://theoval.cmp.uea.ac.uk/~nlct/
#
# This file is distributed as part of the jmlr LaTeX bundle.
#
# Copyright (c) 2006 Nicola L.C. Talbot
# 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 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'.
#
# The Current Maintainer of this work is Nicola Talbot.

use Getopt::Long;
use File::Basename;
use File::Copy;
use Cwd;
use strict;

my $version = "0.1 (2010-03-24)";

my $eol = "\n";
my $dd  = '/';

if ($^O eq 'MSWin32')
{
  $eol = "\r\n";
  $dd = "\\";
}

my $showversion      = 0;
my $showhelp         = 0;
my $quiet            = 0;
my $batchtex         = 0;
my $online           = 1;
my $print            = 1;
my $html             = 1;
my $latexapp         = 'pdflatex';
my $bibtexapp        = 'bibtex';
my $latexopts        = '';
my $bibtexopts       = '';
my $format           = 'pdf';
my $logourl          = '';

unless(&GetOptions(
   "online!"          => \$online,
   "print!"           => \$print,
   "html!"            => \$html,
   "logourl=s"        => \$logourl,
   "format=s"         => \$format,
   "latexapp=s"       => \$latexapp,
   "bibtexapp=s"      => \$bibtexapp,
   "latexopts=s"      => \$latexopts,
   "bibtexopts=s"     => \$bibtexopts,
   "quieter!"           => \$quiet,
   "batchtex!"        => \$batchtex,
   "version"          => \$showversion,
   "help"             => \$showhelp))
{
   die "$!\n", "Use --help for help\n";
}

my $appname = basename($0);

if ($showhelp)
{
  die "$appname version $version Copyright (c) 2010 Nicola L C Talbot\n",
    "Distributed under the LaTeX Project Public License.\n\n",
    "Syntax: $appname [options] <filename>\n\n",
    "<filename> should be the name of the master document for a LaTeX \n",
    "file that uses the jmlrbook class. The .tex extension may be\n",
    "omitted.\n\n",
    "Basic options:\n",
    "--online\t\tGenerate the color on-line version (default)\n",
    "--noonline\t\tDon't generate the color on-line version\n",
    "--print\t\t\tGenerate the grayscale print version (default)\n",
    "--noprint\t\tDon't generate the grayscale print version\n",
    "--html\t\t\tGenerate the HTML version (default)\n",
    "--nohtml\t\tDon't generate the HTML version\n",
    "--logourl <url>\tMake the logo on the HTML index page link to <url>\n",
    "--batchtex\t\tRun TeX in batch mode\n",
    "--nobatchtex\t\tDon't run TeX in batch mode\n",
    "--quieter\t\tReduce messages to stdout and run TeX in batch mode\n",
    "--noquieter\t\tDon't reduce messages to stdout\n",
    "--version\t\tDisplay version number and exit\n",
    "--help\t\t\tDisplay help message and exit\n",
    "\nAdvanced options:\n",
    "--latexapp <name>\tApplication used to call LaTeX\n",
    "\t\t\t(Defaults to 'pdflatex')\n",
    "--format <string>\tOutput format (default: 'pdf')\n",
    "--bibtexapp <name>\tApplication used to call BibTeX\n",
    "\t\t\t(Defaults to 'bibtex')\n",
    "--latexopt <string>\tOptions to pass to LaTeX\n",
    "--bibtexopt <string>\tOptions to pass to BibTeX\n";
}

if ($showversion)
{
  die "$appname version $version copyright (c) 2010 Nicola L C Talbot\n",
    "Distributed under the LaTeX Project Public License.\n";
}

unless ($#ARGV == 0)
{
   die "$appname: missing filename\n",
       "use --help for help\n";
} 

if ($quiet or $batchtex)
{
   $latexopts = '-interaction batchmode '.$latexopts;
}

if ($quiet)
{
   $bibtexopts = '-terse '.$bibtexopts;
}

my ($main_name, $main_path, $main_suffix) 
  = fileparse($ARGV[0], '\.(tex|ltx)');

$main_suffix = '.tex' unless $main_suffix;

my $org_dir = cwd();

chdir $main_path
  or die "Can't change directory to '$main_path': $!\n";

my $mainaux = "$main_name.aux";

# If the aux file doesn't exist or the aux file is older than
# the main file, run latex

if (not -e $mainaux or (-M $mainaux > -M "$main_name$main_suffix"))
{
   &latex($main_name);
}

# Parse aux file

open AUX, "$mainaux" or die "Can't open '$mainaux' $!\n";

&message("Reading '$mainaux'...\n");

my $main_bibdata = 0;
my @imports = ();
my %pagerefs = ();

while (<AUX>)
{
   if (/\\bibdata\b/)
   {
     $main_bibdata = 1;
   }
   elsif (/\\\@jmlr\@import{(.*)}{(.*)}{(.*)}/)
   {
      my $import = 
        {
          label => $1,
          path  => $2,
          name  => $3
        };

      $import->{'name'} =~s/\.(tex|ltx)\Z//;

      push @imports, $import;
   }
   elsif (/\\contentsline\s*{chapterauthor}{(.*)}{}{}}\s*$/
     and $#imports > -1)
   {
      $imports[$#imports]->{'author'} = $1;
   }
   elsif (/^\\newlabel\s*{([^}]*)jmlrstart}{{([^}]*)}{([^}]*)}/)
   {
      my $label = $1;
      
      $pagerefs{$label}->{'start'} = $3;
   }
   elsif (/^\\newlabel\s*{([^}]*)jmlrend}{{([^}]*)}{([^}]*)}/)
   {
      my $label = $1;

      $pagerefs{$label}->{'end'} = $3;
   }
}

close AUX;

# Replace any instances of \articlepagesref

foreach my $import (@imports)
{
   my $label = $import->{'label'};

   my $pages = $pagerefs{$label}->{'start'}.'--'
             . $pagerefs{$label}->{'end'};

   $import->{'author'}=~s/\\articlepagesref\s*{$label}/$pages/;
}

if ($html)
{
   # If the html files need to be created, make the directory
   # html-src, if it doesn't already exist

   unless (-d 'html-src')
   {
      mkdir 'html-src'
        or die "Can't create directory 'html-src': $!\n";
   }

   unless (-d 'html')
   {
      mkdir 'html'
        or die "Can't create directory 'html': $!\n";
   }
}

# Iterate through each imported article

foreach my $import (@imports)
{
   my $importbase = &fname($import->{'path'}, $import->{'name'});

   # Check the aux file of this article

   my $aux = "$importbase.aux";

   # The aux file should exist because running LaTeX on the
   # main file will create the aux file.

   open AUX, $aux or die "Can't open '$aux' $!\n";

   &message("Reading '$aux'...\n");

   my $bibdata = 0;

   while (<AUX>)
   {
      if (/\\bibdata\b/)
      {
         $bibdata = 1;
      }
   }
  
   close AUX;

   # Do we need a bibtex run?

   if ($bibdata)
   {
      my $log = "$importbase.log";

      # If the log file doesn't exist, run LaTeX

      unless (-e $log)
      {
        chdir $import->{'path'}
          or die "Can't change directory to '$import->{path}': $!\n";

         &latex($import->{'name'});

         chdir "$org_dir/$main_path" or
           die "Can't change directory to '$org_dir/$main_path': $!\n";
      }

      open LOGFD, $log or die "Can't open '$log' $!\n";

      &message("Reading '$log'...\n");

      my $runbibtex = 0;

      while (<LOGFD>)
      {
         if (/There were undefined citations\./)
         {
            # Run bibtex and latex
            $runbibtex = 1;

            last;
         }
      }

      close LOGFD;

      if ($runbibtex)
      {
        chdir $import->{'path'}
          or die "Can't change directory to '$import->{path}': $!\n";

         &bibtex($import->{'name'});
         &latex($import->{'name'});

         chdir "$org_dir/$main_path" or
           die "Can't change directory to '$org_dir/$main_path': $!\n";
      }
   }

   if ($html)
   {
     # If html is required, we also need pdf versions of the
     # individual articles.

      # Does the pdf file exist?

      unless (-e "$importbase.$format")
      {
        chdir $import->{'path'}
          or die "Can't change directory to '$import->{path}': $!\n";

         &latex($import->{'name'});

         chdir "$org_dir/$main_path" or
           die "Can't change directory to '$org_dir/$main_path': $!\n";
      }

      # Do we need a rerun?

      if (&needs_rerun($importbase))
      {
        chdir $import->{'path'}
          or die "Can't change directory to '$import->{path}': $!\n";

         &latex($import->{'name'});

         chdir "$org_dir/$main_path" or
           die "Can't change directory to '$org_dir/$main_path': $!\n";
      }

      my $importdir = $import->{'path'};

      if ($importdir = '.')
      {
         $importdir = $import->{'name'};
      }

      # Check the appropriate subdirectory is in html-src

      my $name = &fname('html-src', $importdir);

      unless (-d $name)
      {
         mkdir $name 
           or die "Can't create directory '$name': $!\n";
      }

      my $text = '';

      # Read the LaTeX file and store everything up to
      # the end of the abstract

      my $tex = "$importbase.tex";

      open TEX, $tex or die "Can't open '$tex': $!\n";

      while (<TEX>)
      {
         # This doesn't handle complicated cases, such as
         # the author using \abstract ... \endabstract
         # or commenting out the abstract with conditionals
         if (/^([^%]*)\\end{abstract}/)
         {
            $text .= $&;
            last;
         }

         $text .= $_;
      }

      close TEX;

      # Add the 'html' class option:
      unless ($text=~
       s/^([^%]*)\\documentclass\s*\[(.*)\]/$1\\documentclass[$2,html]/m)
      {
        $text=~s/^([^%]*)\\documentclass\s*/$1\\documentclass[html]/m;
      }

      my $begindoc = '';

      # Set the authors
      if (defined($import->{'author'}))
      {
         my $author = $import->{'author'};

         $author=~s/^([^;]*);/\\textbf{\\emph{$1};}/;

         $begindoc .= "\\jmlrauthors{$author}";
      }

      # Add content div
      $text=~s/^([^%]*)\\begin{document}/$&$begindoc\\HCode{<div id="content">}/m;

      # Create file containing the abstract

      my $absfile = "html-src/$importdir/".$import->{'name'}.'.tex';

      open ABSFD,">$absfile" 
        or die "Can't create '$absfile': $!\n";

      print ABSFD "\\batchmode", $eol if ($batchtex or $quiet);

      my $texpath = $import->{path};

      $texpath=~s/\\/\//g if ($dd eq '\\');

      print ABSFD
        "\\makeatletter",$eol,
        "\\def\\input\@path{{../../$texpath/}}$eol",
        "\\makeatother",$eol,
        $text, $eol, 
        "\\HCode{</div>}", $eol,
        "\\end{document}",$eol;

      close ABSFD;

      # Create the cfg file

      my $cfg = "html-src/$importdir/jmlr.cfg";

      open CFG, ">$cfg" or die "Can't create '$cfg': $!\n";

print CFG <<END_CFG;
\\Preamble{html}
\\begin{document}
\\Css{div.maketitle {text-align:left;}}
\\Css{h2.titleHead {text-align:left;}}
\\Css{. {font-family:verdana,helvetica,sans-serif}}
\\Css{a {text-decoration:none;color:\\#3030a0}}
\\Css{.cmbx-10x-x-109{ font-weight: bold;}}
\\Css{.cmbxti-10x-x-109{ font-weight: bold; font-style: italic;}}
\\EndPreamble
END_CFG

      close CFG;

      # Run htlatex

      # Change directory
      chdir "html-src/$importdir"
        or die "Can't change directory to 'html-src/$importdir': $!\n";


      my $code = system("htlatex \"".$import->{'name'}."\" \"jmlr\"");

      if ($code)
      {
         die "htlatex failed with exit code $code\n";
      }

      # Go back to main directory
      chdir "$org_dir/$main_path"
        or die "Can't change directory to '$org_dir/$main_path': $!\n";

      # Copy the html file to the html directory, but rename
      # the css file to jmlr.css

      my $infile = "html-src/$importdir/".$import->{'name'}.".html";

      open INFD, $infile or die "Can't open '$infile': $!\n";

      my $outfile = "html/".$import->{'name'}.".html";

      open OUTFD, ">$outfile" or die "Can't open '$outfile': $!\n";

      while (<INFD>)
      {
         s/href="$import->{name}\.css"/href="jmlr.css"/;

         print OUTFD;
      }

      close OUTFD;

      close INFD;

      # Copy css file

      copy("html-src/$importdir/$import->{name}.css",
           "html/jmlr.css")
      or die "Can't copy 'html-src/$importdir/$import->{name}.css'",
             " to 'html/jmlr.css': $!\n";

      # Copy pdf file

      copy("$importbase.$format",
           "html/$import->{name}.$format")
      or die "Can't copy '$importdir/$import->{name}.$format'",
             " to 'html/$import->{name}.$format': $!\n";
   }
}

# do we need to run bibtex on the main document?

if ($main_bibdata)
{
   &bibtex($main_name);
}

if ($online)
{
   &latexonline($main_name);

   # do we need a rerun?

   if (&needs_rerun($main_name))
   {
      &message("Rerun required\n");
      &latexonline($main_name);

      # check again

      if (&needs_rerun($main_name))
      {
         &message("Rerun required\n");
         &latexonline($main_name);
      }
   }
}

if ($print)
{
   &latexprint($main_name);

   # do we need a rerun?

   if (&needs_rerun($main_name))
   {
      &message("Rerun required\n");
      &latexprint($main_name);

      # check again

     if (&needs_rerun($main_name))
     {
        &message("Rerun required\n");
        &latexprint($main_name);
     }
   }
}

if ($html)
{
   # Make the index file

   my $indexfile = &fname('html-src', "index");

   my $preamble = '';

   open OUTFD, ">$indexfile.tex" 
    or die "Can't open '$indexfile.tex': $!\n";

   open INFD, "$main_name.tex"
     or die "Can't open '$main_name.tex': $!\n";

   print OUTFD "\\batchmode", $eol if ($batchtex or $quiet);

   print OUTFD
        "\\makeatletter",$eol,
        "\\def\\input\@path{{../}}$eol",
        "\\makeatother",$eol,
        "\\def\\jmlrgrayscale{0}",$eol;

   while (<INFD>)
   {
      unless
        (s/^([^%]*)\\documentclass\[([^\]]*)\]/$1\\documentclass[$2,html]/)
      {
         s/^([^%]*)\\documentclass/$&\[html\]/;
      }

      s/^([^%]*)\\begin{document}/$&\\HCode{<div id="content">}/;

      if (/^([^%]*)\\maketitle/)
      {
         $preamble .= join('', $1, "\\maketitle", $eol);
         last;
      }

      $preamble .= $_;
   }

   close INFD;

   # Find the book logo
   if ($preamble=~/\\logo\s*{(%\s*\n)?\\includegraphics\s*(\[[^\]]*\])?{([^}]*)}}/m)
   {
      my $texpath = $3;
      my $orgtexpath = $texpath;
      $texpath=~s/\//\\/g if ($dd eq "\\");

      my $ext = '';

      if (-e $texpath)
      {
         copy($texpath, 'html')
           or die "Can't copy '$texpath' to 'html': $!\n";
         copy($texpath, 'html-src')
           or die "Can't copy '$texpath' to 'html-src': $!\n";
      }
      elsif (-e "$texpath.png")
      {
         copy("$texpath.png", 'html')
           or die "Can't copy '$texpath.png' to 'html': $!\n";
         copy("$texpath.png", 'html-src')
           or die "Can't copy '$texpath.png' to 'html-src': $!\n";

         $ext = '.png';
      }
      elsif (-e "$texpath.jpg")
      {
         copy("$texpath.jpg", 'html')
           or die "Can't copy '$texpath.jpg' to 'html': $!\n";
         copy("$texpath.jpg", 'html-src')
           or die "Can't copy '$texpath.jpg' to 'html-src': $!\n";

         $ext = '.jpg';
      }
      elsif (-e "$texpath.gif")
      {
         copy("$texpath.gif", 'html')
           or die "Can't copy '$texpath.gif' to 'html': $!\n";
         copy("$texpath.gif", 'html-src')
           or die "Can't copy '$texpath.gif' to 'html-src': $!\n";

         $ext = '.gif';
      }

      my $img = basename($texpath);
      
      if ($logourl)
      {
         $preamble=~s/\\includegraphics(\[[^\]]*\])?{$orgtexpath}/\\href{$logourl}{\\includegraphics${1}{$img$ext}}/mg;
      }
      else
      {
         $preamble=~s/\\includegraphics(\[[^\]]*\])?{$orgtexpath}/\\includegraphics${1}{$img$ext}/mg;
      }
   }

   print OUTFD $preamble, $eol;

   # Parse TOC

   my $toc = "$main_name.toc";

   open TOC, $toc or die "Can't open '$toc': $!\n";

   my $idx = 0;

   while (<TOC>)
   {
      if (/^\\tocpart\s*{(.*)}\s*$/)
      {
         print OUTFD "\\begin{center}\\bfseries $1\\end{center}$eol";
      }
      elsif (/\\contentsline\s*{papertitle}{(.*)}{[^{}]*}{[^{}]*}\s*$/)
      {
         print OUTFD "\\par\\noindent $1$eol";
      }
      elsif (/\\contentsline\s*{chapterauthor}{(.*)}{[^{}]*}{[^{}]*}\s*$/)
      {
         my $details = $1;
         $details=~s/([^;]*);/\\textbf{\\emph{$1};}/;

         my $label = $imports[$idx]->{'label'};
         my $pages = $pagerefs{$label}->{'start'}.'--'
                   . $pagerefs{$label}->{'end'};

         $details=~s/\\articlepagesref\s*{$label}/$pages/;

         print OUTFD "\\par $details$eol";

         my $name = $imports[$idx]->{'name'};

         print OUTFD "\\par [\\href{$name.html}{abs}] [\\href{$name.pdf}{pdf}]$eol$eol";

         $idx++;
      }
   }

   close TOC;

   print OUTFD "\\HCode{</div>}\\end{document}$eol";

   close OUTFD;

   # Go into html-src directory and run htlatex

   chdir('html-src')
     or die "Can't change directory to 'html': $!\n";

   my $code = system('htlatex index');

   if ($code)
   {
      die "htlatex failed with error code $code\n";
   }

   chdir('..')
     or die "Can't change directory to '..': $!\n";

   # Copy to html directory

   my $inname = &fname('html-src', 'index.html');
   my $outname = &fname('html', 'index.html');

   open INFD, $inname or die "Can't open '$inname': $!\n";

   open OUTFD, ">$outname" or die "Can't open '$outname': $!\n";

   while (<INFD>)
   {
      s/href="index.css"/href="jmlr.css"/;

      print OUTFD;
   }

   close OUTFD;

   close INFD;
}

# Return to original directory
chdir $org_dir;

# Subroutines

sub message{ print @_ unless ($quiet) }

sub latex{
   my $file = shift;

   my $code;

   $code = system(join(' ', $latexapp, $latexopts, "\"$file\""));

   if ($code)
   {
      die "**LaTeX run failed with exit code $code.**\n",
          "Check '$file.log' for details\n";
   }
}

sub latexonline{
   my $file = shift;

   my $code;

   $code = system(join(' ', $latexapp, $latexopts,
     "\"\\def\\jmlrgrayscale{0}\\input{$file}\""));

   if ($code)
   {
      die "**LaTeX run failed with exit code $code.**\n",
          "Check '$file.log' for details\n";
   }

   &message("copying '$file.$format' to '$file-online.$format'\n");

   copy("$file.$format", "$file-online.$format")
     or die "Can't copy '$file.$format' to '$file-online.$format': $!\n";
}

sub latexprint{
   my $file = shift;

   my $code;

   $code = system(join(' ', $latexapp, $latexopts,
     "\"\\def\\jmlrgrayscale{1}\\input{$file}\""));

   if ($code)
   {
      die "**LaTeX run failed with exit code $code.**\n",
          "Check '$file.log' for details\n";
   }

   &message("copying '$file.$format' to '$file-print.$format'\n");

   copy("$file.$format", "$file-print.$format")
     or die "Can't copy '$file.$format' to '$file-print.$format': $!\n";
}

sub bibtex{
   my $file = shift;

   my $code;

   $code = system(join(' ', $bibtexapp, $bibtexopts, "\"$file\""));

   if ($code)
   {
      die "**BibTeX run failed with exit code $code.**\n",
          "Check '$file.blg' for details\n";
   }
}

sub needs_rerun{
  my $file = shift;
  my $rerun = 0;

  # Scan log file for rerun message

  my $log = "$file.log";

  # If there's no log file a run is needed
  open LOGFD, $log or return 1;

  while (<LOGFD>)
  {
     if (/Rerun to get cross-references right\./)
     {
        $rerun = 1;
        last;
     }
  }

  close LOGFD;

  return $rerun;
}

sub fname{ join($dd, @_) }

1;