| 1 | #!/bin/bash |
| 2 | # Well-behaved repeating TeX builder -- Matt McCutchen |
| 3 | # usage: retex <cmd> <input-file-minus-.tex> |
| 4 | |
| 5 | set -e |
| 6 | |
| 7 | cmd="$1" |
| 8 | in="$2" |
| 9 | shift 2 |
| 10 | |
| 11 | function run { |
| 12 | echo "[$iter] Running $2..." |
| 13 | yes X | "$cmd" -file-line-error-style "$in" |
| 14 | } |
| 15 | |
| 16 | function compare { |
| 17 | echo "Comparing files..." |
| 18 | for f in "$in"*; do |
| 19 | # ignore pdfs because they have a nonreproducible "ID" at the end |
| 20 | # and logs because they aren't read |
| 21 | if ! [ "$f" != "${f%.keep*}" ] && ! [ "$f" != "${f%.pdf}" ] \ |
| 22 | && ! [ "$f" != "${f%.log}" ]; then |
| 23 | suf="${f#$in}" |
| 24 | cmp "$in$suf" "$in.keep$suf" || return $? |
| 25 | fi |
| 26 | done |
| 27 | echo "Reached a fixed point." |
| 28 | } |
| 29 | |
| 30 | function keep { |
| 31 | echo "Keeping files..." |
| 32 | for f in "$in"*; do |
| 33 | if ! [ "$f" != "${f%.keep*}" ]; then |
| 34 | suf="${f#$in}" |
| 35 | \cp -p "$in$suf" "$in.keep$suf" |
| 36 | fi |
| 37 | done |
| 38 | } |
| 39 | |
| 40 | function clean { |
| 41 | echo "Cleaning up kept files..." |
| 42 | rm -f "$in.keep"* |
| 43 | } |
| 44 | |
| 45 | function fail { |
| 46 | echo "Compiler exited with code $1." |
| 47 | # Remove output files here, a la .DELETE_ON_ERROR? |
| 48 | exit $1 |
| 49 | } |
| 50 | |
| 51 | iter=0 |
| 52 | keep |
| 53 | run || fail $? |
| 54 | limit=10 |
| 55 | while ! compare; do |
| 56 | iter=$(($iter + 1)) |
| 57 | if [ $iter -ge $limit ]; then |
| 58 | echo "Did not reach a fixed point in $limit tries." |
| 59 | exit 2 |
| 60 | fi |
| 61 | keep |
| 62 | run || fail $? |
| 63 | done |
| 64 | clean |
| 65 | echo "Successful." |