#! /bin/csh -f
# File: latexn

# This is a csh script for UNIX systems.  It runs Latex enough times to 
# resolve cross references, and to get the table of contents (if any) correct.
# It also runs makeindex if needed.  See the help text below for more details.

# INSTALLATION: Make this an executable file: chmod a+x latexn
# and put it in an appropriate directory for executable files.
# If your system has non-standard commands to run bibtex, latex and makeindex, 
# then change the appropriate lines.

# CUSTOMIZATION
# Similarly, if you have personal preferences as to how these programs should
# be used, change the appropriate lines.  For example, Andrei Klot points out
# that if you would like latex to stop automatically after the first error it
# finds, you can replace the line
#
#       latex $thisfile
# by
#       echo X | latex $thisfile

# FILES GENERATED: In addition to the standard LaTeX output files, 
# back-up copies of the previous .aux and .idx files are generated. These
# have names like file.aux.bak and file.idx.bak.  They are rm'd at the end.


# J.C. Collins.  collins@phys.psu.edu
# 29 Oct 97 JCC.  Try again with BiBTeX

# A.S. Beach.  asb4@psu.edu
# 23 Oct 97 ASB.  Attempt at integrating BiBTeX.

# J.C. Collins.  collins@phys.psu.edu
# 21 Mar 97 JCC.  Clean-up
# 17 Mar 97 JCC.  Try latex command line to be echo X | latex ...
#                 (Suggestion from klot@priam.serma.cea.fr, so that
#                 LaTeX just stops on an error, without user intervention.)
#                 But leave that as suggestion for customization.
# 20 Jan 97 JCC.  Clean up from previous versions.  

@ maxcount = 5

if ( "$1" == "" ) then
# No command line arguments => help message.
cat << EOT
 Usage 

     latexn file
 
 LaTeXs file.tex.  If there are no errors then LaTeX is repeated as necessary
 to fix cross references, and to ensure that the table of contents and the 
 index (if any) are correct.  Makeindex and/or bibtex are run if necessary.
 If the file has an explicit extension, then LaTeX is applied to that file
 rather to file.tex.
 The maximum number of attempts at LaTeX is $maxcount.  If LaTeX gives an
 error (as indicated by its exit code), then no repeated runs of LaTeX are
 made.  
EOT
exit
endif


# Variables
# thisfile = source file.
# thisbase= base of filename.
# thisext = extension of filename.
# resulttex = exitcode

set thisfile=$1
set thisbase=$thisfile:r
set thisext=$thisfile:e
set resulttex=0

if ( "$thisext" == "" ) then
   # No extension supplied.  
   set thisfile=$thisbase.tex
endif

if ( ! -e $thisfile ) then
   echo File $thisfile does not exist.
   exit
endif

