--- /dev/null
+#!/bin/bash
+# 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..."
+ "$cmd" -file-line-error -halt-on-error "$in"
+}
+
+function compare {
+ echo "Comparing files..."
+ for f in "$in."*; do
+ # ignore pdfs because they have a nonreproducible "ID" at the end
+ # 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."
+}
+
+function keep {
+ echo "Keeping files..."
+ 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.retex-keep."*
+}
+
+iter=0
+keep
+run || exit $?
+limit=10
+while ! compare; do
+ iter=$(($iter + 1))
+ if [ $iter -ge $limit ]; then
+ echo "Did not reach a fixed point in $limit tries."
+ exit 2
+ fi
+ keep
+ run || exit $?
+done
+clean
+echo "Successful."