| 1 | #!/bin/bash |
| 2 | # usage: retex [--bibtex] CMD INPUTNAME (without .tex) |
| 3 | |
| 4 | set -e |
| 5 | |
| 6 | # Add a bibtex option for the streaming notes. |
| 7 | # Since the bibtex needs to be part of the fixed-pointing, I unfortunately don't |
| 8 | # see a better way to handle it than as part of retex. - Matt 2008-09-02 |
| 9 | if [ "$1" == --bibtex ]; then |
| 10 | bibtex=1 |
| 11 | shift |
| 12 | fi |
| 13 | |
| 14 | if [ $# != 2 ]; then |
| 15 | echo >&2 'usage: retex [--bibtex] CMD INPUTNAME (without .tex)' |
| 16 | exit 1 |
| 17 | fi |
| 18 | |
| 19 | cmd="$1" |
| 20 | in="$2" |
| 21 | shift 2 |
| 22 | |
| 23 | # Just Work if passed `foo.tex'. |
| 24 | if [ "$in" != "${in%.tex}" ]; then |
| 25 | in="${in%.tex}" |
| 26 | fi |
| 27 | |
| 28 | function run { |
| 29 | if [ $bibtex ] && [ -r "$in.aux" ]; then |
| 30 | echo "[$iter] Running bibtex..." |
| 31 | bibtex "$in.aux" |
| 32 | # Work around \href commands getting line-broken in places that |
| 33 | # break things ~ Matt 2010-12-17 |
| 34 | sed -i -e '/%$/{N; s/%\n//}; /\\href$/{N; s/\n/ /}' "$in.bbl" |
| 35 | fi |
| 36 | echo "[$iter] Running $2..." |
| 37 | "$cmd" -file-line-error -halt-on-error "$in" |
| 38 | } |
| 39 | |
| 40 | function compare { |
| 41 | echo "Comparing files..." |
| 42 | for f in "$in."*; do |
| 43 | # ignore pdfs because they have a nonreproducible "ID" at the end |
| 44 | # and logs because they have a less-reproducible time; neither are read |
| 45 | if [ "$f" == "${f#$in.retex-keep.}" ] && [ "$f" == "${f%.pdf}" ] && [ "$f" == "${f%.log}" ]; then |
| 46 | suf="${f#$in.}" |
| 47 | cmp "$in.retex-keep.$suf" "$in.$suf" || return $? |
| 48 | fi |
| 49 | done |
| 50 | echo "Reached a fixed point." |
| 51 | } |
| 52 | |
| 53 | function keep { |
| 54 | echo "Keeping files..." |
| 55 | for f in "$in."*; do |
| 56 | if [ "$f" == "${f#$in.retex-keep.}" ]; then |
| 57 | suf="${f#$in.}" |
| 58 | cp "$in.$suf" "$in.retex-keep.$suf" |
| 59 | fi |
| 60 | done |
| 61 | } |
| 62 | |
| 63 | function clean { |
| 64 | echo "Cleaning up kept files..." |
| 65 | rm -f "$in.retex-keep."* |
| 66 | } |
| 67 | |
| 68 | iter=0 |
| 69 | keep |
| 70 | run || exit $? |
| 71 | limit=10 |
| 72 | while ! compare; do |
| 73 | iter=$(($iter + 1)) |
| 74 | if [ $iter -ge $limit ]; then |
| 75 | echo "Did not reach a fixed point in $limit tries." |
| 76 | exit 2 |
| 77 | fi |
| 78 | keep |
| 79 | run || exit $? |
| 80 | done |
| 81 | clean |
| 82 | echo "Successful." |