-
Notifications
You must be signed in to change notification settings - Fork 0
/
compile-songbooks.sh
executable file
·1381 lines (1262 loc) · 60.6 KB
/
compile-songbooks.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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# This bash shell script for Linux compiles songbooks, in the Unilaiva songbook
# family of books, using different tools to produce the main output file
# unilaiva-songbook_A5.pdf and others.
#
# Note that this script -- unmodified -- probably works only with bash, as it
# uses some of it's features, especially for arrays, arithmetic and $BASHPID.
# Bash version 4 or higher is required, and tested for. If using Docker for
# compiling, as is the default, Bash version 3 might be enough if the version
# check is removed, but for compiling without Docker, more changes would need
# to be done.
#
# Required binaries in PATH if not using Docker: :)
# bash, lilypond-book, lualatex, texlua, awk, sed, ps, rm, grep, mkdir, which
#
# Optional binaries in PATH if not using Docker:
# - docker: for default compilation mode inside container
# - context or contextjit: used to create printout versions
# - pdftoppm, convert: for extracting cover images from result PDFs
# - python3, fluidsynth, ffmpeg: for extracting midi and encoding audio
# - sha256 to compare file contents in order to not deploy unless needed
#
# Required binaries in PATH if using Docker (the default): :)
# bash, docker, ps, rm, mkdir, cp, grep
#
#
# Usage: run without argument for default operation. Run with --help argument
# for further information about options, or see function print_usage_and_exit
# below.
#
# Set this to 1 if wanting to use colors, 0 otherwise. The terminal's support
# for colors is tested, and if it is missing, the color codes are disabled
# regardless of this setting.
USE_COLORS=1
# Get settings from ENV variables, and set defaults if they do not exist
# If "true", make the temporary compilation directory a symlink pointing to
# a location under /tmp. Otherwise, and by default, the temporary directory
# is a normal subdirectory under project root. Set this to true only if you
# know the implications (is your /tmp using tmpfs?).
if [ -n "${ULSBS_USE_SYSTEM_TMP_FOR_TEMP}" ] && [ "${ULSBS_USE_SYSTEM_TMP_FOR_TEMP}" = "true" ]; then
USE_SYSTEM_TMP_FOR_TEMP="true"
else
USE_SYSTEM_TMP_FOR_TEMP="false" # default
fi
# Maximum number of parallel compilation jobs. Each job takes quite a bit
# of memory, so this should be limited.
if [ -n "${ULSBS_MAX_PARALLEL}" ]; then
MAX_PARALLEL="${ULSBS_MAX_PARALLEL}"
else
MAX_PARALLEL="6" # default
fi
# Maximum total memory use for Docker container. Note to use small letter g
# to signify a gigabyte. 6g should be enough for 6 parallel jobs.
# This is passed to docker with --memory option. See Docker documentation
# at https://docs.docker.com/config/containers/resource_constraints/
if [ -n "${ULSBS_MAX_DOCKER_MEMORY}" ]; then
MAX_DOCKER_MEMORY="${ULSBS_MAX_DOCKER_MEMORY}"
else
MAX_DOCKER_MEMORY="6g" # default
fi
# Maximum total memory and swap (together) use for Docker container. If set
# to same as MAX_DOCKER_MEMORY, swap is disabled. This is passed to docker with
# # --memory-swap option.
MAX_DOCKER_MEMORY_PLUS_SWAP="${MAX_DOCKER_MEMORY}"
MAIN_FILENAME_BASE="unilaiva-songbook_A5" # filename base for the main document (without .tex suffix)
PART1_FILENAME_BASE="unilaiva-songbook_part1_A5" # filename base for the 2-part document's part 1 (without .tex suffix)
PART2_FILENAME_BASE="unilaiva-songbook_part2_A5" # filename base for the 2-part document's part 2 (without .tex suffix)
ASTRAL_FNAME_PREFIX="unilaiva-astral" # filename prefix for unilaiva astral books
SELECTION_FNAME_PREFIX="ul-selection" # filename prefix for selections
LYRICSONLY_FNAMEPART="_LYRICS-ONLY" # added to filenames for lyrics-only books
CHARANGO_FNAMEPART="_CHARANGO" # added to filenames for charango books
NODEPLOY_FNAMEPART="_NODEPLOY" # files having this in their name are not deployed
PAPERA5_FNAMEPART="_A5" # files having this in their name are treated as having A5 size pages
TEMP_DIRNAME="temp" # just the name of a subdirectory, not an absolute path
LOCKFILE="${TEMP_DIRNAME}/lock" # if exists, compilation is underway (or uncleanly aborted)
RESULT_DIRNAME="result" # just the name of a subdirectory, not an absolute path
RESULT_IMAGE_SUBDIRNAME="images" # just the name of a subdirectory, not an absolute path
RESULT_PRINTOUT_SUBDIRNAME="printouts" # just the name of a subdirectory, not an
RESULT_AUDIO_SUBDIRNAME="audio" # just the name of a subdirectory, not an absolute path
RESULT_MIDI_SUBDIRNAME="midi" # just the name of a subdirectory, not an absolute path
COMMONICON_DIRNAME="content/img"
COMMONMETADATA_DIRNAME="metadata"
DEPLOY_DIRNAME="deploy" # just the name of a subdirectory, not an absolute path
DEPLOY_IMAGE_SUBDIRNAME="images" # just the name of a subdirectory, not an absolute path
DEPLOY_PRINTOUT_SUBDIRNAME="printouts" # just the name of a subdirectory, not an absolute path
DEPLOY_AUDIO_SUBDIRNAME="audio" # just the name of a subdirectory, not an absolute path
DEPLOY_MIDI_SUBDIRNAME="midi" # just the name of a subdirectory, not an absolute path
DEPLOY_COMMONICON_SUBDIRNAME="${RESULT_IMAGE_SUBDIRNAME}/icons"
DEPLOY_COMMONMETADATA_SUBDIRNAME="metadata"
SONG_IDX_SCRIPT="tex/ext_packages/songs/songidx.lua"
# The following is the locale used in creating the indexes, thus affecting the
# sort order. Finnish (UTF8) is the default. Note that the locale used must be
# installed on the system. To list installed locales on an UNIX, execute
# "locale -a".
SORT_LOCALE="fi_FI.utf8" # Recommended default: fi_FI.utf8
# will be added to automatically modified images basename
IMG_AUTOWIDENOTAGS_FNAME_POSTFIX="_AUTOWIDENOTAGS"
COVERIMAGE_HEIGHT="1024" # Height for the optionally extracted cover image file
COVERIMAGE_AUTOWIDE_WIDTH="976" # Width of the optionally extended cover image file
INITIAL_DIR="${PWD}" # Store the initial directory (absolute path)
TOO_MANY_WARNINGS_FILE="${INITIAL_DIR}/${TEMP_DIRNAME}/last_compilation_too_many_warnings"
RESULTLIST_FILE="${INITIAL_DIR}/${TEMP_DIRNAME}/last_compilation_results"
RESULTLIST_FILE_IN_RESULTDIR="${INITIAL_DIR}/${RESULT_DIRNAME}/last_compilation_results"
RESULT_TYPE_INFO="INFO"
RESULT_TYPE_MAIN_PDF="MAINPDF"
RESULT_TYPE_PRINTOUT_PDF="PRINTOUTPDF"
RESULT_TYPE_LYRICONLY_PDF="LYRICONLYPDF"
RESULT_TYPE_IMAGE="IMAGE"
RESULT_TYPE_AUDIODIR="AUDIODIR"
RESULT_TYPE_MIDIDIR="MIDIDIR"
RESULT_TYPE_COMMONICON="COMMONICON"
RESULT_TYPE_COMMONMETADATA="COMMONMETADATA"
# used as separator in result files list, must be only one character
RESULT_SEPARATOR="~"
# Function: print the program usage informationand exit.
print_usage_and_exit() {
echo -e ""
echo -e "${C_LMAGENTA}Usage: ${C_WHITE}./compile-songbooks.sh [OPTION]... [FILE]...${C_RESET}"
echo -e ""
echo -e "If run without any arguments, all main .tex documents of Unilaiva songbook"
echo -e "family (main book, astral books, partial booklets and selections) will be"
echo -e "compiled, plus all supported extra formats for all of them, and the"
echo -e "resulting files will be put into './${RESULT_DIRNAME}' directory, and copied"
echo -e "to the './${DEPLOY_DIRNAME}' directory (if it exists)."
echo -e ""
echo -e "If file names are given as arguments, *only* they will be compiled. The"
echo -e "files must reside in the project's root directory and have .tex extension."
echo -e ""
echo -e "${C_YELLOW}Options:${C_RESET}"
echo -e ""
echo -e " ${C_WHITE}--deploy-last${C_RESET} : only deploy the files created by the last compile; "
echo -e " do nothing else"
echo -e " ${C_WHITE}--deploy-common${C_RESET} : only deploy common files (icons, metadata);"
echo -e " do nothing else"
echo -e " ${C_WHITE}--docker-rebuild${C_RESET} : force rebuilding of Docker image. Not normally needed."
echo -e " ${C_WHITE}--help${C_RESET} : print this usage information"
echo -e " ${C_WHITE}--no-audio${C_RESET} : do not create audio (mp3) files from Lilypond sources"
echo -e " ${C_WHITE}--no-astral${C_RESET} : do not compile unilaiva-astral* books"
echo -e " ${C_WHITE}--no-cleantemp${C_RESET} : do not clean temp dir after succesful compile"
echo -e " ${C_WHITE}--no-coverimage${C_RESET} : do not extract cover page as image"
echo -e " ${C_WHITE}--no-deploy${C_RESET} : do not copy PDF files to ./${DEPLOY_DIRNAME}/"
echo -e " ${C_WHITE}--no-docker${C_RESET} : do not use the Docker container for compiling"
echo -e " ${C_WHITE}--no-extrainstr${C_RESET} : do not generate additional variants for extra instruments"
echo -e " ${C_WHITE}--no-lyric${C_RESET} : do not generate additional lyrics-only books"
echo -e " ${C_WHITE}--no-midi${C_RESET} : do not copy MIDI files created by Lilypond to the result"
echo -e " directory"
echo -e " ${C_WHITE}--no-partial${C_RESET} : do not compile partial books"
echo -e " ${C_WHITE}--no-printouts${C_RESET} : do not create extra printout PDFs"
echo -e " ${C_WHITE}--no-selections${C_RESET} : do not create selection booklets"
echo -e " ${C_WHITE}--pull${C_RESET} : Execute git pull before compiling;"
echo -e " this is always done outside Docker"
echo -e " ${C_WHITE}--sequential${C_RESET} : compile documents sequentially (the default is to"
echo -e " compile them in parallel), use on low memory systems"
echo -e " ${C_WHITE}--shell${C_RESET} : Execute an interactive shell within docker only,"
echo -e " does not compile anything."
echo -e " ${C_WHITE}-q${C_RESET} : use for quick development build of the main document;"
echo -e " equals to ${C_WHITE}--no-partial --no-selections --no-astral"
echo -e " --no-printouts --no-cleantemp --no-coverimage"
echo -e " --no-deploy --no-extrainstr --no-midi --no-audio"
echo -e " --no-lyric${C_RESET}"
echo -e ""
echo -e "In addition to the full version of tha main Unilaiva Songbook, also"
echo -e "two-booklet version of it is created, with parts 1 and 2 in separate PDFs."
echo -e "This is not done, if ${C_WHITE}--no-partial${C_RESET} option is present or files are given"
echo -e "as arguments."
echo -e ""
echo -e "'Unilaiva no Astral' books are also compiled by default in addition to"
echo -e "Unilaiva Songbook, unless ${C_WHITE}--no-astral${C_RESET} option is present or files are"
echo -e "given as arguments. 'Unilaiva no Astral' books' main files are named"
echo -e "unilaiva-astral-*.tex"
echo -e ""
echo -e "Also selection booklets, with specific songs only, specified in files"
echo -e "named ul-selection_*.tex are compiled, unless ${C_WHITE}--no-selections${C_RESET} option"
echo -e "is present or files are given as arguments."
echo -e ""
echo -e "If .tex files are explicitly given on the command line, only those"
echo -e "documents are compiled."
echo -e ""
echo -e "By default, additional lyrics-only books are generated also. They have"
echo -e "no notation, chords, etc. musical information, except for lyrics and"
echo -e "bar lines. Use ${C_WHITE}--no-lyric${C_RESET} option to not create them."
echo -e ""
echo -e "By default, MIDI files and audio files are extracted and generated from"
echo -e "Lilypond sources and copied to './${RESULT_DIRNAME}/${RESULT_MIDI_SUBDIRNAME}' and './${RESULT_DIRNAME}/${RESULT_AUDIO_SUBDIRNAME}'"
echo -e "respectively, unless ${C_WHITE}--no-midi${C_RESET} or ${C_WHITE}--no-audio${C_RESET} options are present."
echo -e ""
echo -e "For documents containing ${PAPERA5_FNAMEPART} in their filename, special versions for"
echo -e "printing on A4 sized paper are created, if 'context' or 'contextjit' binary"
echo -e "is available and ${C_WHITE}--no-printouts${C_RESET} option is not given."
echo -e ""
echo -e "The resulting PDF files will also be copied to './${DEPLOY_DIRNAME}' directory (if"
echo -e "it exists), unless they have ${NODEPLOY_FNAMEPART} in their filename or ${C_WHITE}--no-deploy${C_RESET}"
echo -e "option is given. Common data is always deployed, if there is anything else"
echo -e "to deploy. To only deploy the common data alone, use ${C_WHITE}--deploy-common${C_RESET} option."
echo -e ""
echo -e "For more information, see ${C_GREEN}README.md${C_RESET} file."
echo -e ""
echo -e "${C_YELLOW}TL;DR:${C_RESET} just run ${C_WHITE}./compile-songbooks.sh${C_RESET} without arguments in the project's"
echo -e " root directory for default operation. The resulting PDF files will"
echo -e " be put in the './${RESULT_DIRNAME}' subdirectory."
echo -e ""
exit 1
}
# Function: final clean up needed to run before exiting the whole script
cleanup() {
# return to the original directory
cd "${INITIAL_DIR}"
# Clean up temporary files from the project root, they are sometimes left
# behind.
rm tmp????????.sxc tmp????????.out tmp????????.log tmp????????.pdf idx_*.sxd missfont.log 2>"/dev/null"
# Remove lockfile
rm "${LOCKFILE}" 2>"/dev/null"
}
# Function: sends a signal to a tree of processes (default: KILL)
# Usage: killtree <root process> <signal>
#
# The given signal is sent to the root process and all it's children
# recursively, leaf first. Current subshell and the main shell, if included
# in the tree, are not included. If signal argument is omitted, KILL signal
# is used as the default.
#
# The signal is not sent to the current process or the main process even if
# they are in the tree.
#
# Calls binaries 'kill' and 'ps', requires Bash version 4 or higher.
# Note: $$ holds the main shell's PID, and $BASHPID (introduced in Bash v4)
# holds the current subshell's PID.
killtree() {
local _pid=$1
local _sig=${2:-KILL}
# Stops a parent before killing it's children, to prevent it forking new
# children:
[ ${_pid} -ne ${BASHPID} ] && [ ${_pid} -ne ${$} ] && kill -STOP ${_pid} >/dev/null 2>&1
# Recursively handle the children:
for _child in $(ps -o pid --no-headers --ppid ${_pid}); do
killtree ${_child} ${_sig}
done
# We're at the bottom of the recursion, kill the process, unless it is the
# main process or the current process:
if [ ${_pid} -ne ${BASHPID} ] && [ ${_pid} -ne ${$} ]; then
kill -${_sig} ${_pid} >/dev/null 2>&1
wait ${_pid} 2>/dev/null # This wait suppresses the "Killed" output
fi
}
# Function: print error code and message, kill subprocesses and exit the program.
# Usage: die <errorcode> <message>
#
# If errorcode is 255, only prints error and exits without cleanup.
#
# If this is called from a subprocess, the error is printed, and TERM signal
# sent to the main process, and exit the subprocess. Main process has trapped
# TERM signal, and will respond by calling this function again with <errorcode>
# 99.
#
# If this is called from the main process, print error only if <errorcode> is
# not 99, then kill all the subprocesses, run final cleanup and exit.
#
# Requires Bash v4 or higher for $BASHPID, for current (sub)process id.
die() {
# Do not print the error, if <errorcode> is 99 and we're in the main process.
if [ ${1} -eq 99 ] && [ ${$} -eq ${BASHPID} ]; then
echo -e "${PRETXT_ABORTED}${C_YELLOW}All compilations are aborted.${C_RESET}" >&2
else
# Print the error:
echo -e "${PRETXT_ERROR}${2}" >&2
fi
[ ${1} -eq 255 ] && exit ${1} # Just exit if code is 255 (lockfile present)
if [ ${$} -eq ${BASHPID} ]; then
# We're in the main process.
# Kill the whole tree of child processes:
killtree ${$} KILL
# Make the final clean up:
cleanup
else
# We're in a sub process.
# Send a TERM signal to the main process, it will handle the killing of
# children and the finalisation of the script.
kill -TERM ${$}
fi
exit ${1}
}
# Function: set up UI strings and colors, called in the beginning of the script
setup_ui() {
# Test if colorization is supported
if [ "$USE_COLORS" -eq 1 ]; then
if [[ "$TERM" = *xterm*color* ]]; then
USE_COLORS=1
elif [ -x /usr/bin/tput ] && tput setaf 1 >&/dev/null; then
# We have color support; assume it's compliant with Ecma-48
# (ISO/IEC-6429). (Lack of such support is extremely rare, and such
# a case would tend to support setf rather than setaf.)
USE_COLORS=1
else
USE_COLORS=0
fi
fi
if [ "$USE_COLORS" -eq 1 ]; then # define colors
C_BLACK="\033[0;30m" ; C_BLUE="\033[0;34m" ; C_GREEN="\033[0;32m"
C_CYAN="\033[0;36m" ; C_RED="\033[0;31m" ; C_MAGENTA="\033[0;35m"
C_BROWN="\033[0;33m" ; C_GRAY="\033[0;37m" ; C_DGRAY="\033[1;30m"
C_LBLUE="\033[1;34m" ; C_LGREEN="\033[1;32m" ; C_LCYAN="\033[1;36m"
C_LRED="\033[1;31m" ; C_LMAGENTA="\033[1;35m" ; C_YELLOW="\033[1;33m"
C_WHITE="\033[1;37m" ; C_RESET="\033[0m"
else # if colors are not supported, set color strings empty
C_BLACK="" ; C_BLUE="" ; C_GREEN=""
C_CYAN="" ; C_RED="" ; C_MAGENTA=""
C_BROWN="" ; C_GRAY="" ; C_DGRAY=""
C_LBLUE="" ; C_LGREEN="" ; C_LCYAN=""
C_LRED="" ; C_LMAGENTA="" ; C_YELLOW=""
C_WHITE="" ; C_RESET=""
fi
# setup DOC_COLORS; each document gets it's own color from this array
DOC_COLORS[0]="${C_BROWN}"
DOC_COLORS[1]="${C_MAGENTA}"
DOC_COLORS[2]="${C_CYAN}"
DOC_COLORS[3]="${C_BLUE}"
DOC_COLORS[4]="${C_YELLOW}"
DOC_COLORS[5]="${C_LMAGENTA}"
DOC_COLORS[6]="${C_LCYAN}"
DOC_COLORS[7]="${C_LBLUE}"
DOC_COLOR_COUNT=8
# Some UI Text
PRETXT_DOCKER="${C_WHITE}DOCKER ${C_RESET}"
PRETXT_GIT="${C_WHITE}GIT ${C_RESET}"
PRETXT_START="${C_GREEN}START ${C_RESET}"
PRETXT_EXEC="${C_WHITE}EXEC ${C_RESET}"
PRETXT_NOEXEC="${C_DGRAY}NOEXEC ${C_RESET}"
PRETXT_DEPLOY="${C_WHITE}DEPLOY ${C_RESET}"
PRETXT_NODEPLOY="${C_DGRAY}NODEPLOY ${C_RESET}"
PRETXT_DEBUG="${C_DGRAY}DEBUG ${C_RESET}"
PRETXT_SUCCESS="${C_LGREEN}SUCCESS ${C_RESET}"
PRETXT_ERROR="${C_RED}ERROR ${C_RESET}"
PRETXT_ABORTED="${C_RED}ABORTED ${C_RESET}"
PRETXT_WARNING="${C_YELLOW}WARNING ${C_RESET}"
PRETXT_SEE="See: "
PRETXT_SPACE=" "
TXT_DONE="${C_GREEN}Done.${C_RESET}"
}
# Returns 0 if directory given as argument exists and is not empty, 1 otherwise.
dir_notempty() {
if [ -d ${1} ]; then
[ "$(ls -A ${1})" ] && return 0
fi
return 1
}
# Build, create and stat docker container and start the compile script therein.
# Usage: compile_in_docker <arguments for compile script>
compile_in_docker() {
echo ""
which "docker" >"/dev/null" 2>&1
if [ $? -ne 0 ]; then
echo "Docker executable not found. Please install Docker to compile the"
echo "songbook in the 'official' environment. To compile without Docker,"
echo "use the --no-docker option, but be aware that the resulting book"
echo "might not be exactly as intended."
die 1 "Docker executable not found."
fi
# Build the compiler Docker image only if it doesn't yet exist, or if the
# Dockerfile (modification date) is newer than the image
if [ "${dockerrebuild}" == "true" ]; then # rebuild requested by argument
docker_build_needed="true"
else # try to see
echo -e "${PRETXT_DOCKER}Query compiler image status"
docker_build_needed=""
if [ ! -z $(docker image ls -q unilaiva-compiler) ]; then
# image exists, compare dates...
# NOTE: Date comparison does not work with date -d on MacOS.
dockerimage_ts="$(${datecmd} -d $(docker inspect -f '{{ .Created }}' unilaiva-compiler) +%s)"
if [ $? -eq 0 ]; then
# first date command worked, do the rest of the comparison
dockerfile_ts="$(${datecmd} -r docker/unilaiva-compiler/Dockerfile +%s)"
[ ${dockerfile_ts} -gt ${dockerimage_ts} ] && docker_build_needed="true"
else
echo -e "${PRETXT_ERROR}Can not test the version of docker image. Use --docker-rebuild option if needed."
fi
else
docker_build_needed="true"
fi
fi
if [ "${docker_build_needed}" = "true" ]; then
echo -e "${PRETXT_DOCKER}Build compiler image..."
# Build the compiler image
docker build -t unilaiva-compiler ./docker/unilaiva-compiler || die 1 "Docker build error"
echo -e "${PRETXT_DOCKER}Building image complete."
echo ""
echo -e "${PRETXT_SPACE}To remove old dangling images and unused volumes, it is safe"
echo -e "${PRETXT_SPACE}to run the following command:"
echo ""
echo -e "${PRETXT_SPACE}'docker image prune ; docker volume prune'"
echo ""
echo -e "${PRETXT_SPACE}To remove old images, which are not needed anymore, you have"
echo -e "${PRETXT_SPACE}to find them with 'docker image ls -a' and then remove them"
echo -e "${PRETXT_SPACE}manually with 'docker image rm <image_id>'."
echo ""
fi
echo -e "${PRETXT_DOCKER}Start compiler container"
# Run the container with current user's ID and bind mount current directory.
# Temp dir is mounted separately, as if it is a symlink, this is required.
docker run -it --rm --read-only \
-e ULSBS_MAX_PARALLEL="${ULSBS_MAX_PARALLEL}" \
-e ULSBS_MAX_DOCKER_MEMORY="${ULSBS_MAX_DOCKER_MEMORY}" \
-e ULSBS_USE_SYSTEM_TMP_FOR_TEMP="${ULSBS_USE_SYSTEM_TMP_FOR_TEMP}" \
--memory="${MAX_DOCKER_MEMORY}" \
--memory-swap="${MAX_DOCKER_MEMORY_PLUS_SWAP}" \
--user $(id -u):$(id -g) \
--mount type=bind,src="$(realpath .)",dst="/unilaiva-songbook" \
--mount type=bind,src="$(realpath .)/${TEMP_DIRNAME}",dst="/unilaiva-songbook/${TEMP_DIRNAME}" \
--mount type=volume,src="unilaiva-compiler_homecache",dst="/home/unilaiva" \
--mount type=tmpfs,tmpfs-size=128m,dst="/tmp" \
--mount type=tmpfs,tmpfs-size=16m,dst="/run" \
unilaiva-compiler \
$@
return $?
}
# Function: compile the document given as parameter
# Usage: compile_document <filename_base_for_tex_document> <doc_color_string>
# - Give filename without path and without ".tex" suffix.
# - doc_color_string is a string containing escaped color instructions
# - Intended to be called with & to start a new subprocess
compile_document() {
# Function: prints the error log and calls die().
# Usage: die_log <errorcode> <message> <logfile>
die_log() {
echo -e "${PRETXT_ERROR}${txt_docbase}: ${2}"
echo ""
echo -e "${C_WHITE}Displaying log file for ${txt_doctex}: ${C_YELLOW}${3}${C_RESET}"
echo ""
cat "${3}"
echo ""
echo -e "${PRETXT_SEE}${C_YELLOW}${temp_dirname_twolevels}/${3}${C_RESET}"
echo ""
# Parse output logs for giving better advice:
if [ "${3}" = "log-03_titleidx.log" ]; then # test for locale problem
grep "invalid locale" "${3}"
if [ ${?} -eq 0 ]; then
echo ""
echo -e "${C_RED}Locale ${SORT_LOCALE} must be installed on the system or the compile script"
echo -e "must be modified (line starting with SORT_LOCALE) to use a different locale.${C_RESET}"
fi
fi
die $1 "${txt_docbase}: $2\n${PRETXT_SPACE}Exit code: ${C_RED}${1}1${C_RESET}"
}
# Function: compile the document given as parameter.
# Only creates a PDF document. Must already be in the subdirectory created by
# lilypond-book. Available for compile_document only.
# Usage: compile_document <filename_base_for_tex_document> <doc_color_string>
# - Give filename without path and without ".tex" suffix.
# - doc_color_string is a string containing escaped color instructions
# - prefix for log filenames, can be empty string
compile_document_sub() {
local currentdoc_basename="${1}"
local txt_docbase="${C_DGRAY}[${2}${1}${C_DGRAY}]${C_RESET}"
local logfileprefix="${3}"
echo -e "${PRETXT_EXEC}${txt_docbase}: lualatex (1st run)"
# First run of lualatex:
local log02file="${logfileprefix}log-02_lualatex.log"
lualatex -draftmode -file-line-error -halt-on-error -interaction=nonstopmode \
"${currentdoc_basename}.tex" \
1>"${log02file}" 2>&1 \
|| die_log $? "Compilation error running lualatex!" "${log02file}"
# Only create indices, if not compiling a selection booklet (bashism):
if [[ ${currentdoc_basename} != ${SELECTION_FNAME_PREFIX}* ]]; then
echo -e "${PRETXT_EXEC}${txt_docbase}: texlua (create indices)"
# Create indices:
local log03file="${logfileprefix}log-03_titleidx.log"
texlua "${SONG_IDX_SCRIPT}" -l ${SORT_LOCALE} \
"idx_title.sxd" "idx_title.sbx" \
1>"${log03file}" 2>&1 \
|| die_log $? "Error creating song title indices!" "${log03file}"
# Author index creation is commented out, as it is not used (now):
# local log04file="${logfileprefix}log-04_authidx.log"
# texlua "${SONG_IDX_SCRIPT}" -l ${SORT_LOCALE} \
# idx_auth.sxd idx_auth.sbx \
# 1>"${log04file}" 2>&1 \
# || die_log $? "Error creating author indices!" "${log04file}"
local log05file="${logfileprefix}log-05_tagidx.log"
texlua "${SONG_IDX_SCRIPT}" -l ${SORT_LOCALE} -b "tags.can" \
"idx_tag.sxd" "idx_tag.sbx" \
1>"${log05file}" 2>&1 \
|| die_log $? "Error creating tag (scripture) indices!" "${log05file}"
fi
echo -e "${PRETXT_EXEC}${txt_docbase}: lualatex (2nd run)"
# Second run of lualatex:
local log06file="${logfileprefix}log-06_lualatex.log"
lualatex -draftmode -file-line-error -halt-on-error -interaction=nonstopmode \
"${currentdoc_basename}.tex" \
1>"${log06file}" 2>&1 \
|| die_log $? "Compilation error running lualatex (2nd time)!" "${log06file}"
echo -e "${PRETXT_EXEC}${txt_docbase}: lualatex (3rd run)"
# Third run of lualatex, creates the final main PDF document:
local log07file="${logfileprefix}log-07_lualatex.log"
lualatex -file-line-error -halt-on-error -interaction=nonstopmode \
"${currentdoc_basename}.tex" \
1>"${log07file}" 2>&1 \
|| die_log $? "Compilation error running lualatex (3rd time)!" "${log07file}"
cp "${currentdoc_basename}.pdf" "${INITIAL_DIR}/${RESULT_DIRNAME}/" \
|| die $? "Error copying ${currentdoc_basename}.pdf from temporary directory!"
echo "${RESULT_TYPE_MAIN_PDF}${RESULT_SEPARATOR}${currentdoc_basename}.pdf" \
>>${RESULTLIST_FILE}
# Create printouts, if filename contains ${PAPERA5_FNAMEPART} and printouts are not disabled
# by a command line argument:
if [[ "${currentdoc_basename}" != *"${PAPERA5_FNAMEPART}"* ]]; then
echo -e "${PRETXT_NOEXEC}${txt_docbase}: Extra printout PDFs not created, no ${PAPERA5_FNAMEPART} in filename"
else
if [ ${createprintouts} != "true" ]; then
echo -e "${PRETXT_NOEXEC}${txt_docbase}: Extra printout PDFs not created as per request"
else
local contextbinary=""
which "context" >"/dev/null" 2>&1
if [ $? -eq 0 ]; then
contextbinary="context"
else
which "contextjit" >"/dev/null" 2>&1
if [ $? -eq 0 ]; then
contextbinary="contextjit"
fi
fi
if [ "${contextbinary}" = "" ]; then
echo -e "${PRETXT_NOEXEC}${txt_docbase}: Extra printout PDFs not created; no 'context/contextjit'"
else
echo -e "${PRETXT_EXEC}${txt_docbase}: context (create printouts)"
mkdir "${INITIAL_DIR}/${RESULT_DIRNAME}/${RESULT_PRINTOUT_SUBDIRNAME}" 2>"/dev/null"
# A5 on A4, double sided, must cut: Use 'awk' to create a copy of the
# printout template file with changed input PDF file name and then
# execute 'context' or 'contextjit' on the new file.
printout_dsf_basename="printout-BOOKLET_${currentdoc_basename}-on-A4-doublesided-needs-cutting"
awk "/replace-this-filename.pdf/"' { gsub( "'"replace-this-filename.pdf"'", "'"${currentdoc_basename}.pdf"'" ); t=1 } 1; END{ exit( !t )}' "tex/printout-template_BOOKLET-A5-on-A4-doublesided-needs-cutting.context" >"${printout_dsf_basename}.context" \
|| die $? "[${currentdoc_basename}]: Error with 'awk' when creating dsf printout!"
local log08file="${logfileprefix}log-08_printout-dsf.log"
${contextbinary} "${printout_dsf_basename}.context" \
1>"${log08file}" 2>&1 \
|| die_log $? "Error creating dsf printout!" "${log08file}"
cp "${printout_dsf_basename}.pdf" "${INITIAL_DIR}/${RESULT_DIRNAME}/${RESULT_PRINTOUT_SUBDIRNAME}/" \
|| die $? "Error copying printout PDF from temporary directory!"
echo "${RESULT_TYPE_PRINTOUT_PDF}${RESULT_SEPARATOR}${printout_dsf_basename}.pdf" \
>>${RESULTLIST_FILE}
# A5 on A4, a A5+A5 spread on single A4 surface: Use 'awk' to create a
# copy of the printout template file with changed input PDF file name
# and then execute 'context' or 'contextjit' on the new file.
printout_sss_basename="printout-EASY_${currentdoc_basename}-on-A4-sidebyside-simple"
awk "/replace-this-filename.pdf/"' { gsub( "'"replace-this-filename.pdf"'", "'"${currentdoc_basename}.pdf"'" ); t=1 } 1; END{ exit( !t )}' "tex/printout-template_EASY-A5-on-A4-sidebyside-simple.context" >"${printout_sss_basename}.context" \
|| die $? "[${currentdoc_basename}]: Error with 'awk' when creating sss printout!"
local log09file="${logfileprefix}log-09_printout-sss.log"
${contextbinary} "${printout_sss_basename}.context" \
1>"${log09file}" 2>&1 \
|| die_log $? "Error creating sss printout!" "${log09file}"
cp "${printout_sss_basename}.pdf" "${INITIAL_DIR}/${RESULT_DIRNAME}/${RESULT_PRINTOUT_SUBDIRNAME}/" \
|| die $? "Error copying printout PDF from temporary directory!"
echo "${RESULT_TYPE_PRINTOUT_PDF}${RESULT_SEPARATOR}${printout_sss_basename}.pdf" \
>>${RESULTLIST_FILE}
fi
fi
fi
# Extract cover page as a raster image file(s)
if [ ${coverimage} == "true" ]; then
which "pdftoppm" >"/dev/null" 2>&1
if [ $? -ne 0 ]; then
echo -e "${PRETXT_NOEXEC}${txt_docbase}: Cover not extracted as image; no 'pdftoppm'"
else
echo -e "${PRETXT_EXEC}${txt_docbase}: pdftoppm (extract cover as image)"
mkdir "${INITIAL_DIR}/${RESULT_DIRNAME}/${RESULT_IMAGE_SUBDIRNAME}" 2>"/dev/null"
local log10file="${logfileprefix}log-10_coverimage-extract.log"
pdftoppm -f 1 -singlefile -png -scale-to-x -1 -scale-to-y "${COVERIMAGE_HEIGHT}" \
"${currentdoc_basename}.pdf" "${currentdoc_basename}" \
1>"${log10file}" 2>&1 \
|| die_log $? "Error extracting cover image!" "${log10file}"
cp "${currentdoc_basename}.png" "${INITIAL_DIR}/${RESULT_DIRNAME}/${RESULT_IMAGE_SUBDIRNAME}/" \
|| die $? "Error copying ${currentdoc_basename}.png from temporary directory!"
echo "${RESULT_TYPE_IMAGE}${RESULT_SEPARATOR}${currentdoc_basename}.png" \
>>${RESULTLIST_FILE}
which "convert" >"/dev/null" 2>&1
if [ $? -ne 0 ]; then
echo -e "${PRETXT_NOEXEC}${txt_docbase}: Widened tagless cover image not created; no 'convert'"
else # create extended (closer to square) and tagless version of the image, too:
echo -e "${PRETXT_EXEC}${txt_docbase}: convert (create widened tagless cover image)"
if [[ ${currentdoc_basename} == "${ASTRAL_FNAME_PREFIX}"* ]]; then
convert "${currentdoc_basename}.png" \
-fill white -draw "rectangle 0,0 200,400" \
-gravity center \
-extent "${COVERIMAGE_AUTOWIDE_WIDTH}x${COVERIMAGE_HEIGHT}" \
${currentdoc_basename}${IMG_AUTOWIDENOTAGS_FNAME_POSTFIX}.png \
1>>"${log10file}" 2>&1 \
|| die_log $? "Error modifying cover image!" "${log10file}"
else
convert "${currentdoc_basename}.png"\
-fill white -draw "rectangle 0,0 1024,100" \
-gravity center \
-extent "${COVERIMAGE_AUTOWIDE_WIDTH}x${COVERIMAGE_HEIGHT}" \
${currentdoc_basename}${IMG_AUTOWIDENOTAGS_FNAME_POSTFIX}.png \
1>>"${log10file}" 2>&1 \
|| die_log $? "Error modifying cover image!" "${log10file}"
fi
cp "${currentdoc_basename}${IMG_AUTOWIDENOTAGS_FNAME_POSTFIX}.png" "${INITIAL_DIR}/${RESULT_DIRNAME}/${RESULT_IMAGE_SUBDIRNAME}/" \
|| die $? "Error copying ${currentdoc_basename}${IMG_AUTOWIDENOTAGS_FNAME_POSTFIX}.png from temporary directory!"
echo "${RESULT_TYPE_IMAGE}${RESULT_SEPARATOR}${currentdoc_basename}${IMG_AUTOWIDENOTAGS_FNAME_POSTFIX}.png" \
>>${RESULTLIST_FILE}
fi
fi
fi
# Check warnings in the logs
echo "${currentdoc_basename}" | grep "${LYRICSONLY_FNAMEPART}" >"/dev/null"
if [ $? -ne 0 ]; then # only check lilypond logs if not creating lyrics-only book
local lp_barwarning_count=$(grep -i "warning: barcheck" "${log01file}" | wc -l)
if [ "${lp_barwarning_count}" -gt "0" ]; then
echo -e "${PRETXT_DEBUG}${txt_docbase}: Lilypond bar check warnings: ${lp_barwarning_count}"
fi
fi
local allwarning_count=$(grep -i "warning" "${log07file}" | wc -l)
local fontwarning_count=$(grep -i "Font Warning" "${log07file}" | wc -l)
local overfull_count=$(grep -i "overfull" "${log07file}" | wc -l)
local underfull_count=$(grep -i "underfull" "${log07file}" | wc -l)
if [ "${allwarning_count}" -gt "0" ]; then
echo -e "${PRETXT_DEBUG}${txt_docbase}: TeX warnings - all: ${allwarning_count} (font: ${fontwarning_count})"
fi
if [ "${fontwarning_count}" -gt "20" ]; then
echo -e "${PRETXT_DEBUG}${txt_docbase}: ${C_RED}Too many font warnings! CHECK THE LOG!!${C_RESET}"
echo "Too many font warnings! There is a problem!" >>"${TOO_MANY_WARNINGS_FILE}"
fi
if [ "${overfull_count}" -gt "0" ] || [ "${underfull_count}" -gt "0" ]; then
echo -e "${PRETXT_DEBUG}${txt_docbase}: TeX fullness - overfull: ${overfull_count}, underfull: ${underfull_count}"
fi
} # END compile_document_sub
local document_basename="$1"
# setup some UI text with colors (if enabled):
local txt_docbase="${C_DGRAY}[${2}${1}${C_DGRAY}]${C_RESET}"
local txt_doctex="${C_DGRAY}[${2}${1}.tex${C_DGRAY}]${C_RESET}"
local txt_resultpdf="${C_DGRAY}[${2}${RESULT_DIRNAME}/${1}.pdf${C_DGRAY}]${C_RESET}"
local temp_dirname_twolevels="${TEMP_DIRNAME}/${document_basename}"
echo -e "${PRETXT_START}${txt_docbase}"
# Test if we are currently in the correct directory:
[ -f "./${document_basename}.tex" ] || die 1 "Not currently in the project's root directory!"
# Ensure the result directory exists:
mkdir -p "./${RESULT_DIRNAME}" 2>"/dev/null"
[ -d "./${RESULT_DIRNAME}" ] || die $? "Could not create the result directory ./${RESULT_DIRNAME}."
# Clean old build:
[ -d "${temp_dirname_twolevels}" ] && rm -R "${temp_dirname_twolevels}"/* 2>"/dev/null"
# Ensure the build directory exists:
mkdir -p "${temp_dirname_twolevels}" 2>"/dev/null"
[ -d "${temp_dirname_twolevels}" ] || die $? "Could not create the build directory ${temp_dirname_twolevels}."
# Copy/link the required files to the temp directory (lilypond-book will copy
# the rest of them):
mkdir "${temp_dirname_twolevels}/content"
cp -R "tex" "${temp_dirname_twolevels}/"
cp "tags.can" "${temp_dirname_twolevels}/"
# images are big (and not all of them are needed), so link instead of copy:
ln -s "${INITIAL_DIR}/content/img" "${temp_dirname_twolevels}/content/img"
echo -e "${PRETXT_EXEC}${txt_docbase}: lilypond-book"
# Run lilypond-book. It compiles images out of lilypond source code within tex
# files and outputs the modified .tex files and the musical staff images
# created by it to subdirectory ${temp_dirname_twolevels}. The directory
# (last level only) is created if it doesn't exist. Note the need to include
# the path for the log file, as we are not in the subdirectory yet.
local log01file="log-01_lilypond.log"
lilypond-book -f latex --latex-program=lualatex --output="${temp_dirname_twolevels}" \
--use-source-file-names \
"${document_basename}.tex" \
1>"${temp_dirname_twolevels}/${log01file}" 2>&1 \
|| die_log $? "Error running lilypond-book!" "${temp_dirname_twolevels}/${log01file}"
# Enter the temp directory. (Do rest of the steps there.)
cd "${temp_dirname_twolevels}" || die 1 "Cannot enter temporary directory!"
compile_document_sub "$1" "$2" ""
# Handle midi & mp3
if [ ${midifiles} == "true" ] || [ ${audiofiles} == "true" ]; then
which "python3" >"/dev/null" 2>&1 || die 1 "'python3' binary not found in path"
fi
if [ ${midifiles} == "true" ]; then
echo -e "${PRETXT_EXEC}${txt_docbase}: unilaiva-copy-audio (copy midi files)"
local cur_res_midi_subdirname="${RESULT_DIRNAME}/${RESULT_MIDI_SUBDIRNAME}/${document_basename}"
rm -R "${INITIAL_DIR}/${cur_res_midi_subdirname}"/* 2>"/dev/null"
mkdir -p "${INITIAL_DIR}/${cur_res_midi_subdirname}" 2>"/dev/null"
[ -d "${INITIAL_DIR}/${cur_res_midi_subdirname}" ] || die $? "Could not create the midi result directory ./${cur_res_midi_subdirname}."
local log11file="log-11_copy-midi.log"
# Execute the audio copy tool for midi files
${INITIAL_DIR}/tools/unilaiva-copy-audio.py3 --midi \
"${document_basename}.dep" "${INITIAL_DIR}/${cur_res_midi_subdirname}" \
1>"${log11file}" 2>&1 \
|| die_log $? "Error copying midi files to result directory" "${log11file}"
cp "${INITIAL_DIR}/metadata/audio-dirs-Readme.md" "${INITIAL_DIR}/${cur_res_midi_subdirname}/Readme.md"
echo "${RESULT_TYPE_MIDIDIR}${RESULT_SEPARATOR}${document_basename}" \
>>${RESULTLIST_FILE}
fi
if [ ${audiofiles} == "true" ]; then
echo -e "${PRETXT_EXEC}${txt_docbase}: unilaiva-copy-audio (encode audio)"
local cur_res_audio_subdirname="${RESULT_DIRNAME}/${RESULT_AUDIO_SUBDIRNAME}/${document_basename}"
rm -R "${INITIAL_DIR}/${cur_res_audio_subdirname}"/* 2>"/dev/null"
mkdir -p "${INITIAL_DIR}/${cur_res_audio_subdirname}" 2>"/dev/null"
[ -d "${INITIAL_DIR}/${cur_res_audio_subdirname}" ] || die $? "Could not create the audio result directory ./${cur_res_audio_subdirname}."
local log12file="log-12_encode-audio.log"
# Execute the audio copy tool for encoding audio files
${INITIAL_DIR}/tools/unilaiva-copy-audio.py3 --audio \
"${document_basename}.dep" "${INITIAL_DIR}/${cur_res_audio_subdirname}" \
1>"${log12file}" 2>&1 \
|| die_log $? "Error encoding audio files!" "${log12file}"
cp "${INITIAL_DIR}/metadata/audio-dirs-Readme.md" "${INITIAL_DIR}/${cur_res_audio_subdirname}/Readme.md"
echo "${RESULT_TYPE_AUDIODIR}${RESULT_SEPARATOR}${document_basename}" \
>>${RESULTLIST_FILE}
fi
# Create lyrics-only books, if so wanted
if [ ${lyricbooks} == "true" ]; then
grep '\\input{.*setup_.*\.tex}' "${document_basename}.tex" >"/dev/null"
if [ $? -ne 0 ]; then
echo -e "${PRETXT_NOEXEC}${txt_docbase}: Extra lyric-only book not created, as no '\input{setup_<...>}' in doc"
else
local ldoc_bname_pre=$(echo "${document_basename}" \
| awk '{ split($0, arr, "_A[0-9]"); print arr[1] }')
local ldoc_bname_post=$(echo "${document_basename}" "${ldoc_bname_pre}" \
| awk '{ split($1, arr, $2); print arr[2] }')
local lyricdoc_basename="${ldoc_bname_pre}${LYRICSONLY_FNAMEPART}${ldoc_bname_post}"
cat "${document_basename}.tex" \
| sed -e 's/\(\\input{.*setup_.*\.tex}\)/\\input{tex\/internal-lyricbook-presetup.tex}\1\\input{tex\/internal-lyricbook-postsetup.tex}/g' \
>>"${lyricdoc_basename}.tex"
rm ./idx_*.sxd ./idx_*.sbx 2>"/dev/null"
compile_document_sub "${lyricdoc_basename}" "$2" "lyric-"
fi
fi
# Create books for extra instruments, if so wanted. This is only done for
# books that have certain code words in their main document.
if [ ${extrainstrumentbooks} == "true" ]; then
grep '%%%CREATE_VERSION_CHARANGO%%%' "${document_basename}.tex" >"/dev/null"
if [ $? -eq 0 ]; then
cp "tex/lp-internal-common-head.ly" "tex/lp-internal-common-head_original.ly"
local chadoc_bname_pre=$(echo "${document_basename}" \
| awk '{ split($0, arr, "_A[0-9]"); print arr[1] }')
local chadoc_bname_post=$(echo "${document_basename}" "${chadoc_bname_pre}" \
| awk '{ split($1, arr, $2); print arr[2] }')
local charangodoc_basename="${chadoc_bname_pre}${CHARANGO_FNAMEPART}${chadoc_bname_post}"
cat "${INITIAL_DIR}/${document_basename}.tex" \
| sed -e 's/\(\\input{.*setup_.*\.tex}\)/\\input{tex\/internal-charangobook-presetup.tex}\1\\input{tex\/internal-charangobook-postsetup.tex}/g' \
>>"${charangodoc_basename}.tex"
cat "tex/lp-internal-common-head_original.ly" \
| sed -e 's/ul-chosen-tuning = #ul-guitar-tuning/ul-chosen-tuning = #ul-charango-tuning/g' \
>"tex/lp-internal-common-head.ly"
# TODO: Make better copy that copies everything but the img folder,
# so that other subfolders will be included too
cp "${INITIAL_DIR}/content"/*.tex "content/"
rm ./tmp* ./idx_*.sxd ./idx_*.sbx 2>"/dev/null"
rm ??/* 2>"/dev/null" # Remove earlier lp generated files, as otherwise .pdfs won't be replaced
local log01file="charango-log-01_lilypond.log"
local txt_docbasecharango="${C_DGRAY}[${2}${charangodoc_basename}${C_DGRAY}]${C_RESET}"
echo -e "${PRETXT_EXEC}${txt_docbasecharango}: lilypond-book"
lilypond-book -f latex --latex-program=lualatex --output="lp_charango_output" \
"${charangodoc_basename}.tex" \
1>"${log01file}" 2>&1 \
|| die_log $? "Error running lilypond-book!" "${log01file}"
cp -R "lp_charango_output"/* ./ && rm -R "lp_charango_output"
compile_document_sub "${charangodoc_basename}" "$2" "charango-"
fi
fi
# Clean up the compile directory: remove some temporary files.
rm ./tmp*.out ./tmp*.sxc 2>"/dev/null"
# Get out of ${temp_dirname_twolevels}:
cd "${INITIAL_DIR}" || die $? "Cannot return to the main directory."
echo -e "${PRETXT_DEBUG}${txt_docbase}: Build logs in ${temp_dirname_twolevels}/"
echo -e "${PRETXT_SUCCESS}${txt_resultpdf}: Compilation successful!"
} # END compile_document()
# Copies the result files to the deploy directory ${DEPLOY_DIRNAME}, or it's
# subdirectory depending on file type, if:
# - not inside Docker container
# - deploy is not forbidden by command line argument
# - deploy directory exists
# - ${RESULTLIST_FILE_IN_RESULTDIR} exists and contains data
# - result file's filename does not contain ${NODEPLOY_FNAMEPART}
deploy_results() {
[ -z "${IN_UNILAIVA_DOCKER_CONTAINER}" ] || return
[ "${deployfinal}" = "true" ] || return
if [ -f ${RESULTLIST_FILE_IN_RESULTDIR} ]; then
# Result file exists, there is something to deploy; add common files
# to the file if they don't already exist there
cd "${COMMONICON_DIRNAME}"
local tagicons=(unilaiva-tag-icon*)
cd "${INITIAL_DIR}"
for icon in "${tagicons[@]}"; do
local resultline="${RESULT_TYPE_COMMONICON}${RESULT_SEPARATOR}${icon}"
grep "${resultline}" "${RESULTLIST_FILE_IN_RESULTDIR}" >"/dev/null"
[ ${?} -ne 0 ] && echo "${resultline}" >>${RESULTLIST_FILE_IN_RESULTDIR}
done
cd "${COMMONICON_DIRNAME}"
local chapicons=(ul-default-chapter-*)
cd "${INITIAL_DIR}"
for icon in "${chapicons[@]}"; do
local resultline="${RESULT_TYPE_COMMONICON}${RESULT_SEPARATOR}${icon}"
grep "${resultline}" "${RESULTLIST_FILE_IN_RESULTDIR}" >"/dev/null"
[ ${?} -ne 0 ] && echo "${resultline}" >>${RESULTLIST_FILE_IN_RESULTDIR}
done
cd "${COMMONMETADATA_DIRNAME}"
local metadatafiles=(*.json)
cd "${INITIAL_DIR}"
for mdfile in "${metadatafiles[@]}"; do
local resultline="${RESULT_TYPE_COMMONMETADATA}${RESULT_SEPARATOR}${mdfile}"
grep "${resultline}" "${RESULTLIST_FILE_IN_RESULTDIR}" >"/dev/null"
[ ${?} -ne 0 ] && echo "${resultline}" >>${RESULTLIST_FILE_IN_RESULTDIR}
done
else
echo -e "${PRETXT_NODEPLOY}Nothing to deploy!"
return
fi
if [ ! -d "./${DEPLOY_DIRNAME}" ]; then
echo -e "${PRETXT_NODEPLOY}Resulting PDF files NOT copied to ./${DEPLOY_DIRNAME}/ (directory not found)"
return
fi
local idcontent_skippedcount=0
while IFS="${RESULT_SEPARATOR}" read -r ftype fname; do
if [[ ${fname} == *"${NODEPLOY_FNAMEPART}"* ]]; then
echo -e "${PRETXT_NODEPLOY}${fname} not deployed due to filename"
continue
fi
local deploydir="TOBECHANGED" # leave this here for safety in rm -R below
local resultisdir="false"
local resultdir="./${RESULT_DIRNAME}"
case "${ftype}" in
"${RESULT_TYPE_MAIN_PDF}")
deploydir="./${DEPLOY_DIRNAME}"
;;
"${RESULT_TYPE_LYRICONLY_PDF}")
deploydir="./${DEPLOY_DIRNAME}"
;;
"${RESULT_TYPE_PRINTOUT_PDF}")
resultdir="./${RESULT_DIRNAME}/${RESULT_PRINTOUT_SUBDIRNAME}"
deploydir="./${DEPLOY_DIRNAME}/${DEPLOY_PRINTOUT_SUBDIRNAME}"
;;
"${RESULT_TYPE_IMAGE}")
resultdir="./${RESULT_DIRNAME}/${RESULT_IMAGE_SUBDIRNAME}"
deploydir="./${DEPLOY_DIRNAME}/${DEPLOY_IMAGE_SUBDIRNAME}"
;;
"${RESULT_TYPE_MIDIDIR}")
deploydir="./${DEPLOY_DIRNAME}/${DEPLOY_MIDI_SUBDIRNAME}"
resultdir="./${RESULT_DIRNAME}/${RESULT_MIDI_SUBDIRNAME}"
resultisdir="true"
;;
"${RESULT_TYPE_AUDIODIR}")
deploydir="./${DEPLOY_DIRNAME}/${DEPLOY_AUDIO_SUBDIRNAME}"
resultdir="./${RESULT_DIRNAME}/${RESULT_AUDIO_SUBDIRNAME}"
resultisdir="true"
;;
"${RESULT_TYPE_COMMONICON}")
deploydir="./${DEPLOY_DIRNAME}/${DEPLOY_COMMONICON_SUBDIRNAME}"
resultdir="${COMMONICON_DIRNAME}"
;;
"${RESULT_TYPE_COMMONMETADATA}")
deploydir="./${DEPLOY_DIRNAME}/${DEPLOY_COMMONMETADATA_SUBDIRNAME}"
resultdir="${COMMONMETADATA_DIRNAME}"
;;
"${RESULT_TYPE_INFO}")
# Do nothing for info type
continue
;;
*)
echo -e "${PRETXT_NODEPLOY}${fname} not deployed due to ${C_RED}internal error${C_RESET}"
continue
;;
esac
mkdir -p "${deploydir}" 2>"/dev/null"
if [ ${resultisdir} == "true" ]; then
[ -d "${resultdir}/${fname}" ] || die 21 "Could not access directory ${fname} for deployment"
if dir_notempty "${resultdir}/${fname}"; then
# do not rm the root dir for possible shares to persist
rm -R "${deploydir}/${fname}"/* >"/dev/null" 2>&1
mkdir "${deploydir}/${fname}" 2>"/dev/null"
cp -R "${resultdir}/${fname}"/* "${deploydir}/${fname}/" >"/dev/null" 2>&1
[ $? -eq 0 ] || die 22 "Could not deploy directory ${deploydir}/${fname}"
fi
else
[ -f "${resultdir}/${fname}" ] || die 21 "Could not access ${fname} for deployment"
# Do not overwrite existing file in deploy dir, if it has the same content
# as the new file in the result dir. This way we don't invalidate caches,
# if the deploy dir is synced somewhere and/or served with a web server.
if [ -f "${deploydir}/${fname}" ]; then
which "sha256sum" >"/dev/null" 2>&1
[ ${?} -eq 0 ] || echo -e "${PRETXT_WARNING}'sha256sum' not found in path: file deployed even if no change"
local newhash="$(sha256sum -b ${resultdir}/${fname} | cut -d' ' -f1)"
local oldhash="$(sha256sum -b ${deploydir}/${fname} | cut -d' ' -f1)"
if [ ${newhash} = ${oldhash} ]; then # files identical
((idcontent_skippedcount++))
continue
fi
fi
# Actual copy:
cp "${resultdir}/${fname}" "${deploydir}/" >"/dev/null" 2>&1
[ $? -eq 0 ] || die 22 "Could not deploy ${fname}"
fi
echo -e "${PRETXT_DEPLOY}${deploydir}/${fname}"
done < "${RESULTLIST_FILE_IN_RESULTDIR}"
echo "${RESULT_TYPE_INFO}${RESULT_SEPARATOR}Deployed at: $(${datecmd} --rfc-3339=seconds)" \
>>${RESULTLIST_FILE_IN_RESULTDIR}
if [ ${idcontent_skippedcount} -gt 0 ]; then
echo -e "${PRETXT_NODEPLOY}Files skipped due to identical existing content: ${idcontent_skippedcount}"
fi
} # END deploy_results()
# Set defaults:
usedocker="true"
dockerrebuild="false"
deployfinal="true"
createprintouts="true"
coverimage="true"
cleantemp="true"
mainbook="true"
astralbooks="true"
partialbooks="true"
selections="true"
lyricbooks="true"
extrainstrumentbooks="true"
midifiles="true"
audiofiles="true"
gitpull="false"
parallel="true"
shellonly="false"