-
Notifications
You must be signed in to change notification settings - Fork 7
/
calliope.sh
executable file
·732 lines (649 loc) · 20.1 KB
/
calliope.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
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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#!/bin/bash
VERSION=1.2.5
MY_EDITOR="vimx --servername $(pwgen 8 1)"
MY_VIEWER="xdg-open"
year=$(date +%G)
month=$(date +%m)
day=$(date +%d)
diary_dir="diary"
pdf_dir="pdfs"
todays_entry="$year-$month-$day.tex"
year_to_compile="meh"
entry_to_compile="meh"
entry_to_edit="meh"
entry_to_view="meh"
style_file="research_diary.sty"
package_name=$(basename $style_file ".sty")
other_files_path="other_files/"
images_files_path="images/"
search_command="rg"
search_options="-i -t tex -C2"
search_term=""
author=""
ProjectName=""
bibsrc=""
encryptionId=""
GPG_COMMAND="gpg2"
default_commit_message="Add new entry"
# configuration file to override the above defined variables.
if [ -f .callioperc ]
then
source .callioperc
else
echo "No .callioperc file found. Creating file with empty fields."
echo "Please fill in the necessary information."
echo "It will be used in subsequent runs."
echo "ProjectName=\"\"" > .callioperc
echo "author=\"\"" >> .callioperc
echo "bibsrc=\"\"" >> .callioperc
echo "encryptionId=\"\"" >> .callioperc
exit 0
fi
add_entry ()
{
echo "Today is $year/$month/$day"
echo "Your diary is located in: $diary_dir/."
if [ ! -d "$diary_dir" ]; then
mkdir -p "$diary_dir"
fi
if [ ! -d "$diary_dir/$year" ]; then
mkdir -p "$diary_dir/$year"
mkdir -p "$pdf_dir/$year"
mkdir -p "$diary_dir/$year/$images_files_path"
mkdir -p "$diary_dir/$year/$other_files_path"
fi
if [ -d "$diary_dir/$year" ]; then
echo "Adding new entry to directory $diary_dir/$year."
cd "$diary_dir/$year" || exit -1
filename="$year-$month-$day.tex"
if [ -f "$filename" ]; then
echo "File for today already exists: $diary_dir/$year/$filename."
echo "Happy writing!"
else
if [ ! -f "$style_file" ]; then
ln -s ../../templates/$style_file .
fi
cp ../../templates/entry.tex "$filename" && git add --intent-to-add "$filename"
sed -i "s/@year/$year/g" "$filename"
sed -i "s/@MONTH/$(date +%B)/g" "$filename"
sed -i "s/@dday/$day/g" "$filename"
sed -i "s/@day/$(date +%e)/g" "$filename"
sed -i "s|@author|$author|g" "$filename"
sed -i "s|@project|$ProjectName|g" "$filename"
sed -i "s|@bibsrc|${bibsrc}|g" "$filename"
echo "Finished adding $filename to $year."
cd ../../ || exit -1
fi
fi
if [ -n "$TMUX" ]
then
echo "Setting tmux buffer for your convenience."
tmux set-buffer "$diary_dir/$year/$filename"
else
echo "Not using a tmux session. Not setting buffer."
fi
}
clean ()
{
echo "Cleaning up.."
rm -fv -- *.aux *.bbl *.blg *.log *.nav *.out *.snm *.toc *.dvi *.vrb *.bcf *.run.xml *.cut *.lo* *.brf*
latexmk -c
}
compile_today ()
{
cd "$diary_dir/$year/" || exit -1
echo "Compiling $todays_entry."
if ! latexmk -pdf -recorder -pdflatex="pdflatex -interaction=nonstopmode --shell-escape -synctex=1" -use-make -bibtex "$todays_entry" ; then
echo "Compilation failed. Exiting."
cd ../../ || exit -1
exit -1
fi
clean
if [ ! -d "../../$pdf_dir/$year" ]; then
mkdir -p "../../$pdf_dir/$year"
fi
mv -- *.pdf "../../$pdf_dir/$year/"
echo "Generated pdf moved to pdfs directory."
cd ../../ || exit -1
}
list_latest ()
{
latest_diary_entry=$(ls $diary_dir/$year/$year*tex | tail -1)
latest_pdf_entry=$(ls $pdf_dir/$year/$year*pdf | tail -1)
echo "Latest entry: "
echo "source - $MY_EDITOR $latest_diary_entry"
echo "PDF - $MY_VIEWER $latest_pdf_entry"
}
compile_latest ()
{
cd "$diary_dir/$year/" || exit -1
latest_entry=$(ls $year*tex | tail -1)
echo "Compiling $latest_entry."
if ! latexmk -pdf -recorder -pdflatex="pdflatex -interaction=nonstopmode --shell-escape -synctex=1" -use-make -bibtex "$latest_entry" ; then
echo "Compilation failed. Exiting."
cd ../../ || exit -1
exit -1
fi
clean
if [ ! -d "../../$pdf_dir/$year" ]; then
mkdir -p "../../$pdf_dir/$year"
fi
mv -- *.pdf "../../$pdf_dir/$year/"
echo "Generated pdf moved to pdfs directory."
cd ../../ || exit -1
}
compile_all ()
{
if [ ! -d "$diary_dir/$year_to_compile/" ]; then
echo "$diary_dir/$year_to_compile/ does not exist. Exiting."
exit -1
fi
if [ ! -d "../../$pdf_dir/$year_to_compile" ]; then
mkdir -p ../../$pdf_dir/$year_to_compile
fi
cd "$diary_dir/$year_to_compile/" || exit -1
echo "Compiling all in $year_to_compile."
for i in "$year_to_compile"-*.tex ; do
if ! latexmk -pdf -recorder -pdflatex="pdflatex -interaction=nonstopmode --shell-escape -synctex=1" -use-make -bibtex "$i"; then
echo "Compilation failed at $i. Exiting."
cd ../../ || exit -1
exit -1
fi
mv -- *.pdf "../../$pdf_dir/$year_to_compile/"
echo "Generated pdf for $i moved to pdfs directory."
clean
done
echo "Generated pdf moved to pdfs directory."
cd ../../ || exit -1
}
compile_specific ()
{
year=${entry_to_compile:0:4}
if [ ! -d "$diary_dir/$year/" ]; then
echo "$diary_dir/$year/ does not exist. Exiting."
exit -1
fi
cd "$diary_dir/$year/" || exit -1
echo "Compiling $entry_to_compile"
if ! latexmk -pdf -recorder -pdflatex="pdflatex -interaction=nonstopmode --shell-escape -synctex=1" -use-make -bibtex "$entry_to_compile"; then
echo "Compilation failed. Exiting."
cd ../../ || exit -1
exit -1
fi
clean
if [ ! -d "../../$pdf_dir/$year" ]; then
mkdir -p ../../$pdf_dir/$year
fi
mv -- *.pdf "../../$pdf_dir/$year/"
echo "Generated pdf moved to pdfs directory."
cd ../../ || exit -1
}
# both of these need to be run in the folder itself
# so remember to pushd/popd as required
encrypt ()
{
if [ -z ${encryptionId} ]
then
echo "Encryption is not enabled"
else
if command -v $GPG_COMMAND &> /dev/null
then
if [ -f "$1" ]
then
if [ "${1: -4}" == ".gpg" ]
then
echo "File $1 is already encrypted. Not re-encrypting."
exit 1
else
echo "Encrypting $1 with $encryptionId"
if [ "$1" -ot "${1}.gpg" ]
then
echo "WARNING: Encrypted file newer than text file found."
echo "WARNING: Not encrypting, since this may overwrite a newer encrypted file."
exit 1
else
$GPG_COMMAND --batch --yes --encrypt --sign -r "$encryptionId" "$1" && rm "$1" -f || exit -1
fi
fi
else
echo "File $1 not found"
exit 1
fi
else
echo "$GPG_COMMAND is not installed. Cannot encrypt."
exit 1
fi
fi
}
encrypt_all ()
{
# Any files that do not end in ".gpg" are considered unencrypted, just encrypt those.
if [ -z ${encryptionId} ]
then
echo "Encryption is not enabled"
else
echo "Encrypting all unencrypted files with $encryptionId"
find pdfs/ diary/ -type f -and -not -type l -and -not -name "*.gpg" | while read f
do
directory="$(dirname $f)"
file="$(basename $f)"
pushd $directory
encrypt "$file" || exit -1
popd
done
fi
}
decrypt ()
{
if [ -z ${encryptionId} ]
then
echo "Encryption is not enabled"
else
if command -v $GPG_COMMAND &> /dev/null
then
if [ -f "$1" ]
then
if [ "${1: -4}" == ".gpg" ]
then
echo "Decrypting $1 with $encryptionId"
nongpgfname="$(basename $1 .gpg)"
if [ "$1" -nt "$nongpgfname" ]
then
$GPG_COMMAND --batch --yes --decrypt $1 > "$nongpgfname"
else
echo "WARNING: Decrypted file is newer than encrypted copy."
echo "WARNING: Not overwriting. Please check the files, and re-encrypt if required."
fi
else
echo "File is not a GPG encrypted file. Doing nothing."
fi
else
echo "File $1 not found"
exit 1
fi
else
echo "$GPG_COMMAND is not installed. Cannot decrypt."
exit 1
fi
fi
}
# remember to pushd/popd into the required directory
decrypt_all_sources ()
{
# Any files that end in ".gpg" are considered encrypted, decrypt those.
if [ -z ${encryptionId} ]
then
echo "Encryption is not enabled"
else
echo "Decrypting all encrypted files"
find . -type f -and -not -type l -and -name "*.gpg" | while read f
do
directory="$(dirname $f)"
file="$(basename $f)"
pushd $directory
decrypt "$file" || exit -1
popd
done
fi
}
decrypt_all ()
{
# Any files that end in ".gpg" are considered encrypted, decrypt those.
if [ -z ${encryptionId} ]
then
echo "Encryption is not enabled"
else
echo "Decrypting all encrypted files"
find pdfs/ diary/ -type f -and -not -type l -and -name "*.gpg" | while read f
do
directory="$(dirname $f)"
file="$(basename $f)"
pushd $directory
decrypt "$file" || exit -1
popd
done
fi
}
create_anthology ()
{
Name="$year_to_compile-${ProjectName// /-}-Diary"
FileName=$Name".tex"
tmpName=$Name".tmp"
echo "$ProjectName diary"
echo "Author: $author"
echo "Year: $year_to_compile"
if [ ! -d "$diary_dir/$year_to_compile" ]; then
echo "ERROR: No directory for $year_to_compile exists"
exit;
fi
if [ -z ${encryptionId} ]
then
echo ""
else
pushd "$diary_dir/$year_to_compile" && decrypt_all_sources && popd
fi
cd "$diary_dir" || exit -1
touch $FileName
echo "%" > $FileName
echo "% $ProjectName Diary for $author, $year_to_compile" >> $FileName
echo "%" >> $FileName
echo "\documentclass[a4paper,twoside,11pt]{report}" >> $FileName
echo "\newcommand{\workingDate}{\textsc{$year_to_compile}}" >> $FileName
echo "\newcommand{\userName}{$author}" >> $FileName
echo "\newcommand{\projectName}{$ProjectName}" >> $FileName
echo "\usepackage{$package_name}" >> $FileName
echo " " >> $FileName
echo "\title{$ProjectName diary - $year_to_compile}" >> $FileName
echo "\author{$author}" >> $FileName
echo " " >> $FileName
echo "\rhead{\textsc{$year_to_compile}}" >> $FileName
echo "\chead{\textsc{$ProjectName Diary}}" >> $FileName
echo "\lhead{\textsc{\userName}}" >> $FileName
echo "\rfoot{\textsc{\thepage}}" >> $FileName
echo "\cfoot{\textit{Last modified: \today}}" >> $FileName
echo "\addbibresource{$bibsrc}" >> $FileName
echo "\graphicspath{{./$year_to_compile/$images_files_path}}" >> $FileName
echo "\lstset{{inputpath=./$year_to_compile/$other_files_path}}" >> $FileName
echo " " >> $FileName
echo " " >> $FileName
echo "\begin{document}" >> $FileName
echo "\begin{center} \begin{LARGE}" >> $FileName
echo "\textbf{$ProjectName Diary} \\\\[3mm]" >> $FileName
echo "\textbf{$year_to_compile} \\\\[2cm]" >> $FileName
echo "\end{LARGE} \begin{large}" >> $FileName
echo "$author \end{large} \\\\" >> $FileName
echo "\textsc{Compiled \today}" >> $FileName
echo "\end{center}" >> $FileName
echo "\thispagestyle{empty}" >> $FileName
echo "\newpage" >> $FileName
echo "\tableofcontents" >> $FileName
echo "\thispagestyle{empty}" >> $FileName
# echo "\clearpage" >> $FileName
for i in "$year_to_compile"/"$year_to_compile"-*.tex ; do
echo -e "\n%%% --- $i --- %%%\n" >> $tmpName
echo "\rhead{`grep workingDate $i | cut -d { -f 4 | cut -d } -f 1`}" >> $tmpName
sed -n '/\\begin{document}/,/\\end{document}/p' $i >> $tmpName
echo -e "\n" >> $tmpName
echo "\newpage" >> $tmpName
done
# uncomment the chapter line
sed -i 's/%\\chapter/\\chapter/' $tmpName
sed -i 's/\\begin{document}//g' $tmpName
sed -i 's/\\printindex//g' $tmpName
sed -i 's/\\bibliography.*$//g' $tmpName
sed -i 's/\\printbibliography.*$//g' $tmpName
sed -i 's/\\end{document}//g' $tmpName
sed -i 's|\\includegraphics\(.*\)'"$images_files_path"'\(.*\)|\\includegraphics\1\2|g' $tmpName
sed -i 's|\\lstinputlisting\(.*\)'"$other_files_path"'\(.*\)|\\lstinputlisting\1\2|g' $tmpName
sed -i 's|\\inputminted\(.*\)\('"$other_files_path"'\)\(.*\)|\\inputminted\1'"./$year_to_compile/"'\2\3|g' $tmpName
# with options: options can contain a {, so need to handle them first
sed -i 's/\\includepdf\(\[.*\]\){\(.*\)/\\includepdf\1{'"$year_to_compile"'\/\2/g' $tmpName
# without options
sed -i 's/\\includepdf{\(.*\)/\\includepdf{'"$year_to_compile"'\/\1/g' $tmpName
sed -i 's/\\newcommand/\\renewcommand/g' $tmpName
cat $tmpName >> $FileName
echo "\printbibliography" >> $FileName
echo "\printindex" >> $FileName
echo "\end{document}" >> $FileName
if [ ! -f "$style_file" ]; then
ln -sf ../templates/$style_file .
fi
if ! latexmk -pdf -recorder -pdflatex="pdflatex -interaction=nonstopmode --shell-escape -synctex=1" -use-make -bibtex "$FileName"; then
echo "Compilation failed. Exiting."
cd ../ || exit -1
exit -1
fi
mv -- *.pdf "../$pdf_dir/"
clean
rm $tmpName
echo "$year_to_compile master document created in $pdf_dir."
cd ../ || exit -1
if [ -z ${encryptionId} ]
then
echo ""
else
pushd "$diary_dir/$year_to_compile" && git clean -dfx . && popd
fi
}
edit_latest ()
{
pushd $diary_dir/$year/
latest_diary_entry=$(ls $year*tex* | tail -1)
decrypt "$latest_diary_entry"
outputfile="$(basename $latest_diary_entry .gpg)"
$MY_EDITOR "$outputfile"
popd
}
edit_specific ()
{
year=${entry_to_edit:0:4}
if [ ! -d "$diary_dir/$year/" ]; then
echo "$diary_dir/$year/ does not exist. Exiting."
exit -1
fi
pushd "$diary_dir/$year/"
decrypt "$entry_to_edit.tex.gpg"
$MY_EDITOR "$entry_to_edit.tex"
popd
}
view_specific ()
{
year=${entry_to_view:0:4}
if [ ! -d "$pdf_dir/$year/" ]; then
echo "$pdf_dir/$year/ does not exist. Exiting."
exit -1
fi
pushd "$pdf_dir/$year"
decrypt "$entry_to_view.pdf.gpg"
$MY_VIEWER "$entry_to_view.pdf"
popd
}
view_latest ()
{
pushd $pdf_dir/$year/
latest_pdf_entry=$(ls $year*pdf* | tail -1)
decrypt "$latest_pdf_entry"
outputfile="$(basename $latest_pdf_entry .gpg)"
$MY_VIEWER "$outputfile"
popd
}
view_anthology ()
{
Name="$year_to_compile-${ProjectName// /-}-Diary"
pushd $pdf_dir/
FileName="$Name.pdf.gpg"
decrypt "$FileName"
outputfile="$(basename $FileName .gpg)"
$MY_VIEWER "$outputfile"
popd
}
search_diary ()
{
if ! command -v $search_tool &> /dev/null
then
echo "$search_tool not found."
exit -1
else
echo "Running search command: $search_command $search_options -- $search_term $diary_dir/*/"
$search_command $search_options -- "$search_term" $diary_dir/*/
fi
}
remove_unencrypted ()
{
# if encryption is enabled, delete all unencrypted pdf files before committing
if [ -z ${encryptionId} ]
then
echo "Encryption is not enabled"
else
echo "Deleting all unencrypted files"
find pdfs/ diary/ -not -name "*.gpg" -and -type f -and -not -type l -delete
fi
}
commit_changes ()
{
if ! command -v git &> /dev/null
then
echo "git is not installed."
exit -1
else
encrypt_all && remove_unencrypted || exit -1
echo "Committing changes to repository with commit message \"${commit_message:-$default_commit_message}\""
git add .
if ! git commit -m "${commit_message:-$default_commit_message}"
then
echo "Commit failed. Please check the output and commit manually."
exit -1
else
exit 0
fi
fi
}
usage ()
{
cat << EOF
usage: $0 options
Master script file that provides functions to maintain a journal using LaTeX.
Version: $VERSION
OPTIONS:
-h Show this message and quit
-t Add new entry for today
-l Compile latest entry
-C Compile latest entry and commit to repository.
An optional commit message may be given using the commit_message
variable:
commit_message="Test" ./calliope.sh -C
If one is not given, the default is used: "$default_commit_message".
Note that encryption, if enabled, is only done before committing.
So, please remember to commit early and commit often.
-m Commit to repository (but do not compile).
An optional commit message may be given using the commit_message
variable:
commit_message="Test" ./calliope.sh -m
If one is not given, the default is used: "$default_commit_message".
Note that encryption, if enabled, is only done before committing.
So, please remember to commit early and commit often.
-c Compile today's entry
-a <year>
Year to generate anthology of
-A <year>
Year to view anthology of
-p <year>
Compile all entries in this year
-s <entry> (yyyy-mm-dd)
Compile specific entry
-e edit the latest entry using \$MY_EDITOR
-E <entry> (yyyy-mm-dd)
edit specific entry using \$MY_EDITOR
-v view the latest entry using \$MY_VIEWER
-V <entry> (yyyy-mm-dd)
view specific entry using \$MY_VIEWER
-k <search term>
search diary for term using $search_tool
Please see the documentation of the search tool you use
to see what search terms/regular expressions are supported.
Note: only works when encryption is *not* enabled.
-G <file to decrypt>
decrypt a file
-g <file to encrypt>
encrypt a file
-x
clean folder: remove any unencrypted files if encryption is enabled
EOF
}
if [ "$#" -eq 0 ]; then
usage
exit 0
fi
while getopts "evLltca:A:hp:s:E:V:k:CG:g:xm" OPTION
do
case $OPTION in
t)
add_entry
exit 0
;;
L)
list_latest
exit 0
;;
e)
edit_latest
exit 0
;;
v)
view_latest
exit 0
;;
l)
compile_latest
exit 0
;;
c)
compile_today
exit 0
;;
a)
year_to_compile=$OPTARG
create_anthology
exit 0
;;
A)
year_to_compile=$OPTARG
view_anthology
exit 0
;;
h)
usage
exit 0
;;
p)
year_to_compile=$OPTARG
compile_all
exit 0
;;
s)
entry_to_compile=$OPTARG
compile_specific
exit 0
;;
E)
entry_to_edit=$OPTARG
edit_specific
exit 0
;;
V)
entry_to_view=$OPTARG
view_specific
exit 0
;;
k)
search_term=$OPTARG
search_diary
exit 0
;;
C)
compile_latest
commit_changes
exit 0
;;
m)
commit_changes
exit 0
;;
G)
decrypt "$OPTARG"
exit 0
;;
g)
encrypt "$OPTARG"
exit 0
;;
x)
remove_unencrypted
exit 0
;;
?)
usage
exit 0
;;
esac
done