-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathswth
executable file
·582 lines (516 loc) · 12.6 KB
/
swth
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
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/bin/sh
#
# swth.sh -- create or manage theses for swathesis
#
#
VERSION="1.0"
PROGRAM=$(echo $0 | sed 's%.*/%%')
PROGDIR="$(cd `dirname $0`; echo $PWD)"
if [ ! -z "$ZSH_VERSION" ]; then
setopt shwordsplit
fi
ECHO="/usr/bin/printf %b\\n"
USAGE="Usage: $PROGRAM [OPTIONS] COMMAND
Create or manage swathesis documents.
OPTIONS
--bachelor operate in BSc mode
--master operate in MSc mode
--phd operate in PhD mode
--xe use XeLaTeX
--pdf use pdfLaTeX (default)
--lua use LuaLaTeX
--biber use Biber instead of BibTeX
--help output this help and exit
--dry-run don't execute, just say what would be
--install-completion install bash autocompletion
--version output version information and exit
COMMAND
create create swathesis document
init put directory under $PROGRAM management
bibtex run bibtex [especially for BSc mode]
latex run latex (honors OPTIONS and management file)
gloss run makeglossaries [especially for PhD mode]
show open result document
clean clean auxiliary files
go short for latex; bibtex; latex; latex; latex; show
author add another author in BSc mode
"
_dragons() {
$ECHO "Beware of Dragons!"
}
MODE=
DRY=false
MAIN=
BIBTEX=bibtex
LATEX=pdflatex
MKGLOSS=makeglossaries
RM=rm
MKDIR="mkdir -p"
CP=cp
MV=mv
THESESMARKER="%SWTH_do_not_remove"
TEMPLATEMARKER="TEMPLATE"
__has_cmd () {
command -v "$1" 2>/dev/null >/dev/null
}
_T=$(kpsewhich swathesis.cls)
if [ -z "$_T" ]; then
$ECHO "$PROGRAM: cannot find swathesis"
exit 200
fi
TEMPLATEROOT="$(dirname "$_T")/contrib"
if [ ! -d "$TEMPLATEROOT" ]; then
TEMPLATEROOT="${PROGDIR}/contrib"
fi
if [ ! -d "$TEMPLATEROOT" ]; then
$ECHO "$PROGRAM: cannot find template root "
exit 200
fi
if [ -z "$OSTYPE" ]; then
OSTYPE=$(uname -s | tr A-Z a-z)
fi
if __has_cmd pushd ; then
__pushd() {
pushd "$@" 2>/dev/null >/dev/null || exit
}
__popd() {
popd "$@" 2>/dev/null >/dev/null || exit
}
else
# emulate pushd to our usage
__pushd() {
export P_OLDPWD="$PWD"
cd $1 2>/dev/null >/dev/null || exit
}
__popd() {
if [ -z "$P_OLDPWD" ]; then
$ECHO "$0: directory stack empty"
(exit 1)
return 1
else
cd "$P_OLDPWD" 2>/dev/null >/dev/null || exit
export P_OLDPWD=
fi
}
fi
__sed() {
case "$OSTYPE" in
darwin*) sed -E "$@" ;;
*) sed -r "$@";;
esac
}
__sed_inplace() {
case "$OSTYPE" in
darwin*) sed -E -i '' "$@" ;;
*) sed -r -i "$@";;
esac
}
_clean() {
$RM -f *.log *.aux *.aut *.out *.toc *.synctex.gz *.bbl *.blg *.lol *.loc *.lot *.loa *.idx *.ind *.ilg *.glo *.gls *.glg *.fdb_latexmk *.run.xml *.bcf *.lof *.tdo
$RM -f */*.log */*.aux */*.aut */*.out */*.toc */*.synctex.gz */*.bbl */*.blg */*.lol */*.loc */*.lot */*.loa */*.idx */*.ind */*.ilg */*.glo */*.gls */*.glg */*.fdb_latexmk */*.run.xml */*.bcf */*.lof */*.tdo
}
__readmode() {
if [ -z $MODE ]; then
printf "$1 [BACHELOR|master|phd] "
read ANS
case "$ANS" in
[Mm][Ss][Cc]|[Mm][Aa][Ss]*) MODE=master ;;
[Pp][Hh][Dd]) MODE=phd ;;
*) MODE=bachelor ;;
esac
fi
TEMPLATEDIR="$TEMPLATEROOT/${MODE}_template"
}
__readdir() {
printf "$1 [$PWD] "
read ANS
if [ -z $ANS ]; then
SWTHDIR="$PWD"
else
SWTHDIR="$ANS"
fi
}
__readmain_bp() {
if [ $(date +"%m") -gt 9 ]; then
YEAR=$(date +"%Y")
else
if date -v 1d > /dev/null 2>&1 ; then
# this is BSDish date
YEAR=$(date -v-1y +"%Y")
else
# this is GNUish date
YEAR=$(date --date="1 year ago" +"%Y")
fi
fi
BP=
while [ -z $BP ]; do
printf "What is your bachelor project number (eg, H1)? "
read BP
BP=$($ECHO "$BP" | __sed 's%[ ,./!?]%%g')
done
MAIN="BP${YEAR}${BP}_Theses"
}
__ask_for_main_to_OUT() {
YEAR=$(date +"%Y")
NAME=
while [ -z $NAME ]; do
printf "What is your family name? "
read NAME
NAME=$($ECHO "$NAME" | __sed 's%[ ,./!?]%%g')
done
TITLE=
if [ -z $TITLE ]; then
printf "Please give a short title or leave blank: "
read TITLE
if [ -z $TITLE ]; then
TITLE=Thesis
fi
fi
OUT="${NAME}_${YEAR}_${TITLE}"
}
__readmain() {
if [ "$MODE" = "bachelor" ]; then
printf "Use HPI Bachelor Project information? [NO/yes] "
read ANS
case "$ANS" in
[Yy]|[Yy][Ee][Ss]*)
__readmain_bp
return
;;
*) ;;
esac
fi
__ask_for_main_to_OUT
MAIN="$OUT"
}
__untemplate() {
DFLT=TEMPLATE
__pushd "$1"
for ITEM in ${DFLT}+*; do
if [ -e "$ITEM" ]; then
DST=$(echo "$ITEM" | __sed "s%${TEMPLATEMARKER}\\+%%")
$MV "$ITEM" "$DST"
fi
done
for ITEM in ${DFLT}-*; do
if [ -e "$ITEM" ]; then
DST=$(echo "$ITEM" | __sed "s%${TEMPLATEMARKER}\\-%${MAIN}-%")
$MV "$ITEM" "$DST"
fi
done
__popd
}
__copy_template() {
DFLT=TEMPLATE
$CP -r "$TEMPLATEDIR/". "$SWTHDIR"
$MV "$SWTHDIR/${DFLT}.tex" "$SWTHDIR/${MAIN}.tex"
$MV "$SWTHDIR/${DFLT}-names.tex" "$SWTHDIR/${MAIN}-names.tex"
__untemplate "$SWTHDIR"
__sed_inplace -e "s!${TEMPLATEMARKER}!${MAIN}!g" "${SWTHDIR}"/*.tex
__sed_inplace -e "s!${TEMPLATEMARKER}!${MAIN}!g" "${SWTHDIR}"/*.md
if [ -d "${SWTHDIR}/common" ]; then
__untemplate "${SWTHDIR}/common"
__sed_inplace -e "s!${TEMPLATEMARKER}!${MAIN}!g" "${SWTHDIR}/common"/*.tex
fi
: > "$SWTHDIR/${MAIN}.tex.latexmain"
}
_create() {
if [ -n "$SWTHFILE" -a -f "$SWTHFILE" ]; then
printf "Already configured, really continue? [NO/yes] "
read ANS
case "$ANS" in
[Yy]|[Yy][Ee][Ss]*)
:
;;
*)
$ECHO "aborting"
exit 4
;;
esac
fi
__readmode "What thesis do you want to create?"
__readdir "Where do you want to create?"
if [ -d "$SWTHDIR" ]; then
$ECHO "Found $SWTHDIR"
else
printf "$SWTHDIR does not yet exist, create? [YES/no] "
read ANS
case "$ANS" in
[Nn][Oo]?) ;;
*) $MKDIR "$SWTHDIR" ;;
esac
fi
if [ "$MODE" != "bachelor" ]; then
__set_biber
fi
_init
__copy_template
}
_init() {
__readmode "What thesis do you have?"
__readmain
if [ -z $SWTHFILE ]; then
SWTHFILE="$SWTHDIR/.swth"
fi
if [ "$DRY" != "true" ]; then
: > $SWTHFILE
printf "# swth config file\n" >> $SWTHFILE
printf "MODE=\"$MODE\"\n" >> $SWTHFILE
printf "LATEX=\"$LATEX\"\n" >> $SWTHFILE
printf "BIBTEX=\"$BIBTEX\"\n" >> $SWTHFILE
printf "MAIN=\"$MAIN\"\n" >> $SWTHFILE
fi
}
__set_biber() {
if __has_cmd biber; then
$ECHO "Using Biber for BibTeX"
BIBTEX=biber
fi
}
_bibtex() {
if [ "$MODE" = "bachelor" ]; then
find "$SWTHDIR" -mindepth 2 -name \*.aux | while read FILENAME; do
dir=$(dirname "$FILENAME");
base=$(basename "$FILENAME" | __sed -e 's/\.aux$//')
__pushd "$dir"
$BIBTEX "$base" "$@"
__popd
done
elif [ -n $MAIN ]; then
$BIBTEX "$MAIN" "$@"
else
$ECHO "$PROGRAM: Not working on a main file, forgot \`$PROGRAM init'?"
$BIBTEX "$@"
fi
}
_latex() {
if [ -n $MAIN ]; then
$LATEX "$@" "$MAIN"
else
$ECHO "$PROGRAM: Not working on a main file, forgot \`$PROGRAM init'?"
$LATEX "$@"
fi
}
_gloss() {
if [ -n $MAIN ]; then
$MKGLOSS "$@" "$MAIN"
else
$ECHO "$PROGRAM: Not working on a main file, forgot \`$PROGRAM init'?"
$MKGLOSS "$@"
fi
}
_show() {
if [ -n $MAIN ]; then
$OPEN "$PDFOUT"
else
$ECHO "No main file. Forgot \`$PROGRAM init'?"
exit 3
fi
}
_go() {
_latex && \
[ "$MODE" = "phd" ] && _gloss || true && \
_bibtex && _latex && _latex && _latex && _show
}
_install_completion() {
if ! bash --version >/dev/null 2>/dev/null ; then
$ECHO ">> \`bash' not found, skipping configuration of auto completion."
exit 300
fi
if ! bash -c type complete >/dev/null 2>/dev/null ; then
$ECHO ">> \`complete' not found, skipping configuration of auto completion."
exit 300
fi
if ! bash -c type pkg-config >/dev/null 2>/dev/null ; then
$ECHO ">> \`pkg-config' not found, skipping configuration of auto completion."
exit 300
fi
COMPLETE_INSTALL_DIR=$(pkg-config --variable=compatdir bash-completion)
if [ ! -z "$COMPLETE_INSTALL_DIR" ]; then
if [ ! -w "$COMPLETE_INSTALL_DIR" ]; then
printf "\n$COMPLETE_INSTALL_DIR is not writable, continue as root (sudo)? [NO|yes] "
EOLD=$E
read ANS
case "$ANS" in
[Yy][Ee][Ss]) E=sudo ;;
*) exit 2 ;;
esac
fi
$ECHO 'complete -W "latex bibtex go gloss show clean" swth' | $E tee -a "$COMPLETE_INSTALL_DIR"/swth >/dev/null
E=$EOLD
else
$ECHO ">> no automatic installation directory found, skipping configuration of auto completion."
fi
}
_author() {
if [ "$MODE" != "bachelor" ]; then
$ECHO "$PROGRAM: adding an author outside BSc mode is not meaningful, aborting"
exit 5;
fi
AU_TEMPLATEDIR="$TEMPLATEROOT/Author_template"
AUTHOR=
while [ -z $AUTHOR ]; do
printf "Please name the author to add: "
read AUTHOR
AUTHOR=$($ECHO "$AUTHOR" | sed -e 's%[ ,./!?]%%g')
done
AU_DIR="$SWTHDIR/$AUTHOR"
if [ -d "$AU_DIR" ]; then
$ECHO "$PROGRAM: $AUTHOR directory already exists, don't know what to do, aborting"
exit 6
fi
$CP -r "$AU_TEMPLATEDIR"/. "$AU_DIR"
__untemplate "$AU_DIR"
__ask_for_main_to_OUT
AU_MAIN="$OUT"
$MV "$AU_DIR/TEMPLATE.tex" "$AU_DIR/${AU_MAIN}.tex"
__sed_inplace -e "s!$TEMPLATEMARKER!$AU_MAIN!g" "${AU_DIR}"/*.tex
__sed_inplace -e "s!$TEMPLATEMARKER!$AU_MAIN!g" "${AU_DIR}"/*.md
$ECHO "" > "$AU_DIR/${AU_MAIN}.tex.latexmain"
if grep -q "$THESESMARKER" "$SWTHDIR/${MAIN}.tex" 2>/dev/null >/dev/null; then
__sed -i.bak -e "s!$THESESMARKER!$AUTHOR,$THESESMARKER!" "$SWTHDIR/${MAIN}.tex"
else
$ECHO "$PROGRAM: Cannot find Thesesmarker \`$THESESMARKER' in $MAIN, you're on your own."
fi
}
# process .swth file here or in parent (one dir up)
if [ -z "$SWTHDIR" ]; then
SWTHDIR=$PWD
fi
SWTHFINDDIR=$PWD
for SWTHFINDFILE in ./.swth ../.swth ../../.swth; do
if [ -f "$SWTHFINDFILE" ]; then
SWTHFILE=$SWTHFINDFILE
SWTHDIR=$SWTHFINDDIR
. $SWTHFILE
else
SWTHFINDDIR=$(dirname $SWTHFINDDIR)
# try next round
fi
done
if [ -n $MAIN ]; then
PDFOUT=$MAIN.pdf
fi
#
# help if nothing specified
#
if [ $# -lt 1 ]; then
_dragons
set -- --help
fi
while [ $# -gt 0 ]; do
case "$1" in
--help | -help | -h | help)
$ECHO "$USAGE"
exit 0
;;
--version | -version | -V)
$ECHO "$PROGRAM $VERSION"
exit 0
;;
--install-completion)
_install_completion
exit 0
;;
--verbose | -verbose | -v) VERBOSE=true ;;
--dry-run | -n) DRY=true ;;
--bachelor | -B | -BA | -BSc) MODE=bachelor ;;
--master | -M | -MA | -MSc) MODE=master ;;
--phd | -P | -D | -PhD | --dr | --doctor) MODE=phd ;;
--xe) LATEX=xelatex ;;
--pdf) LATEX=pdflatex ;;
--lua) LATEX=lualatex ;;
--biber) __set_biber ;;
-*)
$ECHO "$PROGRAM: unknown option \`$1', try --help if you need it." >&2
exit 1
;;
*)
break
;;
esac
shift
done
case "$OSTYPE" in
cygwin*)
OPEN="cygstart"
RM="del"
MKDIR="md"
CP="copy"
MV="move"
;;
solaris*) OPEN="xdg-open" ;;
bsd*) OPEN="xdg-open" ;;
linux*)
if __has_cmd open; then
OPEN="open";
else
OPEN="xdg-open"
fi
;;
darwin*)
OPEN="open"
SKIM="net.sourceforge.skim-app.skim"
SKIMS=$(mdfind -count "kMDItemCFBundleIdentifier == 'net.sourceforge.skim-app.skim'")
if [ "$SKIMS" -ge 1 ] || \
[ -e /Applications/Skim.app/Contents/MacOS/Skim ] || \
[ -e /Applications/TeX/Skim.app/Contents/MacOS/Skim ] ; then
OPEN="$OPEN -b $SKIM"
fi
;;
*)
$ECHO "Warn: cannot provide open. \`show' won't work."
OPEN=$ECHO
;;
esac
_dragons
if [ $# -lt 1 ]; then
$ECHO "$PROGRAM: no command specified, try --help." >&2
exit 2
fi
if __has_cmd texfot && [ "$VERBOSE" != "true" ]; then
LATEX="texfot --quiet $LATEX"
fi
if [ "$DRY" = "true" ]; then
RM="$ECHO $RM"
MKDIR="$ECHO $MKDIR"
CP="$ECHO $CP"
MV="$ECHO $MV"
OPEN="$ECHO $OPEN"
LATEX="$ECHO $LATEX"
BIBTEX="$ECHO $BIBTEX"
fi
if [ "$VERBOSE" = "true" ]; then
$ECHO "\
Information
===========
MAIN = $MAIN
MODE = $MODE
SWTHDIR = $SWTHDIR
.swth = $SWTHFILE
DRY = $DRY
PDFOUT = $PDFOUT
LATEX = $LATEX
BIBTEX = $BIBTEX
OPEN = $OPEN
TEMPLATEROOT = $TEMPLATEROOT
TEMPLATEDIR = $TEMPLATEDIR
command = $1"
fi
__pushd "$SWTHDIR" >/dev/null 2>/dev/null
trap '__popd >/dev/null 2>/dev/null' EXIT
CMD="_$1"
if __has_cmd $CMD; then
shift
$CMD "$@"
else
$ECHO "$PROGRAM: unknown command \`$1', try --help if you need it."
exit 2
fi
EXIT=$?
if [ $EXIT -ne 0 ]; then
$ECHO "Whoops"
else
$ECHO "Done"
fi
exit $EXIT