-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.cpp
3653 lines (3450 loc) · 133 KB
/
scripts.cpp
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
// -----------------------------------------------------------------------------
// scripts.cpp scripts.cpp
// -----------------------------------------------------------------------------
/**
* @file
* @brief This file defines all members of the @ref scripts namespace.
* @author Col. Walter E. Kurtz
* @version 2019-06-23
* @copyright GNU General Public License - Version 3.0
*/
// -----------------------------------------------------------------------------
// Includes Includes
// -----------------------------------------------------------------------------
#include <sys/types.h>
#include <sys/stat.h>
#include <fstream>
#include <iostream>
#include "message.h"
#include "scripts.h"
// -----------------------------------------------------------------------------
// Used namespaces Used namespaces
// -----------------------------------------------------------------------------
using namespace std;
// -----------------------------------------------------------------------------
// Definitions Definitions
// -----------------------------------------------------------------------------
namespace scripts
{
// ---------------
// printTripScript
// ---------------
/**
* Print the bash code that uses cdparanoia to rip audio CDs.
*/
bool printTripScript(const string& filename)
{
// open file for writing
ofstream cout( filename.c_str() );
// check if file has been opened
if ( !cout.is_open() )
{
// notify user
msg::err( msg::catq("unable to open file: ", filename) );
// signalize trouble
return false;
}
cout << "#!/bin/bash" << endl;
cout << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << "# settings settings" << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "# use dot as decimal separator" << endl;
cout << "LC_NUMERIC='en_US.UTF-8'" << endl;
cout << endl;
cout << "# set device" << endl;
cout << "DEVICE='/dev/sr0'" << endl;
cout << endl;
cout << "# set default speed" << endl;
cout << "SPEED=20" << endl;
cout << endl;
cout << "# number of seconds to wait before an idle process gets killed" << endl;
cout << "IDLEMAX=90" << endl;
cout << endl;
cout << "# the CD's table of contents will be written to this file" << endl;
cout << "TOCFILE='cd.toc'" << endl;
cout << endl;
cout << "# the offset to the CD's track numbers will be written to this file" << endl;
cout << "DBFILE='offset.db'" << endl;
cout << endl;
cout << "# terminal colors" << endl;
cout << " NONE=$(tput sgr0)" << endl;
cout << " RED=$(tput setaf 1)" << endl;
cout << " GREEN=$(tput setaf 2)" << endl;
cout << " YELLOW=$(tput setaf 3)" << endl;
cout << " BLUE=$(tput setaf 4)" << endl;
cout << "MAGENTA=$(tput setaf 5)" << endl;
cout << " CYAN=$(tput setaf 6)" << endl;
cout << " WHITE=$(tput setaf 7)" << endl;
cout << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << "# functions functions" << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "# ---------------------" << endl;
cout << "# show_version_and_exit" << endl;
cout << "# ---------------------" << endl;
cout << "#" << endl;
cout << "# This function shows the script's version and terminates the script." << endl;
cout << "#" << endl;
cout << "function show_version_and_exit()" << endl;
cout << "{" << endl;
cout << " echo" << endl;
cout << " echo 'version 2016-11-20.1'" << endl;
cout << " echo" << endl;
cout << " exit 1" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ------------------" << endl;
cout << "# show_help_and_exit" << endl;
cout << "# ------------------" << endl;
cout << "#" << endl;
cout << "# This function shows the script's help and terminates it." << endl;
cout << "#" << endl;
cout << "function show_help_and_exit()" << endl;
cout << "{" << endl;
cout << " echo" << endl;
cout << " echo \"NAME\"" << endl;
cout << " echo \" trip - test rip\"" << endl;
cout << " echo" << endl;
cout << " echo \"SYNOPSIS\"" << endl;
cout << " echo \" trip [options]\"" << endl;
cout << " echo" << endl;
cout << " echo \"DESCRIPTION\"" << endl;
cout << " echo \" trip uses cdparanoia to rip each track of an audio CD\"" << endl;
cout << " echo \" into its own file.\"" << endl;
cout << " echo" << endl;
cout << " echo \"OPTIONS\"" << endl;
cout << " echo \" -A get the track number offset from the database file\"" << endl;
cout << " echo \" -a get the track number offset from the highest filename\"" << endl;
cout << " echo \" -d <dev> rip from device <dev> (default: $DEVICE)\"" << endl;
cout << " echo \" -h show help and exit\"" << endl;
cout << " echo \" -o <num> add an offset of <num> to each track number\"" << endl;
cout << " echo \" -s <num> set device speed to <num> (default: $SPEED)\"" << endl;
cout << " echo \" -t <num> stop ripping the current track if the file size didn't\"" << endl;
cout << " echo \" change within the last <num> seconds (default: $IDLEMAX)\"" << endl;
cout << " echo \" -v show version and exit\"" << endl;
cout << " echo \" -z retry to rip empty files\"" << endl;
cout << " echo" << endl;
cout << " exit 1" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# failmsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints a fail message via stderr." << endl;
cout << "#" << endl;
cout << "function failmsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${RED}[FAIL]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# warnmsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints a warn message via stderr." << endl;
cout << "#" << endl;
cout << "function warnmsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${YELLOW}[WARN]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# infomsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints an info message via stderr." << endl;
cout << "#" << endl;
cout << "function infomsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${BLUE}[INFO]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# donemsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints a done message via stderr." << endl;
cout << "#" << endl;
cout << "function donemsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${GREEN}[DONE]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# --------------------" << endl;
cout << "# get_number_of_tracks" << endl;
cout << "# --------------------" << endl;
cout << "#" << endl;
cout << "# $1 toc file" << endl;
cout << "#" << endl;
cout << "function get_number_of_tracks()" << endl;
cout << "{" << endl;
cout << " sed --quiet \\" << endl;
cout << " --regexp-extended \\" << endl;
cout << " --expression=\"" << endl;
cout << " /^=====/,/^TOTAL/{" << endl;
cout << endl;
cout << " s/^[[:space:]]*[[:digit:]]+\\.[[:space:]]+.*/./p" << endl;
cout << endl;
cout << " }" << endl;
cout << " \" \"$1\" \\" << endl;
cout << " | wc --lines" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ------------------------" << endl;
cout << "# get_offset_from_filename" << endl;
cout << "# ------------------------" << endl;
cout << "#" << endl;
cout << "#" << endl;
cout << "#" << endl;
cout << "function get_offset_from_filename()" << endl;
cout << "{" << endl;
cout << " # find wav files in the current directory" << endl;
cout << " find -maxdepth '1' \\" << endl;
cout << " -type 'f' \\" << endl;
cout << " -regextype 'posix-extended' \\" << endl;
cout << " -regex '.+/track[[:digit:]]+\\.cdda\\.wav$' \\" << endl;
cout << " -printf '%P\\n' \\" << endl;
cout << " | sort --reverse \\" << endl;
cout << " | head --lines='1' \\" << endl;
cout << " | sed --quiet \\" << endl;
cout << " --regexp-extended \\" << endl;
cout << " --expression=\"" << endl;
cout << endl;
cout << " # crop digits" << endl;
cout << " s/^track([[:digit:]]+)\\.cdda\\.wav$/\\1/" << endl;
cout << endl;
cout << " #remove leading zeros" << endl;
cout << " s/0*([[:digit:]]+)/\\1/" << endl;
cout << endl;
cout << " # branch to end of script if all s commands failed" << endl;
cout << " T" << endl;
cout << endl;
cout << " # print number" << endl;
cout << " p" << endl;
cout << " \"" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ---------------------" << endl;
cout << "# get_offset_from_dbase" << endl;
cout << "# ---------------------" << endl;
cout << "#" << endl;
cout << "# $1 filename of the database" << endl;
cout << "# $2 hash value of the toc file" << endl;
cout << "#" << endl;
cout << "function get_offset_from_dbase()" << endl;
cout << "{" << endl;
cout << " # get last matching offset" << endl;
cout << " tac \"$1\" 2>'/dev/null' | sed -nre \"s/^$2 ([[:digit:]]+)/\\1/p ; T ; q\"" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ---------------" << endl;
cout << "# update_log_file" << endl;
cout << "# ---------------" << endl;
cout << "#" << endl;
cout << "# $1 log file" << endl;
cout << "# $2 track number" << endl;
cout << "#" << endl;
cout << "function update_log_file()" << endl;
cout << "{" << endl;
cout << " # check if log file exists" << endl;
cout << " if [ ! -f \"$1\" ]; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " warnmsg \"unable to open log file: \\\"$1\\\"\"" << endl;
cout << endl;
cout << " # exit function" << endl;
cout << " return" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # create track number tag" << endl;
cout << " TRACK=$(printf 'TRACK %03d' \"$2\")" << endl;
cout << endl;
cout << " # get current date" << endl;
cout << " RIPPED=$(date +'%Y-%m-%d %H:%M:%S')" << endl;
cout << " " << endl;
cout << " # update log file" << endl;
cout << " sed -i -re \"s/^ *(\\(== PROGRESS ==.+==\\)) *$/$TRACK @ $RIPPED \\1/\" \"$1\"" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ---------" << endl;
cout << "# rip_track" << endl;
cout << "# ---------" << endl;
cout << "#" << endl;
cout << "# $1 track number" << endl;
cout << "# $2 output filename" << endl;
cout << "#" << endl;
cout << "function rip_track()" << endl;
cout << "{" << endl;
cout << " # show progress" << endl;
cout << " infomsg \"ripping track $1 to file \\\"$2\\\"\\n\"" << endl;
cout << endl;
cout << " # set the name of the log file" << endl;
cout << " LOGFILE=\"$2.log\"" << endl;
cout << endl;
cout << " # kill cdparanoia if this script gets killed" << endl;
cout << " trap '{ kill $PID ; truncate -s0 \"$2\" \"$LOGFILE\" ; } &>/dev/null' EXIT" << endl;
cout << endl;
cout << " # rip given track in background" << endl;
cout << " cdparanoia --log-summary=\"$LOGFILE\" \\" << endl;
cout << " --force-cdrom-device \"$DEVICE\" \\" << endl;
cout << " --force-read-speed \"$SPEED\" \\" << endl;
cout << " --never-skip='100' \\" << endl;
cout << " --abort-on-skip \\" << endl;
cout << " \"$1\" \\" << endl;
cout << " \"$2\" &" << endl;
cout << endl;
cout << " # get process ID" << endl;
cout << " PID=$!" << endl;
cout << endl;
cout << " # output file's size" << endl;
cout << " FILESIZE=0" << endl;
cout << " LASTSIZE=0" << endl;
cout << " IDLETIME=0" << endl;
cout << endl;
cout << " # while cdparanoia is running" << endl;
cout << " while kill -0 \"$PID\" &>'/dev/null'" << endl;
cout << " do" << endl;
cout << endl;
cout << " # delay" << endl;
cout << " sleep 1" << endl;
cout << endl;
cout << " # check if output file is present" << endl;
cout << " if [ -f \"$2\" ]; then" << endl;
cout << endl;
cout << " # get current file size" << endl;
cout << " FILESIZE=$(stat --printf='%s' \"$2\")" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # check if some data has been ripped" << endl;
cout << " if (( FILESIZE == LASTSIZE )) ; then" << endl;
cout << endl;
cout << " # increase idle time" << endl;
cout << " (( IDLETIME += 1 ))" << endl;
cout << endl;
cout << " # idle for too long" << endl;
cout << " if (( IDLETIME > IDLEMAX )) ; then" << endl;
cout << endl;
cout << " # terminate cdparanoia" << endl;
cout << " kill -SIGTERM \"$PID\" &>'/dev/null'" << endl;
cout << endl;
cout << " # wait for child processes to terminate" << endl;
cout << " wait &>'/dev/null'" << endl;
cout << endl;
cout << " # reset wav file" << endl;
cout << " truncate -s0 \"$2\"" << endl;
cout << endl;
cout << " # update log file" << endl;
cout << " echo '(== PROGRESS == [~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~| 000000 00 ] == ERROR ==)' >> \"$LOGFILE\"" << endl;
cout << endl;
cout << " # add some space" << endl;
cout << " echo -e \"\\n\"" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"unable to rip track $1\\n\"" << endl;
cout << endl;
cout << " # exit loop" << endl;
cout << " break" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " else" << endl;
cout << endl;
cout << " # update last size" << endl;
cout << " LASTSIZE=\"$FILESIZE\"" << endl;
cout << endl;
cout << " # reset idle time" << endl;
cout << " IDLETIME=0" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " done" << endl;
cout << endl;
cout << " # disable trap" << endl;
cout << " trap - EXIT" << endl;
cout << endl;
cout << " # append paranoia tag to log file" << endl;
cout << " update_log_file \"$LOGFILE\" \"$1\"" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << "# options options" << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "# set default values" << endl;
cout << "NOSIZE='skip'" << endl;
cout << "OFFGET='none'" << endl;
cout << "OFFSET=0" << endl;
cout << endl;
cout << "# check passed options" << endl;
cout << "while getopts ':Aad:ho:s:t:vz' OPTION \"$@\"" << endl;
cout << "do" << endl;
cout << endl;
cout << " case \"$OPTION\" in" << endl;
cout << endl;
cout << " 'A') OFFGET='dbase'" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " 'a') OFFGET='files'" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " 'd') DEVICE=\"$OPTARG\"" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " 'h') show_help_and_exit" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " 'o') OFFGET='param'" << endl;
cout << " OFFSET=\"$OPTARG\"" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " 's') SPEED=\"$OPTARG\"" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " 't') IDLEMAX=\"$OPTARG\"" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " 'v') show_version_and_exit" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " 'z') NOSIZE='retry'" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " '?') failmsg \"unknown option: -$OPTARG\"" << endl;
cout << " exit 1" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " ':') failmsg \"missing argument: -$OPTARG <argument>\"" << endl;
cout << " exit 1" << endl;
cout << " ;;" << endl;
cout << endl;
cout << " esac" << endl;
cout << endl;
cout << "done" << endl;
cout << endl;
cout << "# get number of positional parameters" << endl;
cout << "PCOUNT=$(( $# - OPTIND + 1 ))" << endl;
cout << endl;
cout << "# check number of positional parameters" << endl;
cout << "if (( PCOUNT != 0 )); then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg 'no positional parameters allowed'" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << "fi" << endl;
cout << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << "# commands commands" << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "# get offset from wav files" << endl;
cout << "if [ \"$OFFGET\" == 'files' ] ; then" << endl;
cout << endl;
cout << " # get current offset" << endl;
cout << " OFFSET=$(get_offset_from_filename)" << endl;
cout << endl;
cout << " # check offset" << endl;
cout << " if [ -z \"$OFFSET\" ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg 'unable to detect offset automatically'" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << "fi" << endl;
cout << endl;
cout << "# reset toc file" << endl;
cout << "truncate -s0 \"$TOCFILE\" &>'/dev/null'" << endl;
cout << endl;
cout << "# show toc and create toc file" << endl;
cout << "cdparanoia --force-cdrom-device \"$DEVICE\" \\" << endl;
cout << " --query \\" << endl;
cout << " 2>&1 \\" << endl;
cout << "| tee \"$TOCFILE\"" << endl;
cout << endl;
cout << "# check toc file" << endl;
cout << "if [ ! -s \"$TOCFILE\" ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg 'unable to create toc file'" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << "fi" << endl;
cout << endl;
cout << "# create hash value of the created toc file" << endl;
cout << "CHKSUM=$(sha1sum \"$TOCFILE\" | sed -re 's/ .+//')" << endl;
cout << endl;
cout << "# get offset from db file" << endl;
cout << "if [ \"$OFFGET\" == 'dbase' ] ; then" << endl;
cout << endl;
cout << " # get current offset" << endl;
cout << " OFFSET=$(get_offset_from_dbase \"$DBFILE\" \"$CHKSUM\")" << endl;
cout << endl;
cout << " # check offset" << endl;
cout << " if [ -z \"$OFFSET\" ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg 'unable to get offset from database'" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << "fi" << endl;
cout << endl;
cout << "# check if a specific offset was given" << endl;
cout << "if [ \"$OFFGET\" != 'none' ] ; then" << endl;
cout << endl;
cout << " # check if db file is already pressent" << endl;
cout << " if [ -s \"$DBFILE\" ] ; then" << endl;
cout << endl;
cout << " # remove db entries for this hash value" << endl;
cout << " sed --in-place \\" << endl;
cout << " --regexp-extended \\" << endl;
cout << " --expression=\"/^$CHKSUM/d\" \\" << endl;
cout << " \"$DBFILE\"" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # save current offset" << endl;
cout << " echo \"$CHKSUM $OFFSET\" >> \"$DBFILE\"" << endl;
cout << endl;
cout << "fi" << endl;
cout << endl;
cout << "# get number of audio tracks" << endl;
cout << "TRACKS=$(get_number_of_tracks \"$TOCFILE\")" << endl;
cout << endl;
cout << "# rip all tracks" << endl;
cout << "for (( TNO = 1 ; TNO <= TRACKS ; TNO++ ))" << endl;
cout << "do" << endl;
cout << endl;
cout << " # create output filename" << endl;
cout << " FILENAME=$(printf 'track%03d.cdda.wav' \"$(( OFFSET + TNO ))\")" << endl;
cout << endl;
cout << " # always skip non-zero files" << endl;
cout << " if [ -s \"$FILENAME\" ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " warnmsg \"skipping track $TNO (file already exists: \\\"$FILENAME\\\")\\n\"" << endl;
cout << endl;
cout << " # next cycle " << endl;
cout << " continue" << endl;
cout << endl;
cout << " # check what to do with empty files" << endl;
cout << " elif [ -f \"$FILENAME\" ] && [ \"$NOSIZE\" != 'retry' ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " warnmsg \"skipping track $TNO (file already exists: \\\"$FILENAME\\\")\\n\"" << endl;
cout << endl;
cout << " # next cycle " << endl;
cout << " continue" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # rip given track" << endl;
cout << " rip_track \"$TNO\" \"$FILENAME\"" << endl;
cout << endl;
cout << "done" << endl;
cout << endl;
cout << "# show log summary" << endl;
cout << "find -maxdepth '1' \\" << endl;
cout << " -type 'f' \\" << endl;
cout << " -regextype 'posix-extended' \\" << endl;
cout << " -regex '.+\\.[Ll][Oo][Gg]$' \\" << endl;
cout << " -print0 \\" << endl;
cout << "| sort -z \\" << endl;
cout << "| xargs -0 cat \\" << endl;
cout << "| grep 'PROGRESS' \\" << endl;
cout << "| nl" << endl;
cout << endl;
cout << "# insert an empty line at the end" << endl;
cout << "echo" << endl;
cout << endl;
cout << "# open tray" << endl;
cout << "eject \"$DEVICE\" &" << endl;
cout << endl;
cout << "# signalize success" << endl;
cout << "exit 0" << endl;
// check state of stream
if ( !cout )
{
// notify user
msg::err( msg::catq("unable to create script: ", filename) );
// close file
cout.close();
// signalize trouble
return false;
}
// close file
cout.close();
// signalize success
return true;
}
// ---------------------
// printBack2TrackScript
// ---------------------
/**
* Print the bash code that decodes flac files back to wav.
*/
bool printBack2TrackScript(const string& filename)
{
// open file for writing
ofstream cout( filename.c_str() );
// check if file has been opened
if ( !cout.is_open() )
{
// notify user
msg::err( msg::catq("unable to open file: ", filename) );
// signalize trouble
return false;
}
cout << "#!/bin/bash" << endl;
cout << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << "# settings settings" << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "# use dot as decimal separator" << endl;
cout << "LC_NUMERIC='en_US.UTF-8'" << endl;
cout << endl;
cout << "# terminal colors" << endl;
cout << " NONE=$(tput sgr0)" << endl;
cout << " RED=$(tput setaf 1)" << endl;
cout << " GREEN=$(tput setaf 2)" << endl;
cout << " YELLOW=$(tput setaf 3)" << endl;
cout << " BLUE=$(tput setaf 4)" << endl;
cout << "MAGENTA=$(tput setaf 5)" << endl;
cout << " CYAN=$(tput setaf 6)" << endl;
cout << " WHITE=$(tput setaf 7)" << endl;
cout << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << "# functions functions" << endl;
cout << "# ------------------------------------------------------------------------------" << endl;
cout << endl;
cout << "# ---------------------" << endl;
cout << "# show_version_and_exit" << endl;
cout << "# ---------------------" << endl;
cout << "#" << endl;
cout << "# This function shows the script's version and terminates the script." << endl;
cout << "#" << endl;
cout << "function show_version_and_exit()" << endl;
cout << "{" << endl;
cout << " echo" << endl;
cout << " echo 'version 2016-11-20.1'" << endl;
cout << " echo" << endl;
cout << " exit 1" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ------------------" << endl;
cout << "# show_help_and_exit" << endl;
cout << "# ------------------" << endl;
cout << "#" << endl;
cout << "# This function shows the script's help and terminates it." << endl;
cout << "#" << endl;
cout << "function show_help_and_exit()" << endl;
cout << "{" << endl;
cout << " echo" << endl;
cout << " echo \"NAME\"" << endl;
cout << " echo \" back2track - convert flac files back to wav\"" << endl;
cout << " echo" << endl;
cout << " echo \"SYNOPSIS\"" << endl;
cout << " echo \" back2track [options] [{directory|text file|flac file(s)}]\"" << endl;
cout << " echo" << endl;
cout << " echo \"DESCRIPTION\"" << endl;
cout << " echo \" back2track will decode and rename flac files from a given directory\"" << endl;
cout << " echo \" in order that the wav files can be re-encoded by a ripgen script.\"" << endl;
cout << " echo" << endl;
cout << " echo \"OPTIONS\"" << endl;
cout << " echo \" -h show help and exit\"" << endl;
cout << " echo \" -n <num> always use serial number starting with <num>\"" << endl;
cout << " echo \" -t use TRACKNUMBER if COMPILATIONINDEX is missing\"" << endl;
cout << " echo \" -T always use TRACKNUMBER\"" << endl;
cout << " echo \" -v show version and exit\"" << endl;
cout << " echo" << endl;
cout << " exit 1" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# failmsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints a fail message via stderr." << endl;
cout << "#" << endl;
cout << "function failmsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${RED}[FAIL]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# warnmsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints a warn message via stderr." << endl;
cout << "#" << endl;
cout << "function warnmsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${YELLOW}[WARN]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# infomsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints an info message via stderr." << endl;
cout << "#" << endl;
cout << "function infomsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${BLUE}[INFO]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------" << endl;
cout << "# donemsg" << endl;
cout << "# -------" << endl;
cout << "#" << endl;
cout << "# This function prints a done message via stderr." << endl;
cout << "#" << endl;
cout << "function donemsg()" << endl;
cout << "{" << endl;
cout << " # push to stderr" << endl;
cout << " echo -e \"${GREEN}[DONE]${NONE} $1\" 1>&2" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ------------" << endl;
cout << "# escape_lines" << endl;
cout << "# ------------" << endl;
cout << "#" << endl;
cout << "# no arguments needed: use as filter" << endl;
cout << "#" << endl;
cout << "function escape_lines()" << endl;
cout << "{" << endl;
cout << " # prepare (NL separated) lines so they can pass 'read' unchanged" << endl;
cout << " sed --regexp-extended \\" << endl;
cout << " --expression=\"" << endl;
cout << " # skip empty lines" << endl;
cout << " /^[[:space:]]*$/ { d }" << endl;
cout << endl;
cout << " # skip comments" << endl;
cout << " /^[[:space:]]*#/ { d }" << endl;
cout << endl;
cout << " # escape each internal escape character (colon) first" << endl;
cout << " s/:/:c/g" << endl;
cout << endl;
cout << " # escape characters treated special by 'read'" << endl;
cout << " s/\\x09/:t/g" << endl;
cout << " s/\\x20/:s/g" << endl;
cout << " s/\\\\\\\\/:b/g" << endl;
cout << " \"" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -------------" << endl;
cout << "# unescape_line" << endl;
cout << "# -------------" << endl;
cout << "#" << endl;
cout << "# no arguments needed: use as filter" << endl;
cout << "#" << endl;
cout << "function unescape_line()" << endl;
cout << "{" << endl;
cout << " sed --regexp-extended \\" << endl;
cout << " --expression=\"" << endl;
cout << " # unescape special characters first" << endl;
cout << " s/:t/\\x09/g" << endl;
cout << " s/:s/\\x20/g" << endl;
cout << " s/:b/\\\\\\\\/g" << endl;
cout << endl;
cout << " # unescape each internal escape character at the end" << endl;
cout << " s/:c/:/g" << endl;
cout << " \"" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ----------" << endl;
cout << "# is_integer" << endl;
cout << "# ----------" << endl;
cout << "#" << endl;
cout << "# $1 number" << endl;
cout << "#" << endl;
cout << "function is_integer()" << endl;
cout << "{" << endl;
cout << " # no argument given" << endl;
cout << " [ -z \"$1\" ] && return 1" << endl;
cout << endl;
cout << " # try to print as integer" << endl;
cout << " printf '%d' \"$1\" &>'/dev/null' || return 1" << endl;
cout << endl;
cout << " # integer found" << endl;
cout << " return 0" << endl;
cout << "}" << endl;
cout << endl;
cout << "# ----------" << endl;
cout << "# get_number" << endl;
cout << "# ----------" << endl;
cout << "#" << endl;
cout << "# This function returns the number for the name" << endl;
cout << "# of the next wav file to create." << endl;
cout << "#" << endl;
cout << "# $1 flac file" << endl;
cout << "#" << endl;
cout << "function get_number()" << endl;
cout << "{" << endl;
cout << " # reset return value" << endl;
cout << " TRACKNUMBER=''" << endl;
cout << endl;
cout << " if [ \"$NUMSOURCE\" == 'COUNTER' ] ; then" << endl;
cout << endl;
cout << " # set counter value" << endl;
cout << " TRACKNUMBER=\"$NUMCOUNTER\"" << endl;
cout << endl;
cout << " elif [ \"$NUMSOURCE\" == 'TRACK_ALWAYS' ] ; then" << endl;
cout << endl;
cout << " # get track number from flac file" << endl;
cout << " TRACKNUMBER=$(metaflac --show-tag='TRACKNUMBER' \"$1\" 2>'/dev/null' \\" << endl;
cout << " | sed -nre 's/^[^=]+=0*([[:digit:]]+)$/\\1/p')" << endl;
cout << endl;
cout << " elif [ \"$NUMSOURCE\" == 'TRACK_IF_MISSING' ] ; then" << endl;
cout << endl;
cout << " # get compilation index from flac file first" << endl;
cout << " TRACKNUMBER=$(metaflac --show-tag='COMPILATIONINDEX' \"$1\" 2>'/dev/null' \\" << endl;
cout << " | sed -nre 's/^[^=]+=0*([[:digit:]]+)$/\\1/p')" << endl;
cout << endl;
cout << " # check if COMPILATIONINDEX is missing" << endl;
cout << " if [ -z \"$TRACKNUMBER\" ] ; then" << endl;
cout << endl;
cout << " # get track number from flac file" << endl;
cout << " TRACKNUMBER=$(metaflac --show-tag='TRACKNUMBER' \"$1\" 2>'/dev/null' \\" << endl;
cout << " | sed -nre 's/^[^=]+=0*([[:digit:]]+)$/\\1/p')" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " elif [ \"$NUMSOURCE\" == 'INDEX' ] ; then" << endl;
cout << endl;
cout << " # get compilation index from flac file" << endl;
cout << " TRACKNUMBER=$(metaflac --show-tag='COMPILATIONINDEX' \"$1\" 2>'/dev/null' \\" << endl;
cout << " | sed -nre 's/^[^=]+=0*([[:digit:]]+)$/\\1/p')" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # check number" << endl;
cout << " if [ -z \"$TRACKNUMBER\" ] ; then" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " return 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # return value" << endl;
cout << " printf '%d' \"$TRACKNUMBER\"" << endl;
cout << endl;
cout << " # signalize success" << endl;
cout << " return 0" << endl;
cout << "}" << endl;
cout << endl;
cout << "# --------" << endl;
cout << "# flac2wav" << endl;
cout << "# --------" << endl;
cout << "#" << endl;
cout << "# This function converts a flac file back to wav and renames" << endl;
cout << "# it in the same way cdparamoia would do." << endl;
cout << "#" << endl;
cout << "# $1 flac file" << endl;
cout << "#" << endl;
cout << "function flac2wav()" << endl;
cout << "{" << endl;
cout << " # get base name" << endl;
cout << " BASENAME=$(basename \"$1\")" << endl;
cout << endl;
cout << " # get number for next filename" << endl;
cout << " TRACKNUMBER=$(get_number \"$1\")" << endl;
cout << endl;
cout << " # check number" << endl;
cout << " if [ -z \"$TRACKNUMBER\" ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"unable to get track number: \\\"$BASENAME\\\"\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " return 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # check minimum value" << endl;
cout << " if (( TRACKNUMBER < 1 )) ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"invalid track number found: \\\"$TRACKNUMBER\\\" ($BASENAME)\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " return 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # set 'original' filename" << endl;
cout << " TRACKNAME=$(printf 'track%03d.cdda.wav' \"$TRACKNUMBER\")" << endl;
cout << endl;
cout << " # check file" << endl;
cout << " if [ -s \"$TRACKNAME\" ] ; then" << endl;
cout << endl;
cout << " # notify user" << endl;
cout << " failmsg \"file already exists: $TRACKNAME\"" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " return 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # show progress" << endl;
cout << " infomsg \"decoding file: $TRACKNAME <-- $BASENAME\"" << endl;
cout << endl;
cout << " # try to decode flac to wav" << endl;
cout << " if ! flac --totally-silent --decode --output-name=\"$TRACKNAME\" \"$1\" ; then" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " return 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " # step counter" << endl;
cout << " (( NUMCOUNTER += 1 ))" << endl;
cout << endl;
cout << " # signalize success" << endl;
cout << " return 0" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -----------------" << endl;
cout << "# operate_directory" << endl;
cout << "# -----------------" << endl;
cout << "#" << endl;
cout << "# $1 directory" << endl;
cout << "#" << endl;
cout << "function operate_directory()" << endl;
cout << "{" << endl;
cout << " find \"$1\" \\" << endl;
cout << " -maxdepth '1' \\" << endl;
cout << " -type 'f' \\" << endl;
cout << " -regextype 'posix-extended' \\" << endl;
cout << " -regex '.+\\.[Ff][Ll][Aa][Cc]$' \\" << endl;
cout << " -print0 \\" << endl;
cout << " | sort -z \\" << endl;
cout << " | while read -rd $'\\0' FILENAME" << endl;
cout << " do" << endl;
cout << endl;
cout << " # try to decode flac file" << endl;
cout << " if ! flac2wav \"$FILENAME\" ; then" << endl;
cout << endl;
cout << " # signalize trouble" << endl;
cout << " exit 1" << endl;
cout << endl;
cout << " fi" << endl;
cout << endl;
cout << " done" << endl;
cout << endl;
cout << " # signalize success" << endl;
cout << " return 0" << endl;
cout << "}" << endl;
cout << endl;
cout << "# -----------------" << endl;
cout << "# operate_text_file" << endl;
cout << "# -----------------" << endl;
cout << "#" << endl;
cout << "# $1 text file" << endl;
cout << "#" << endl;
cout << "function operate_text_file()" << endl;
cout << "{" << endl;
cout << " # read lines from text file" << endl;
cout << " while read ESCAPED" << endl;
cout << " do" << endl;
cout << endl;
cout << " # get unescape version" << endl;
cout << " FILENAME=$(unescape_line <<< \"$ESCAPED\")" << endl;
cout << endl;
cout << " # try to decode flac file" << endl;
cout << " if ! flac2wav \"$FILENAME\" ; then" << endl;
cout << endl;
cout << " # signalize trouble" << endl;