/
add_tex_root.sh
executable file
·82 lines (74 loc) · 1.95 KB
/
add_tex_root.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/bin/bash
#
# (C) ExpertAnalytics 2016
# Åsmund Ødegård <asmund@xal.no>
#
# This is a script in the public domain - use however you like.
#
cleanup=no
set -- `getopt c $*`
for i ; do
case "$i"
in
-c) cleanup=yes; shift;;
--) shift;;
esac
done
if [[ $# -lt 1 ]] ; then
echo "Please, specify the master tex-document as argument"
exit 1
fi
# set master-file, normalized
master=$1
[[ $master =~ ^\./.* ]] || master="./$master"
if [[ ! -f $master ]] ; then
echo "Unable to find master tex-document $master"
exit 2
fi
if [[ ${master: -4} != ".tex" ]] ; then
echo "Specified master $master doesn't look like a TeX source"
exit 3
fi
rootline="%!TEX root=$master"
texdocs=$(find . -name '*.tex')
for file in $texdocs ; do
if [[ $file != $master ]] ; then
echo "Edit $file"
firstline=$(head -1 $file)
# Trim leading/trailing whitespace
firstline="${firstline##*( )}"
firstline="${firstline%%*( )}"
#echo $firstline
if [[ $firstline != $rootline ]] ; then
if [[ $firstline =~ %.*TEX.*root.* ]] ; then
#echo "Need to replace rootline"
action="replace"
else
#echo "Need to add rootline"
action="addline"
fi
#echo $firstline
else
#echo "Firstline already correct"
action="ok"
#echo $firstline
fi
if [[ $action = "replace" ]] ; then
tf=$(mktemp)
echo "$rootline" > $tf
tail -n +2 $file >> $tf
mv $file $file.back
mv $tf $file
else if [[ $action = "addline" ]] ; then
tf=$(mktemp)
echo "$rootline" > $tf
cat $file >> $tf
mv $file $file.back
mv $tf $file
fi
fi
fi
done
if [[ $cleanup = yes ]] ; then
find . -name '*.tex.back' -delete
fi