1#!/bin/sh
2#
3# $Id: latex-mk.in,v 1.40 2010/12/28 07:28:50 dan Exp $
4#
5# Copyright (c) 2002, 2003, 2004, 2005, 2006, 2010 Dan McMahill
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16# 3. All advertising materials mentioning features or use of this software
17#    must display the following acknowledgement:
18#        This product includes software developed by Dan McMahill
19#  4. The name of the author may not be used to endorse or promote products
20#     derived from this software without specific prior written permission.
21#
22#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23#  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24#  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25#  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26#  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27#  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28#  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29#  AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30#  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32#  SUCH DAMAGE.
33#
34
35VERSION=@VERSION@
36
37MAXITERS=${MAXITERS:-5}
38
39BIBTEX=${BIBTEX:-bibtex}
40BIBTEX_FLAGS=${BIBTEX_FLAGS:-}
41LATEX=${LATEX:-latex}
42LATEX_FLAGS=${LATEX_FLAGS:-}
43MAKEIDX=${MAKEIDX:-makeindex}
44MAKEIDX_FLAGS=${MAKEIDX_FLAGS:-}
45MAKEGLS=${MAKEGLS:-makeindex}
46MAKEGLS_FLAGS=${MAKEGLS_FLAGS:-}
47PDFLATEX=${PDFLATEX:-pdflatex}
48PDFLATEX_FLAGS=${PDFLATEX_FLAGS:-}
49TEX2PAGE=${TEX2PAGE:-tex2page}
50TEX2PAGE_FLAGS=${TEX2PAGE_FLAGS:-}
51
52LATEX_MK_LOG=${LOG:-latex-mk.log}
53
54# The environment variable TEXMFOUTPUT is a "special" one.  LaTeX will
55# try to write its output files (including intermediate files) to the
56# current directory.  If the current directory is not writeable, then
57# it will try to use the directory specified by TEXMFOUTPUT.  This
58# is rather important for this script as we monitor certain intermediate
59# files produced by latex and we also need to know where to point programs
60# like BibTeX.  Because of this, we will test right away if we can write
61# to the current directory.
62
63here=${PWD}
64t=latex-mk.test.$$$$
65if touch ${t} 2>/dev/null ; then
66	rm -f ${t}
67	ODIR="."
68else
69	if test -z "${TEXMFOUTPUT}"; then
70		echo "$0:  The current directory: ${here}"
71		echo "is not writeable and TEXMFOUTPUT is not set.  LaTeX can not be run."
72		exit 1
73	else
74		t="${TEXMFOUTPUT}/${t}"
75		if touch ${t} 2>/dev/null ; then
76			rm -f ${t}
77			ODIR="${TEXMFOUTPUT}"
78			if test -z "${TEXINPUTS}" ; then
79				TEXINPUTS="${ODIR}:"
80			else
81				TEXINPUTS="${ODIR}:${TEXINPUTS}:"
82			fi
83			export TEXINPUTS
84			if test -z "${BIBINPUTS}" ; then
85				BIBINPUTS=".:${here}:"
86			else
87				BIBINPUTS=".:${here}:${BIBINPUTS}:"
88			fi
89			export BIBINPUTS
90		else
91			echo "$0:  Neither the current directory: ${here}"
92			echo "     nor the directory specified by TEXMFOUTPUT: ${TEXMFOUTPUT}"
93			echo "     can be written to.  LaTeX can not be run."
94			exit 1
95		fi
96	fi
97fi
98
99echo "${LATEX_MK_LOG}" | grep "^/"
100if test $? -ne 0 ; then
101	LATEX_MK_LOG="${ODIR}/${LATEX_MK_LOG}"
102fi
103
104# list of files to monitor.  If they change, we will rerun
105# LaTeX again.  These are all suffixes of ${NAME}
106#
107# glo = \glossaryentry commands from \glossary
108# gls = glossary file created by (normally) makeindex from the .glo file
109# ilg = glossary log file created by (normally) makeindex
110# ind = index file created by the makeindex program from the .idx file
111# lof = list of figures
112# lot = list of tables
113# toc = table of contents
114monitor_files="glo gls ind lof lot toc"
115clean_files="idx ilg ${monitor_files}"
116
117sep="------------------------------------"
118
119#######################################################################
120#
121# usage()
122#
123
124usage() {
125cat << EOF
126
127$0
128
129This is a wrapper script used by the LaTeX-Mk
130makefile system for LaTeX documents.  Its purpose is to
131run LaTeX the appropriate number of times to resolve all
132references and page changes associated with resolving
133references.  In addition BibTex and makeindex runs are
134performed as needed.
135
136Although the primary purpose of this script is to be a support
137tool for the LaTeX-Mk system, it may be run standalone.
138
139
140OPTIONS
141
142  -b, --bibtex      :  Force a run of bibtex even if there were no
143		       missing citations.
144
145  --clean           :  Clean up various state files and log files used
146		       by $0.
147
148  --debug           :  Enable verbose debugging output.
149
150  -h, --help        :  Display this help message.
151
152  --ignore-errors   :  Ignores any error codes from the various programs
153		       which are run.
154
155  --pdflatex        :  Use PDFLaTeX instead of LaTeX.
156
157  --testlog logfile :  Log the actions taken by this script to the file
158		       "logfile".  This is used as part of the regression
159		       test suite and is primarily for developer use.
160		       The exact format and contents of the log file are
161		       subject to change without notification.
162
163  --tex2page        :  Use TeX2Page instead of LaTeX.
164
165  --version         :  Displays the version and exits.
166
167ENVIRONMENT VARIABLES
168
169The following environment variables, with defaults shown in [],
170affect the behaviour of $0.
171
172  BIBTEX         [${BIBTEX}]
173  BIBTEX_FLAGS   [${BIBTEX_FLAGS}]
174  LATEX          [${LATEX}]
175  LATEX_FLAGS    [${LATEX_FLAGS}]
176  MAKEIDX        [${MAKEIDX}]
177  MAKEIDX_FLAGS  [${MAKEIDX_FLAGS}]
178  MAKEGLS        [${MAKEGLS}]
179  MAKEGLS_FLAGS  [${MAKEGLS_FLAGS}]
180  PDFLATEX       [${PDFLATEX}]
181  PDFLATEX_FLAGS [${PDFLATEX_FLAGS}]
182  TEX2PAGE       [${TEX2PAGE}]
183  TEX2PAGE_FLAGS [${TEX2PAGE_FLAGS}]
184  TEXMFOUTPUT    [${TEXMFOUTPUT}]
185  LATEX_MK_LOG   [${LOG}]
186
187SEE ALSO
188
189  http://latex-mk.sf.net
190
191EOF
192}
193#
194#######################################################################
195
196ignore_errors=no
197debug=no
198do_clean=no
199force_bibtex=no
200test_log=""
201flags=""
202
203while test -n "$1"
204do
205	case "$1"
206	in
207
208	-b|--bibtex)
209		force_bibtex=yes
210		flags="${flags} bibtex"
211		shift
212		;;
213
214	--clean)
215		do_clean=yes
216		flags="${flags} clean"
217		shift
218		;;
219
220	--debug)
221		debug=yes
222		echo "$0:  Enabling debug mode"
223		shift
224		;;
225
226	-h|--help)
227		usage
228		exit 0
229		;;
230
231	--ignore-errors)
232		ignore_errors=yes
233		flags="${flags} ignore_errors"
234		shift
235		;;
236
237	--pdflatex)
238		LATEX=$PDFLATEX
239		LATEX_FLAGS=$PDFLATEX_FLAGS
240		flags="${flags} pdflatex"
241		shift
242		;;
243
244	--testlog)
245		test_log="$2"
246		shift 2
247		;;
248
249	--tex2page)
250		LATEX=$TEX2PAGE
251		LATEX_FLAGS=$TEX2PAGE_FLAGS
252		flags="${flags} tex2page"
253		shift
254		;;
255
256	--version)
257		echo "latex-mk version $VERSION"
258		exit 0
259		;;
260
261	-*)
262		echo "unknown option: $1"
263		exit 1
264		;;
265
266	*)
267		break
268		;;
269
270	esac
271done
272
273#######################################################################
274#
275# log()
276#
277
278first_log=yes
279log() {
280	# Log a message to the test log file if it has been defined.
281	if test -n "$test_log" ; then
282		if test "${first_log}" = "yes" ; then
283			first_log=no
284			rm -f $test_log
285		fi
286		if test ! -f $test_log ; then
287			touch $test_log
288			if test $? -ne 0 ; then
289				echo "$0:  Could not write to test log $test_log" > /dev/stderr
290				exit 1
291			fi
292		fi
293		echo "$*" >> $test_log
294
295	fi
296}
297
298#
299#
300#######################################################################
301
302#######################################################################
303#
304# check_bibunits()
305
306check_bibunits() {
307	msg="Checking if ${NAME} uses the bibunits package..."
308	if grep "bibunits.sty" "$LATEX_MK_LOG" >/dev/null ; then
309	    rslt="yes"
310	    using_bibunits=yes
311	    log "bibunits package detected"
312	else
313	    rslt="yes"
314	    using_bibunits=no
315	fi
316	report "$msg $rslt"
317}
318
319#
320#######################################################################
321
322#######################################################################
323#
324# new_or_changed()
325#
326
327new_or_changed() {
328	f=$1
329	r=1
330	report "Checking for ${f} ..."
331	if [ -f "${f}" ]; then
332		if [ ! -f "${f}.old" ]; then
333			log "\"${f}\" is new"
334			r=0
335		elif cmp -s "${f}.old" "${f}" ; then
336			log "\"${f}\" is not changed"
337		else
338			log "\"${f}\" has changed"
339			r=0
340		fi
341
342		cp -p "${f}" "${f}.old"
343	fi
344
345	return ${r}
346}
347
348#
349#
350#######################################################################
351
352#######################################################################
353#
354# report() -- Like echo but with some decorations added to help
355#             the user know what tool is producing the message
356#
357
358report() {
359cat <<EOF
360LaTeX-Mk: $*
361EOF
362}
363
364#
365#
366#######################################################################
367
368
369INFILE=$1
370
371log "latex-mk invoked with flags = $flags"
372log "BIBTEX         [${BIBTEX}]"
373log "BIBTEX_FLAGS   [${BIBTEX_FLAGS}]"
374log "LATEX          [${LATEX}]"
375log "LATEX_FLAGS    [${LATEX_FLAGS}]"
376log "MAKEIDX        [${MAKEIDX}]"
377log "MAKEIDX_FLAGS  [${MAKEIDX_FLAGS}]"
378log "MAKEGLS        [${MAKEGLS}]"
379log "MAKEGLS_FLAGS  [${MAKEGLS_FLAGS}]"
380log "PDFLATEX       [${PDFLATEX}]"
381log "PDFLATEX_FLAGS [${PDFLATEX_FLAGS}]"
382log "TEX2PAGE       [${TEX2PAGE}]"
383log "TEX2PAGE_FLAGS [${TEX2PAGE_FLAGS}]"
384log "LATEX_MK_LOG   [${LATEX_MK_LOG}]"
385log "ODIR           [${ODIR}]"
386log ""
387log "TEXMFOUTPUT    [${TEXMFOUTPUT}]"
388log "TEXINPUTS      [${TEXINPUTS}]"
389log "BIBINPUTS      [${BIBINPUTS}]"
390
391if test "X$debug" = "Xyes" ; then
392	echo "$0 debug: command line INFILE=$INFILE"
393fi
394
395if test -z "$INFILE" ; then
396	echo "$0:   You must specify an input file"
397	exit 1
398fi
399
400if test ! -f "${INFILE}" ; then
401    INFILE=${INFILE}.tex
402fi
403if test ! -f "${INFILE}" ; then
404	echo "$0:   Neither ${INFILE} nor ${INFILE}.tex exists"
405	exit 1
406fi
407if test "X$debug" = "Xyes" ; then
408	echo "$0 debug: processed INFILE=$INFILE"
409fi
410
411NAME=`basename "${INFILE}" .tex`
412
413if test "X$debug" = "Xyes" ; then
414	echo "$0 debug: NAME=$NAME"
415fi
416
417if [ "X$do_clean" = "Xyes" ]; then
418	report "Cleaning for project: ${NAME}"
419	# clean up any of the .aux, .blg and .bbl files that may have
420	# been created by the bibunits package.
421	log "Cleaning bibunits.sty leftover files"
422	for sfx in aux blg bbl ; do
423		for f in "${ODIR}"/bu[0-9]*.${sfx} "${ODIR}/bu.${sfx}" 	; do
424			if test -f "${f}" ; then
425				report "Removing bibunits package leftover file ${f}"
426				rm -f "${f}"
427			fi
428		done
429	done
430
431	if test -f "$LATEX_MK_LOG" ; then
432		rm -f "$LATEX_MK_LOG"
433	fi
434	# clean up any of the .old versions of files we were
435	# monitoring
436	for sfx in $clean_files ; do
437		f="${ODIR}/${NAME}.${sfx}.old"
438		log "cleaning ${f}"
439		if test -f "${f}" ; then
440			rm -f "${f}"
441		fi
442	done
443
444	exit 0
445fi
446
447GLSFILE=nomencl.ist
448if test -f "$NAME.ist" ; then
449	GLSFILE="$NAME.ist"
450	if test "X$debug" = "Xyes" ; then
451		echo "$0 debug: Using \"$GLSFILE\" as glossary style file"
452	fi
453fi
454
455
456#######################################################################
457#
458# check_bib()
459#
460
461check_bib() {
462	# run latex.  If it fails, manually remove the .dvi file since
463	# latex won't.  Leaving the .dvi file would confuse 'make' because on the next
464	# invocation, the .dvi file would appear to be up to date already.
465	log "check_bib() called"
466	bibtex_ok=no
467	for w in citation bibdata bibstyle ; do
468		msg="Checking for \\$w in \"${ODIR}/${NAME}.aux\" ..."
469		rslt="no"
470		if grep "^\\\\${w}" "${ODIR}/${NAME}.aux" >/dev/null 2>/dev/null ; then
471			rslt="yes"
472			bibtex_ok=yes
473			log "check_bib():  It is ok to run BibTex (found $w)"
474		fi
475		report "$msg $rslt"
476	done
477	rc=0
478	if test $bibtex_ok = no ; then
479		rc=1
480	fi
481	return $rc
482}
483
484#
485#######################################################################
486
487#######################################################################
488#
489# run_latex()
490#
491
492run_latex() {
493	# run latex.  If it fails, manually remove the .dvi file since
494	# latex won't.  Leaving the .dvi file would confuse 'make' because on the next
495	# invocation, the .dvi file would appear to be up to date already.
496	log "run_latex() called"
497	( ( ( "$LATEX" $LATEX_FLAGS "$INFILE" 2>&1 ; echo $? >&4) | tee "$LATEX_MK_LOG" 1>&3) 4>&1 |  (read a; exit $a)) 3>&1
498	rc=$?
499	if test $rc -ne 0 -a "$ignore_errors" != "yes"  ; then
500		echo "$sep"
501		echo "$0:  Error:  LaTeX failed"
502		echo "$sep"
503		rm -f "${ODIR}/${NAME}.dvi" "${ODIR}/${NAME}.pdf"
504		rm -f "$LATEX_MK_LOG"
505		log "run_latex() failed"
506		exit $rc
507	fi
508
509}
510
511#
512#######################################################################
513
514#######################################################################
515#
516# run_bibtex()
517#
518
519run_bibtex() {
520	log "run_bibtex $* called"
521	do_post=yes
522	while test $# -gt 0 ; do
523		case $1 in
524			--skip-post-hook)
525				do_post=no
526				shift
527				;;
528			-*)
529				echo "$0:  INTERNAL ERROR:  run_bibtex $*"
530				exit 1
531				;;
532			*)
533				break
534				;;
535		esac
536	done
537	BIBFILE="${1:-${NAME}}"
538	log "cd \"${ODIR}\" && \"$BIBTEX\" $BIBTEX_FLAGS \"${BIBFILE}\""
539	echo "${sep}"
540	echo "Running BibTeX"
541	echo "cd $\"{ODIR}\" && \"$BIBTEX\" $BIBTEX_FLAGS \"${BIBFILE}\""
542	cd "${ODIR}" && "$BIBTEX" $BIBTEX_FLAGS "${BIBFILE}"
543	rc=$?
544	cd ${here}
545	if test $rc -ne 0 -a "$ignore_errors" != "yes" ; then
546		echo "$sep"
547		echo "$0:  Error:  BibTeX failed"
548		echo "$sep"
549		rm -f "${ODIR}/${NAME}.dvi" "${ODIR}/${NAME}.pdf"
550		rm -f "$LATEX_MK_LOG"
551		log "run_bibtex($*) failed"
552		exit $rc
553	fi
554
555	# run the post-bibtex hook if its been defined
556	if test "$do_post" = "yes" -a "X$POST_BIBTEX_HOOK" != "X" ; then
557	    if test -x $POST_BIBTEX_HOOK ; then
558		$POST_BIBTEX_HOOK "$BIBFILE"
559		rc=$?
560		if test $rc -ne 0 ; then
561		    echo "$POST_BIBTEX_HOOK "$BIBFILE" failed"
562		    rm -f "${ODIR}/${NAME}.dvi" "${ODIR}/${NAME}.pdf"
563		    rm -f "$LATEX_MK_LOG"
564		    exit $rc
565		fi
566	    fi
567	fi
568	echo "${sep}"
569}
570
571#######################################################################
572#
573# run_makeindex()
574#
575
576run_makeindex() {
577	log "run_makeindex() called"
578	log "cd \"${ODIR}\" && \"$MAKEIDX\" $MAKEIDX_FLAGS \"$NAME\""
579	echo "${sep}"
580	echo "Running makeindex"
581	echo "cd \"${ODIR}\" && \"$MAKEIDX\" $MAKEIDX_FLAGS \"$NAME\""
582	cd "${ODIR}" && "$MAKEIDX" $MAKEIDX_FLAGS "$NAME"
583	rc=$?
584	cd ${here}
585	if test $rc -ne 0 -a "$ignore_errors" != "yes" ; then
586		echo "$sep"
587		echo "$0:  Error:  makeindex failed"
588		echo "$sep"
589		rm -f "${ODIR}/${NAME}.dvi" "${ODIR}/${NAME}.pdf"
590		rm -f "$LATEX_MK_LOG"
591		log "run_makeindex() failed"
592		exit $rc
593	fi
594	echo "${sep}"
595
596}
597
598#######################################################################
599#
600# run_makeglossary()
601#
602
603run_makeglossary() {
604	log "run_makeglossary() called"
605	log "cd \"${ODIR}\" && \"$MAKEGLS\" $MAKEGLS_FLAGS -s \"$GLSFILE\" \"$NAME.glo\" -o \"$NAME.gls\""
606	echo "${sep}"
607	echo "Running `basename $MAKEGLS`"
608	echo "cd \"${ODIR}\" && \"$MAKEGLS\" $MAKEGLS_FLAGS -s \"$GLSFILE\" -o \"$NAME.gls\" \"$NAME.glo\""
609	cd "${ODIR}" && "$MAKEGLS" $MAKEGLS_FLAGS -s "$GLSFILE" -o "$NAME.gls" "$NAME.glo"
610	rc=$?
611	cd ${here}
612	if test $rc -ne 0 -a "$ignore_errors" != "yes" ; then
613		echo "$sep"
614		echo "$0:  Error:  `basename $MAKEGLS` failed"
615		echo "$sep"
616		rm -f "${ODIR}/${NAME}.dvi" "${ODIR}/${NAME}.pdf"
617		rm -f "$LATEX_MK_LOG"
618		log "run_makeglossary() failed"
619		exit $rc
620	fi
621	echo "${sep}"
622
623}
624
625#
626#######################################################################
627
628if [ "X$force_bibtex" = "Xyes" ] ; then
629	log "Running with force_bibtex"
630	run_latex
631	run_bibtex
632fi
633
634
635cnt=0
636bibcnt=0
637while test $cnt -lt $MAXITERS ; do
638	log "${sep}"
639	log "Pass #${cnt}"
640	run_latex
641
642	# if the document uses the bibunits package, then we will end up
643	# with a bunch of .bib files and a bunch of .aux files and we will
644	# need to run bibtex
645	check_bibunits
646
647	# if we need to run bibtex, we should end up with something like this:
648	#LaTeX Warning: Citation `foo' on page 1 undefined on input line 2
649	# and
650	#LaTeX Warning: There were undefined references.
651	#
652	# After bibtex is run, we will get one more latex run that has a citation
653	# complaint so we allow one more complaint from latex before declaring
654	# that there is a problem with the citations.  Just in case someone
655	# wants to keep running latex and viewing/printing the results with these
656	# errors present, provide a way to override this.
657	#
658	# Evidently, we may also simply get the following complaint:
659	#
660	# No file foo.bbl
661	#
662	# instead of the previous warning.  This can happen if you only have
663	# something like \nocite{*} in your document instead of \cite{x}
664	#
665	if test "$using_bibunits" = "yes" -a $cnt -eq 0 ; then
666		for f in "${ODIR}"/*.aux ; do
667			case $f in
668				# FIXME -- the user can potentially change
669				# the file names used here.  Is there a more robust
670				# way?
671				${ODIR}/bu[0-9]*.aux)
672					fb=`basename $f .aux`
673					log "Running bibtex on $f"
674					run_bibtex --skip-post-hook "${fb}"
675					;;
676			esac
677		done
678	fi
679
680	needbib=`grep "Citation.*undefined" "$LATEX_MK_LOG"`
681	if test -n "$needbib" ; then log "Citation undefined" ; fi
682	if test -z "$needbib" ; then
683	    report "Checking for missing .bbl files in latex log ..."
684	    needbib=`grep "No file.*\.bbl" "$LATEX_MK_LOG"`
685	    if test -n "$needbib" ; then log "Missing .bbl" ; fi
686	fi
687	if test -n "$needbib" ; then
688		if test $bibcnt -eq 0 ; then
689			# This is a bit of a dodgy thing here.  If we are using
690			# the bibunits package, we will probably end up here
691			# but we actually don't need to run bibtex on the main
692			# document.  There is also the possiblilty that we still
693			# need to run bibtex.  It all depends on if we have a
694			# non-bibunits bibliography too or not
695			if check_bib ; then
696				echo "$sep"
697				echo "$0:  Running bibtex to resolve citations"
698				echo "$sep"
699				log "BibTex run #1"
700				run_bibtex
701			else
702				log "Skip BibTex run #1 (due to check_bib)"
703			fi
704		elif test $bibcnt -eq 1 ; then
705			echo "$sep"
706			echo "$0:  BibTeX has already been run, but LaTeX is still reporting"
707			echo "    $needbib."
708			echo "    Trying LaTeX one more time...."
709			echo "$sep"
710			log "Try another latex run for the citations"
711		elif test -n "$IGNORE_CITATION_WARNS" ; then
712			echo "$sep"
713			echo "$0:  WARNING:  BibTeX has already been run, but LaTeX is still reporting"
714			echo "$0:  $needbib"
715			echo "$0:  You have set the IGNORE_CITATION_WARNS variable to override this, but"
716			echo "$0:  you probably do have an error in your citations."
717			echo "$sep"
718			log "Could not resolve citations but you are ignoring this"
719		else
720			echo "$sep"
721			echo "$0:  Error:  BibTeX has already been run, but LaTeX is still reporting"
722			echo "$0:  $needbib"
723			echo "$sep"
724			rm -f "$LATEX_MK_LOG"
725			log "Exit with undefined citations"
726			if test "$ignore_errors" = "yes" ; then exit 0 ; else exit 1 ; fi
727		fi
728		bibcnt=`expr $bibcnt + 1`
729	fi
730
731	# Check if we need to run makeindex.  What will happen is the first LaTeX run
732	# generates a .idx file.  Then we run makeindex to operate on the .idx file
733	# and generate a .ind file.  Since we monitor the .ind file all we must do here
734	# is see if a makeindex run is needed.
735	if new_or_changed "${ODIR}/${NAME}.idx" ; then
736		log "\"${ODIR}/${NAME}.idx\" new or changed -> need to run makeindex"
737		run_makeindex
738	fi
739
740	# we shouldn't need this, but lets be sure
741	if test -f "${ODIR}/${NAME}.idx" -a ! -f "${ODIR}/${NAME}.ind" ; then
742		log "\"${ODIR}/${NAME}.idx\" exists but not \"${ODIR}/${NAME}.ind\" -> run makeindex"
743		run_makeindex
744	fi
745
746	if test -f "${ODIR}/${NAME}.glo" && new_or_changed "${ODIR}/${NAME}.glo" ; then
747		log "\"${ODIR}/${NAME}.glo\" new or changed -> need to update glossary"
748		run_makeglossary
749	fi
750
751	# Examine the list of figures, list of tables, table of contents, etc
752	# files because when these change, we need to re-run latex, but
753	# sometimes latex does not tell us this.
754	changed_file=""
755	for sfx in $monitor_files ; do
756		if new_or_changed "${ODIR}/${NAME}.${sfx}" ; then
757			changed_file="${changed_file} \"${ODIR}/${NAME}.${sfx}\""
758			echo "\"${ODIR}/${NAME}.${sfx}\" is new or changed.  LaTeX needs to be re-run"
759		fi
760	done
761
762	# with latex, you might see:
763	#LaTeX Warning: Label(s) may have changed. Rerun to get cross-references right
764	#
765	# with tex2page, you may get:
766	#Rerun: tex2page week5.tex
767	#
768	rerun=`grep "Rerun" "$LATEX_MK_LOG"`
769	log "rerun        = ${rerun}"
770	log "needbib      = ${needbib}"
771	log "changed_file = ${changed_file}"
772	if test -z "$rerun" -a -z "$needbib" -a -z "$changed_file" ; then
773		log "No rerun is needed"
774		break;
775	fi
776	log "Rerun is needed"
777	echo "$sep"
778	echo "$0:   LaTeX references have changed, another latex run is needed"
779	echo "$sep"
780	cnt=`expr $cnt + 1`
781	if test "X$debug" = "Xyes" ; then echo "$0 debug: incremented cnt=$cnt" ; fi
782done
783
784#LaTeX Warning: There were undefined references.
785undefined=`grep "There were undefined references" "$LATEX_MK_LOG"`
786if test -n "$undefined" ; then
787	log "Finished but still have undefined references."
788	echo "$sep"
789	echo "$0:  There were undefined references.  Please correct this."
790	echo "$0:  I have already made $cnt passes."
791	#LaTeX Warning: Reference `tab:fuse_table' on page 3 undefined on input line 82.
792	grep "Reference.*on page.*undefined" "$LATEX_MK_LOG"
793	echo "$sep"
794fi
795
796#LaTeX Warning: There were multiply-defined labels.
797mult=`grep "multiply-defined labels" "$LATEX_MK_LOG"`
798if test -n "$mult" ; then
799	log "Finished but still have multiply defined labels."
800	echo "$sep"
801	echo "$0:  There were multiply defined labels."
802	echo "$0:  You may wish to correct this"
803	#LaTeX Warning: Label `fig:myfig' multiply defined.
804	grep "multiply defined" "$LATEX_MK_LOG"
805	echo "$sep"
806fi
807
808if test $cnt -eq $MAXITERS ; then
809	log "Reached iteration limit."
810	echo "$sep"
811	echo "$0:   Failed to get LaTeX to converge after $MAXITERS tries"
812	echo "$0:   Please fix the document or try again if you think it"
813	echo "$0:   should be ok"
814	echo "$sep"
815	rm -f "$LATEX_MK_LOG"
816	if test "$ignore_errors" = "yes" ; then exit 0 ; else exit 1 ; fi
817fi
818
819log "Remove log and exit."
820rm -f "$LATEX_MK_LOG"
821exit 0
822
823