-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompile.sh
executable file
·522 lines (409 loc) · 13 KB
/
compile.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
#!/bin/bash
# get path to this script
SCRIPT=`realpath $0`
SCRIPTPATH=`dirname $SCRIPT`
# get terminal codes for colors, bold, and normal text
bold=$(tput bold)
normal=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
# option for recompiling all recipes that have been edited
a_flag=false
# option to make a booklet cannot be used with merge option (-m)
b_flag=false
# option for just directly merging all pdfs instead of generating a basic book
m_flag=false
# verbose option
verbose=false
# EXTRA verbose (FULL LaTeX output)
extra=false
# option to run slower (one process at a time)
s_flag=false
# option to supress spinner
quiet=false
# force recompilation of all recipes
force=false
# path to output file
output_file_path='Recipes.pdf'
# path to parent directory which is home to all recipe pdfs
recipes_parent_dir="$PWD"
usage="$(basename "$0") [-h] [-abdmoqsvx] -- combine all recipes
Note: This script assumes each recipe's file name is the name of the recipe
written in CamelCase. For example, for a fresh strawberry oatmeal recipe,
the filename should be something like FreshStrawberryOatmeal.tex,
DailyOatmeal.tex, MyFavoriteWinterBreakfast.tex, etc.
where:
-h show this help text
-a recompile all recipes in parent directory that have been edited
-b compile a printable booklet as well (cannot be used with -m)
-d path to parent directory containing all recipe directories [Default: .]
Expected file structure:
|/path/to/parent/dir/
| |
| |RecipeOne/
| | |RecipeOne.tex
| | |RecipeOne.pdf
| |RecipeTwo/
| | |RecipeTwo.tex
| | |RecipeTwo.pdf
| |RecipeThree/
| | |RecipeThree.tex
| | |RecipeThree.pdf
| |...
| |...
| |...
-f force compile everything, even if some recipes have not been changed
-m directly merge all recipe pdfs instead of generating a basic book
(cannot be used with -b)
-s run slower, usually for debug purposes
-o path to output file [Default: Recipes.pdf]
-q quiet, supress all console output
-v verbose
-x EXTRA verbose (show all LaTeX compiler output"
while getopts 'absmd:o:vhxqf' flag; do
case "${flag}" in
a) a_flag=true ;;
b) b_flag=true ;;
m) m_flag=true ;;
s) s_flag=true ;;
d) recipes_parent_dir="${OPTARG}" ;;
o) output_file_path="${OPTARG}" ;;
v) verbose=true ;;
x) extra=true ;;
h) echo "$usage"
exit 2 ;;
q) quiet=true
verbose=false ;;
f) force=true
a_flag=true ;;
esac
done
if [ "$b_flag" = true ] && [ "$m_flag" = true ]
then
if [ "$quiet" = false ]
then
echo "FAILED: -b and -m cannot be used together."
fi
exit 1
fi
# a fancy spinor while things are working
spinner()
{
local delay=0.15
local spinstr='|/-\'
pgrep --parent $$ > /dev/null 2>&1
nonzerochildren=$?
while [ "$nonzerochildren" = 0 ]; do
local temp=${spinstr#?}
if [ "$quiet" = false ]
then
printf "[%c]" "$spinstr"
fi
local spinstr=$temp${spinstr%"$temp"}
sleep $delay
printf "\b\b\b\b\b\b"
pgrep --parent $$ > /dev/null 2>&1
nonzerochildren=$?
done
printf " \b\b\b\b"
}
# start
# move to parent dir
cd "$recipes_parent_dir"
# if -m, preform some setup
if [ "$m_flag" = true ]
then
tmp_dir="$recipes_parent_dir/.tmp"
bookmarks_file="$tmp_dir/bookmarks.txt"
bookmarks_fmt="BookmarkBegin
BookmarkTitle: %s
BookmarkLevel: 1
BookmarkPageNumber: 1
"
rm -rf "$tmp_dir"
mkdir -p "$tmp_dir"
fi
# if not -m, preform different setup
if [ "$m_flag" = false ]
then
# this is a silly way of doing this, but it works
tex_start="\\RequirePackage{recipebook}
\\usepackage{tocloft}
\\renewcommand\\numberline[1]{} % removes numbers from ToC
\\renewcommand{\contentsname}{Index}
\\begin{document}
\\maketitle
\\setlength{\\columnseprule}{0.5pt}
\\tableofcontents
\\addtocontents{toc}{\\protect\\thispagestyle{empty}}
\\pagenumbering{gobble}
\\setlength{\\columnseprule}{0pt}
\\pagenumbering{arabic}"
tex_end="\\end{document}"
tex_contents="$tex_start"
fi
# keep track of number of recompiled files
COMPILECOUNT=0
# keep track of number of new pdf files
PDFCOUNT=0
# loop through all subdirectories for recipes
for recipe_dir in */; do
if [ "$verbose" = true ]
then
echo "$recipe_dir"
fi
# move to recipe dir
cd "$recipe_dir"
# if -a, then recompile all tex files in each recipe
if [ "$a_flag" = true ]
then
for f in *.tex; do
if [ "$force" = false ]
then
# check to see if the file has changed
# is the pdf modified date newer than the source?
# AND is the pdf creation date newer than the source
# modification date
# AND does git diff show no changes to the .tex
# AND is the compiled Recipes.pdf newer than the .tex
git diff --quiet "$f"; nochanges=$? # git diff changes
# date the pdf was created on
creationdate="$(pdfinfo "${f%.*}.pdf" | \
grep CreationDate | sed -e "s|CreationDate:\s*||g")"
# convert to time since the epoch
creationdate="$(date --date="$creationdate" +"%s")"
# get the modification date of the tex file
modificationdate="$(date -r "$f" +"%s")"
if [ "${f%.*}.pdf" -nt "$f" ] && \
[ "$creationdate" -ge "$modificationdate" ] && \
[ "$nochanges" = 0 ] && \
[ "$SCRIPTPATH/Recipes.pdf" -nt "$f" ]
then
if [ "$verbose" = true ]
then
echo " $f unchanged, skipping compilation."
fi
continue
else
if [ "$verbose" = true ]
then
echo "${green}${bold} $f changed.${normal}"
fi
# increase the changed file counter
COMPILECOUNT=$[COMPILECOUNT + 1]
fi
else
# if force is set, then every file will be recompiled
COMPILECOUNT=$[COMPILECOUNT + 1]
fi
if [ "$verbose" = true ]
then
echo " Compiling $f..."
fi
if [ "$extra" = false ]
then
if [ "$s_flag" = false ]
then
{
pdflatex "$f" > /dev/null 2>&1
pdflatex "$f" > /dev/null 2>&1
} &
fi
if [ "$s_flag" = true ]
then
pdflatex "$f" > /dev/null 2>&1 &
spinner
pdflatex "$f" > /dev/null 2>&1 &
spinner
fi
fi
if [ "$extra" = true ]
then
if [ "$s_flag" = false ]
then
{
pdflatex "$f"
pdflatex "$f"
} &
fi
if [ "$s_flag" = true ]
then
pdflatex "$f" &
spinner
pdflatex "$f" &
spinner
fi
fi
done
fi
# loop through all pdf files in recipe dir
for f in *.pdf; do
title="${f%.*}"
spaced_name=$(echo "$title" | \
sed -e 's/\([^[:blank:]]\)\([[:upper:]]\)/\1 \2/g' \
-e 's/\([^[:blank:]]\)\([[:upper:]]\)/\1 \2/g')
if [ "$verbose" = true ]
then
echo " $spaced_name"
fi
# if -m, then just merge the pdfs and be done
if [ "$m_flag" = true ]
then
# make bookmarks for pdfs and prepare them for merge
if [ "$verbose" = true ]
then
echo " Bookmarking $spaced_name..."
fi
printf "$bookmarks_fmt" "$spaced_name" > "$bookmarks_file"
pdftk "$f" update_info "$bookmarks_file" output "$tmp_dir/$f"
if [ "$verbose" = true ]
then
echo " Done."
fi
cd "$script_dir"
fi
# if not -m, then create the basic recipe book (in a very dumb way)
if [ "$m_flag" = false ]
then
if [ "$verbose" = true ]
then
echo " Writing TeX entry..."
fi
tex_contents="$tex_contents
\includepdf[pagecommand={\\thispagestyle{fancy}},linktodoc=true,pages=-,addtotoc={1,section,1, $spaced_name, \
sec:$title}]{$recipe_dir/$f}"
if [ "$verbose" = true ]
then
echo " Done."
fi
fi
done
# move back to parent dir
cd "$recipes_parent_dir"
done
# spin while individual recipes may still be compiling
if [ "$a_flag" = true ] && [ "$COMPILECOUNT" -gt 0 ]
then
spinner
fi
if [ "$verbose" = true ]
then
echo "Done."
fi
# check to see how many pdfs may have changed after possible compilation
for recipe_dir in */; do
# move to recipe folder
cd "$recipe_dir"
for f in *.pdf; do
if [ "$force" = false ]
then
# check to see if the file has changed
git diff --quiet "$f"; nochanges=$?
if [ "$nochanges" = 0 ] && \
[ "$SCRIPTPATH/Recipes.pdf" -nt "$f" ]
then
if [ "$verbose" = true ]
then
echo "$f unchanged."
fi
else
if [ "$verbose" = true ]
then
echo "${green}${bold}$f changed.${normal}"
fi
# increase the changed file counter
PDFCOUNT=$[PDFCOUNT + 1]
fi
else
# if force is set, then every file will be recompiled
PDFCOUNT=$[PDFCOUNT + 1]
fi
# move back to parent dir
cd "$recipes_parent_dir"
done
done
# if -m output the merged pdf and exit 0
if [ "$m_flag" = true ]
then
if [ "$verbose" = true ]
then
echo "Writing $output_file_path..."
fi
pdftk "$tmp_dir"/*.pdf cat output "$output_file_path" &
spinner
if [ "$verbose" = true ]
then
echo "Done."
fi
rm -rf "$tmp_dir"
exit 0
fi
# if not -m compile the recipe book
if [ "$m_flag" = false ]
then
if [ "$COMPILECOUNT" = 0 ]
then
if [ "$quiet" = false ]
then
echo "${red}No recipes have changed, so no compilation was attempted.${normal}"
fi
fi
if [ "$PDFCOUNT" = 0 ]
then
if [ "$quiet" = false ]
then
echo "${red}${bold}No recipe PDFs have changed, so no output was attempted.${normal}"
fi
touch "${output_file_path%.*}.tex"
touch "${output_file_path%.*}.pdf"
# special exit code which symbolizes that everything is up to date
exit 3
fi
if [ "$verbose" = true ]
then
echo "Writing $output_file_path..."
fi
tex_contents="$tex_contents
$tex_end"
echo "$tex_contents" > "${output_file_path%.*}.tex"
if [ "$extra" = false ]
then
{
pdflatex "${output_file_path%.*}.tex" > /dev/null 2>&1
pdflatex "${output_file_path%.*}.tex" > /dev/null 2>&1
} &
spinner
fi
if [ "$extra" = true ]
then
{
pdflatex "${output_file_path%.*}.tex"
pdflatex "${output_file_path%.*}.tex"
} &
spinner
fi
if [ "$verbose" = true ]
then
echo "Done."
fi
# make booklet if desired
if [ "$b_flag" = true ]
then
if [ "$verbose" = true ]
then
echo "Assembling booklet..."
fi
if [ "$extra" = true ]
then
pdfbook2 --short-edge -p letterpaper -i 50 -o 50 -t 0 -b 0 --resolution=150 "${output_file_path%.*}.pdf" &
else
pdfbook2 --short-edge -p letterpaper -i 50 -o 50 -t 0 -b 0 --resolution=150 "${output_file_path%.*}.pdf" > /dev/null 2>&1 &
fi
spinner
if [ "$verbose" = true ]
then
echo "Done."
fi
fi
exit 0
fi