Import updates to some utilities that were sitting in my personal bin
[utils/utils.git] / retex
diff --git a/retex b/retex
index d603e2e..447aa71 100755 (executable)
--- a/retex
+++ b/retex
@@ -1,27 +1,50 @@
 #!/bin/bash
-# Well-behaved repeating TeX builder -- Matt McCutchen
-# usage: retex <cmd> <input-file-minus-.tex>
+# usage: retex [--bibtex] CMD INPUTNAME (without .tex)
 
 set -e
 
+# Add a bibtex option for the streaming notes.
+# Since the bibtex needs to be part of the fixed-pointing, I unfortunately don't
+# see a better way to handle it than as part of retex.  - Matt 2008-09-02
+if [ "$1" == --bibtex ]; then
+       bibtex=1
+       shift
+fi
+
+if [ $# != 2 ]; then
+       echo >&2 'usage: retex [--bibtex] CMD INPUTNAME (without .tex)'
+       exit 1
+fi
+
 cmd="$1"
 in="$2"
 shift 2
 
+# Just Work if passed `foo.tex'.
+if [ "$in" != "${in%.tex}" ]; then
+       in="${in%.tex}"
+fi
+
 function run {
+       if [ $bibtex ] && [ -r "$in.aux" ]; then
+               echo "[$iter] Running bibtex..."
+               bibtex "$in.aux"
+               # Work around \href commands getting line-broken in places that
+               # break things ~ Matt 2010-12-17
+               sed -i -e '/%$/{N; s/%\n//}; /\\href$/{N; s/\n/ /}' "$in.bbl"
+       fi
        echo "[$iter] Running $2..."
-       yes X | "$cmd" -file-line-error-style "$in"
+       "$cmd" -file-line-error -halt-on-error "$in"
 }
 
 function compare {
        echo "Comparing files..."
-       for f in "$in"*; do
+       for f in "$in."*; do
                # ignore pdfs because they have a nonreproducible "ID" at the end
-               # and logs because they aren't read
-               if ! [ "$f" != "${f%.keep*}" ] && ! [ "$f" != "${f%.pdf}" ] \
-                       && ! [ "$f" != "${f%.log}" ]; then
-                       suf="${f#$in}"
-                       cmp "$in$suf" "$in.keep$suf" || return $?
+               # and logs because they have a less-reproducible time; neither are read
+               if [ "$f" == "${f#$in.retex-keep.}" ] && [ "$f" == "${f%.pdf}" ] && [ "$f" == "${f%.log}" ]; then
+                       suf="${f#$in.}"
+                       cmp "$in.retex-keep.$suf" "$in.$suf" || return $?
                fi
        done
        echo "Reached a fixed point."
@@ -29,28 +52,22 @@ function compare {
 
 function keep {
        echo "Keeping files..."
-       for f in "$in"*; do
-               if ! [ "$f" != "${f%.keep*}" ]; then
-                       suf="${f#$in}"
-                       \cp -p "$in$suf" "$in.keep$suf"
+       for f in "$in."*; do
+               if [ "$f" == "${f#$in.retex-keep.}" ]; then
+                       suf="${f#$in.}"
+                       cp "$in.$suf" "$in.retex-keep.$suf"
                fi
        done
 }
 
 function clean {
        echo "Cleaning up kept files..."
-       rm -f "$in.keep"*
-}
-
-function fail {
-       echo "Compiler exited with code $1."
-       # Remove output files here, a la .DELETE_ON_ERROR?
-       exit $1
+       rm -f "$in.retex-keep."*
 }
 
 iter=0
 keep
-run || fail $?
+run || exit $?
 limit=10
 while ! compare; do
        iter=$(($iter + 1))
@@ -59,7 +76,7 @@ while ! compare; do
                exit 2
        fi
        keep
-       run || fail $?
+       run || exit $?
 done
 clean
 echo "Successful."