@ count = 0
# We'll repeat LaTeX until the variable dorepeat is undefined.  
# We'll use LaTeX at least once.
set dorepeat
while ( $?dorepeat ) 
   if ( -e $thisbase.idx) then
      echo Making backup of old .idx file: $thisbase.idx.bak.  Then makeindex...
      cp -p $thisbase.idx $thisbase.idx.bak
      makeindex $thisbase.idx
      echo ' '
   endif

   if ( -e $thisbase.aux) then
      echo Making backup of old .aux file: $thisbase.aux.bak
      cp -p $thisbase.aux $thisbase.aux.bak

      grep \\\\bibdata $thisbase.aux > /dev/null
      if ( ( $status == 0 ) && ( $count == 0 ) )  then 
          echo Need bibtex run before first pass...
          bibtex $thisbase
          echo ' '
      endif
   else if ( ! -e $thisbase.aux.bak) then
      # Make dummy file.  This will save a pass if there are no
      # cross-references generated
      echo "\relax " >! $thisbase.aux.bak
   endif

   if ( -e $thisbase.bbl) then
      echo Making backup of old .bbl file: $thisbase.bbl.bak
      cp -p $thisbase.bbl $thisbase.bbl.bak
   endif




   #  Normal latex:
   latex $thisfile
   #  Commented out alternative to standard latex command to make
   #  latex stop after it encounters an error.
   #  echo X | latex $thisfile

   set resulttex=$status
   @ count = $count + 1
   echo Exit code $resulttex.  Number of iterations $count.
   # status = 0 => no error or mild error (overfull and underfull etc)

      
   # Now run bibtex, if needed, i.e., 
   # an .aux file exists, it contains the string \bibdata, and
   # this is the first pass.
   # The information that bibtex reads from the .aux file is
   # pass-independent. If we find (below) that the bbl file is unchanged,
   # then the last latex saw a correct bibliography.
   if ( -e $thisbase.aux ) then
       # I think I have put the correct number of \'s:
       grep \\\\bibdata $thisbase.aux
       if ( ( $status == 0 ) && ( $count == 1 ) )  then 
           bibtex $thisbase
           echo ' '
       endif
   endif

   if ( $count >= $maxcount ) then
       unset dorepeat
       echo "I've tried $count times.  That's enough."
   else if ( $resulttex >= 1 ) then
      echo "Bad error.  Therefore I will not run latex a second time"
      echo Return code $resulttex
      unset dorepeat
   else
      # Default to not needing to do anything more:
      unset dorepeat
      # Now find any conditions that require a rerun of LaTeX:

      # new .aux file
      if ( ! -e $thisbase.aux ) then
         echo latex produced no .aux file.
         if ( -e $thisbase.aux.bak ) then
            echo The .aux.bak file is of no use.  I will delete it.
            rm  $thisbase.aux.bak
         endif
      else if ( -e $thisbase.aux.bak ) then
         # "Has the .aux file changed?"
         diff $thisbase.aux $thisbase.aux.bak > /dev/null
         if ( $status == 0) then
            echo The .aux file has not changed.
         else
            echo The .aux file has changed.  I must run latex again.
            set dorepeat
         endif
      else
         echo I have a .aux file, but there was no previous one, so I must run latex again.
         set dorepeat
      endif

      # new .idx file
      if ( ! -e $thisbase.idx ) then
         if ( -e $thisbase.idx.bak ) then
            echo latex produced no .idx file.
            echo The .idx.bak file is of no use.  I will delete it.
            rm  $thisbase.idx.bak
         endif
      else if ( -e $thisbase.idx.bak ) then
         # Has the .idx file changed?
         diff $thisbase.idx $thisbase.idx.bak > /dev/null
         if ( $status == 0) then
            echo The .idx file has not changed.
         else
            echo The .idx file has changed.  I must run latex again.
            set dorepeat
         endif
      else
         echo I have a .idx file, but there was no previous one, so I must run latex again.
         set dorepeat
      endif

      # new .bbl file
      if ( ! -e $thisbase.bbl ) then
         if ( -e $thisbase.bbl.bak ) then
            echo latex produced no .bbl file.
            echo The .bbl.bak file is of no use.  I will delete it.
            rm  $thisbase.bbl.bak
         endif
      else if ( -e $thisbase.bbl.bak ) then
         # Has the .bbl file changed?
         diff $thisbase.bbl $thisbase.bbl.bak > /dev/null
         if ( $status == 0) then
            echo The .bbl file has not changed.
         else
            echo The .bbl file has changed.  I must run latex again.
            set dorepeat
         endif
      else
         echo I have a .bbl file, but there was no previous one, so I must run latex again.
         set dorepeat
      endif
   endif
   echo ===============================================
end

# now delete the backup files.
if ( -e $thisbase.aux.bak ) rm $thisbase.aux.bak
if ( -e $thisbase.bbl.bak ) rm $thisbase.bbl.bak
if ( -e $thisbase.idx.bak ) rm $thisbase.idx.bak

exit $resulttex