1##########################################################################
2# Copyright (c) 2010, ETH Zurich.
3# All rights reserved.
4#
5# This file is distributed under the terms in the attached LICENSE file.
6# If you do not find this file, copies can be found by writing to:
7# ETH Zurich D-INFK, Haldeneggsteig 4, CH-8092 Zurich. Attn: Systems Group.
8##########################################################################
9#
10# run-pdflatex.sh: helper script to build Barrelfish technical notes
11#
12# This script wraps up pdflatex into something that can be entirely
13# controlled by command-line options, making it considerably easier to
14# invoke from a Makefile or Hakefile.
15#
16# In some ways this is a poor relation to rubber, but it doesn't
17# require any support packages (not even Python), and is specifically
18# tailored to the kinds of filesystem naming games that Hake likes to
19# play.  That said, it's completely independent of Hake, and usable
20# standalone.
21
22#
23# Usage summary
24#
25usage () {
26    cat >&1 <<EOF
27Usage: $0 <options>
28	--input-tex <filename>
29	--working-dir <dir>
30	--output-pdf <filename>
31	(--texinput <dir>)*
32	(--bininput <dir>)*
33	[--has-bib]
34EOF
35    exit 1
36}
37
38#
39# Shorthand for running pdflatex.  This is nicer than using a single
40# shell variable as it always gets the quoting right.
41#
42run_latex () {
43#    echo "dirs: $WORKING_DIR, $JOB_NAME, $INPUT_TEX, $TEXINPUTS, $BIBINPUTS" \
44    pdflatex \
45	-interaction=nonstopmode \
46	-file-line-error \
47	-output-directory "$WORKING_DIR" \
48	-jobname "$JOB_NAME" \
49	"$INPUT_TEX" #|| exit
50}
51
52#
53# Work out how to construct TEXINPUTS and BIBINPUTS paths, according
54# to Cygwin or not.
55#
56case "`which pdflatex`" in
57    /cygdrive/*) _ISCYGWIN=Yes ;;
58esac
59
60cons_inputs() {
61    path=$(readlink -f ${2})
62    if [ -z "$_ISCYGWIN" ] ; then
63	echo "${1}${path}//:"
64    else
65	echo "${1}${path}//\\;"
66    fi
67}
68
69#
70# Initial values for input arguments
71#
72if [ -z "$TEXINPUTS" ]; then
73    TEXINPUTS=`cons_inputs "" "."`
74else
75    TEXINPUTS=`cons_inputs "$TEXINPUTS" "."`
76fi
77
78# echo "TEXinputs is $TEXINPUTS"
79# exit
80
81BIBINPUTS=`cons_inputs "" "."`
82INPUT_TEX=
83WORKING_DIR=
84OUTPUT_PDF=
85HAS_BIB=
86HAS_GLO=
87
88#
89# Argument processing.
90#
91while [ -n "$*" ]; do
92    case $1 in
93	--input-tex)
94	    shift; INPUT_TEX=$1 ;;
95	--output-pdf)
96	    shift; OUTPUT_PDF="$1" ;;
97	--working-dir)
98	    shift; WORKING_DIR="$1" ;;
99	--texinput)
100	    shift; TEXINPUTS=`cons_inputs "$TEXINPUTS" "$1"` ;;
101	--bibinput)
102	    shift; BIBINPUTS=`cons_inputs "$BIBINPUTS" "$1"` ;;
103	--has-bib)
104	    HAS_BIB=Yes ;;
105	--has-glo)
106	    HAS_GLO=Yes ;;
107	*)
108	    usage ;;
109    esac
110    shift
111done
112if [ -z "$INPUT_TEX" ] ; then usage ; fi
113if [ -z "$WORKING_DIR" ] ; then usage ; fi
114if [ -z "$OUTPUT_PDF" ] ; then usage ; fi
115
116#
117# Calculate all the other stuff we need.
118#
119INPUT_DIR=`dirname "$INPUT_TEX"`
120INPUT_DIR=`readlink -f $INPUT_DIR`
121INPUT_BASE=`basename "$INPUT_TEX" .tex`
122TEXINPUTS=`cons_inputs "$TEXINPUTS" "$INPUT_DIR"`
123BIBINPUTS=`cons_inputs "$BIBINPUTS" "$INPUT_DIR"`
124export TEXINPUTS
125export BIBINPUTS
126
127#
128# We use a different jobname due to parallel Make paranoia: it's a
129# good idea if the output file that we really care out only appears as
130# the last step of the build.  This also allows us to specify
131# different directories for the final PDF file and the intermediate
132# working files, something (apparently) not possible within pdflatex.
133#
134JOB_NAME="${INPUT_BASE}.tmp"
135# the man page for bibtex specifically reads
136# "the filename on the command line must be given without the .aux extension"
137AUX_FILE_WITHOUT_AUX="$WORKING_DIR/${JOB_NAME}"
138AUX_FILE="$WORKING_DIR/${JOB_NAME}.aux"
139OUT_FILE="$WORKING_DIR/${JOB_NAME}.out"
140HST_FILE="$WORKING_DIR/${JOB_NAME}.hst"
141LOG_FILE="$WORKING_DIR/${JOB_NAME}.log"
142TOC_FILE="$WORKING_DIR/${JOB_NAME}.toc"
143BBL_FILE="$WORKING_DIR/${JOB_NAME}.bbl"
144BLG_FILE="$WORKING_DIR/${JOB_NAME}.blg"
145VER_FILE="$WORKING_DIR/${JOB_NAME}.ver"
146GLO_FILE="$WORKING_DIR/${JOB_NAME}.glo"
147ACN_FILE="$WORKING_DIR/${JOB_NAME}.acn"
148IST_FILE="$WORKING_DIR/${JOB_NAME}.ist"
149PDF_FILE="$WORKING_DIR/${JOB_NAME}.pdf"
150LOT_FILE="$WORKING_DIR/${JOB_NAME}.lot"
151LOF_FILE="$WORKING_DIR/${JOB_NAME}.lof"
152GLS_FILE="$WORKING_DIR/${JOB_NAME}.gls"
153GDE_FILE="$WORKING_DIR/${JOB_NAME}.glsdefs"
154GLG_FILE="$WORKING_DIR/${JOB_NAME}.glg"
155ACR_FILE="$WORKING_DIR/${JOB_NAME}.acr"
156ALG_FILE="$WORKING_DIR/${JOB_NAME}.alg"
157
158# remove ubuntu chroot from environment to make pdflatex work
159# -SG, 2014-10-14
160export LD_LIBRARY_PATH=
161
162#
163# And, finally, do what the old Makefile used to do.
164#
165# bibtex on cygwin can be miktex which always 0 exit code
166run_latex
167if [ -n "$HAS_BIB" ]; then (cd $WORKING_DIR && bibtex $JOB_NAME) && test -r $BBL_FILE || exit; echo run_latex; fi
168if [ -n "$HAS_GLO" ]; then (makeglossaries -s $IST_FILE $GLO_FILE && makeglossaries -s $IST_FILE $ACN_FILE && test -r $GLO_FILE)  || exit; echo run_latex; fi
169if [ -e "$TOC_FILE" -o -e "$BBL_FILE" -o -e "$VER_FILE" -o -e "$GLO_FILE" -o -e "$ACN_FILE" ]; then run_latex; fi
170while egrep -e '^LaTeX Warning.*Rerun|^\(.*\).*Rerun' "$LOG_FILE"; do run_latex; done
171rm -f "$AUX_FILE" "$HST_FILE" "$LOG_FILE" "$TOC_FILE" "$BBL_FILE"
172rm -f "$BLG_FILE" "$VER_FILE" "$OUT_FILE" "$GDE_FILE"
173rm -f "$GLO_FILE" "$ACN_FILE" "$IST_FILE"
174rm -f "$LOT_FILE" "$LOF_FILE" "$GLS_FILE" 
175rm -f "$GLG_FILE" "$ACR_FILE" "$ALG_FILE"
176mv "$PDF_FILE" "$OUTPUT_PDF"
177echo "Output file is in $OUTPUT_PDF"
178