web-logs/Makefile: Use only the previous day's logs to avoid overlap.
[utils/utils.git] / retex
CommitLineData
273c3903 1#!/bin/bash
6ac3cd7b 2# usage: retex [--bibtex] CMD INPUTNAME (without .tex)
273c3903
MM
3
4set -e
5
6ac3cd7b
MM
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
9if [ "$1" == --bibtex ]; then
10 bibtex=1
11 shift
12fi
13
14if [ $# != 2 ]; then
15 echo >&2 'usage: retex [--bibtex] CMD INPUTNAME (without .tex)'
16 exit 1
17fi
18
273c3903
MM
19cmd="$1"
20in="$2"
21shift 2
22
6ac3cd7b
MM
23# Just Work if passed `foo.tex'.
24if [ "$in" != "${in%.tex}" ]; then
25 in="${in%.tex}"
26fi
27
273c3903 28function run {
6ac3cd7b
MM
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
273c3903 36 echo "[$iter] Running $2..."
6ac3cd7b 37 "$cmd" -file-line-error -halt-on-error "$in"
273c3903
MM
38}
39
40function compare {
41 echo "Comparing files..."
6ac3cd7b 42 for f in "$in."*; do
273c3903 43 # ignore pdfs because they have a nonreproducible "ID" at the end
6ac3cd7b
MM
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 $?
273c3903
MM
48 fi
49 done
50 echo "Reached a fixed point."
51}
52
53function keep {
54 echo "Keeping files..."
6ac3cd7b
MM
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"
273c3903
MM
59 fi
60 done
61}
62
63function clean {
64 echo "Cleaning up kept files..."
6ac3cd7b 65 rm -f "$in.retex-keep."*
273c3903
MM
66}
67
68iter=0
69keep
6ac3cd7b 70run || exit $?
273c3903
MM
71limit=10
72while ! 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
6ac3cd7b 79 run || exit $?
273c3903
MM
80done
81clean
82echo "Successful."