-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrotten.sh
executable file
·3866 lines (3039 loc) · 131 KB
/
rotten.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
#!/usr/bin/env bash
[[ "$PATH" != *"/usr/local/bin"* ]] && [[ -e "/usr/local/bin" ]] && export PATH=/usr/local/bin:"$PATH"
[[ "$PATH" != *"/usr/bin"* ]] && [[ -e "/usr/bin" ]] && export PATH=/usr/bin:"$PATH"
[[ "$PATH" != *"/bin:"* ]] && [[ -e "/bin" ]] && export PATH=/bin:"$PATH"
# Rotten.
_test() { false; }
_setup() { false; }
_typeDep() { true; }
_getDep() { true; }
_wantGetDep() { true; }
_if_cygwin() { false; }
_user_log-ub() { false; }
_collect() { false; }
_enter() { false; }
#### from messaging.sh
_color_demo() {
_messagePlain_request _color_demo
_messagePlain_nominal _color_demo
_messagePlain_probe _color_demo
_messagePlain_probe_expr _color_demo
_messagePlain_probe_var ubiquitousBashIDshort
_messagePlain_good _color_demo
_messagePlain_warn _color_demo
_messagePlain_bad _color_demo
_messagePlain_probe_cmd echo _color_demo
_messagePlain_probe_quoteAddDouble echo _color_demo
_messagePlain_probe_quoteAddSingle echo _color_demo
_messageNormal _color_demo
_messageError _color_demo
_messageDELAYipc _color_demo
_messageProcess _color_demo
}
_color_end() {
( [[ "$current_scriptedIllustrator_markup" == "html" ]] || [[ "$current_scriptedIllustrator_markup" == "mediawiki" ]] ) && echo -e -n '</span>'
[[ "$current_scriptedIllustrator_markup" == "" ]] && echo -e -n ' \E[0m'
}
_color_begin_request() {
echo -e -n '\E[0;35m '
}
_color_begin_nominal() {
echo -e -n '\E[0;36m '
}
_color_begin_probe() {
echo -e -n '\E[0;34m '
}
_color_begin_probe_noindent() {
echo -e -n '\E[0;34m'
}
_color_begin_good() {
echo -e -n '\E[0;32m '
}
_color_begin_warn() {
echo -e -n '\E[1;33m '
}
_color_begin_bad() {
echo -e -n '\E[0;31m '
}
_color_begin_Normal() {
echo -e -n '\E[1;32;46m '
}
_color_begin_Error() {
echo -e -n '\E[1;33;41m '
}
_color_begin_DELAYipc() {
echo -e -n '\E[1;33;47m '
}
#Purple. User must do something manually to proceed. NOT to be used for dependency installation requests - use probe, bad, and fail statements for that.
_messagePlain_request() {
_color_begin_request
echo -n "$@"
_color_end
echo
return 0
}
#Cyan. Harmless status messages.
#"generic/ubiquitousheader.sh"
_messagePlain_nominal() {
_color_begin_nominal
echo -n "$@"
_color_end
echo
return 0
}
#Blue. Diagnostic instrumentation.
#"generic/ubiquitousheader.sh"
_messagePlain_probe() {
_color_begin_probe
#_color_begin_probe_noindent
echo -n "$@"
_color_end
echo
return 0
}
_messagePlain_probe_noindent() {
#_color_begin_probe
_color_begin_probe_noindent
echo -n "$@"
_color_end
echo
return 0
}
#Blue. Diagnostic instrumentation.
#"generic/ubiquitousheader.sh"
_messagePlain_probe_expr() {
_color_begin_probe
echo -e -n "$@"
_color_end
echo
return 0
}
#Blue. Diagnostic instrumentation.
#"generic/ubiquitousheader.sh"
_messagePlain_probe_var() {
_color_begin_probe
echo -n "$1"'= '
eval echo -e -n \$"$1"
_color_end
echo
return 0
}
_messageVar() {
_messagePlain_probe_var "$@"
}
#Green. Working as expected.
#"generic/ubiquitousheader.sh"
_messagePlain_good() {
_color_begin_good
echo -n "$@"
_color_end
echo
return 0
}
#Yellow. May or may not be a problem.
#"generic/ubiquitousheader.sh"
_messagePlain_warn() {
_color_begin_warn
echo -n "$@"
_color_end
echo
return 0
}
#Red. Will result in missing functionality, reduced performance, etc, but not necessarily program failure overall.
_messagePlain_bad() {
_color_begin_bad
echo -n "$@"
_color_end
echo
return 0
}
#Blue. Diagnostic instrumentation.
#Prints "$@" and runs "$@".
# WARNING: Use with care.
_messagePlain_probe_cmd() {
_color_begin_probe
_safeEcho "$@"
_color_end
echo
"$@"
return
}
_messageCMD() {
_messagePlain_probe_cmd "$@"
}
#Blue. Diagnostic instrumentation.
#Prints "$@" with quotes around every parameter.
_messagePlain_probe_quoteAddDouble() {
_color_begin_probe
_safeEcho_quoteAddDouble "$@"
_color_end
echo
return
}
_messagePlain_probe_quoteAdd() {
_messagePlain_probe_quoteAddDouble "$@"
}
#Blue. Diagnostic instrumentation.
#Prints "$@" with single quotes around every parameter.
_messagePlain_probe_quoteAddSingle() {
_color_begin_probe
_safeEcho_quoteAddSingle "$@"
_color_end
echo
return
}
#Blue. Diagnostic instrumentation.
#Prints "$@" and runs "$@".
# WARNING: Use with care.
_messagePlain_probe_cmd_quoteAdd() {
_messagePlain_probe_quoteAdd "$@"
"$@"
return
}
_messageCMD_quoteAdd() {
_messagePlain_probe_cmd_quoteAdd "$@"
}
#Demarcate major steps.
_messageNormal() {
_color_begin_Normal
echo -n "$@"
_color_end
echo
return 0
}
#Demarcate major failures.
_messageError() {
_color_begin_Error
echo -n "$@"
_color_end
echo
return 0
}
#Demarcate need to fetch/generate dependency automatically - not a failure condition.
_messageNEED() {
_messageNormal "NEED"
#echo " NEED "
}
#Demarcate have dependency already, no need to fetch/generate.
_messageHAVE() {
_messageNormal "HAVE"
#echo " HAVE "
}
_messageWANT() {
_messageNormal "WANT"
#echo " WANT "
}
#Demarcate where PASS/FAIL status cannot be absolutely proven. Rarely appropriate - usual best practice is to simply progress to the next major step.
_messageDONE() {
_messageNormal "DONE"
#echo " DONE "
}
_messagePASS() {
_messageNormal "PASS"
#echo " PASS "
}
#Major failure. Program stopped.
_messageFAIL() {
_messageError "FAIL"
#echo " FAIL "
_stop 1
return 0
}
_messageWARN() {
echo
echo "$@"
return 0
}
# Demarcate *any* delay performed to allow 'InterProcess-Communication' connections (perhaps including at least some network or serial port servers).
_messageDELAYipc() {
_color_begin_DELAYipc
echo -e -n 'delay: InterProcess-Communication'
_color_end
echo
}
_messageProcess() {
local processString
processString="$1""..."
local processStringLength
processStringLength=${#processString}
local currentIteration
currentIteration=0
local padLength
let padLength=40-"$processStringLength"
[[ "$processStringLength" -gt "38" ]] && _messageNormal "$processString" && return 0
_color_begin_Normal
echo -n "$processString"
_color_end
while [[ "$currentIteration" -lt "$padLength" ]]
do
echo -e -n ' '
let currentIteration="$currentIteration"+1
done
return 0
}
#echo -n
_safeEcho() {
printf '%s' "$1"
shift
[[ "$@" == "" ]] && return 0
local currentArg
for currentArg in "$@"
do
printf '%s' " "
printf '%s' "$currentArg"
done
return 0
}
#echo
_safeEcho_newline() {
_safeEcho "$@"
printf '\n'
}
_safeEcho_quoteAddSingle() {
# https://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_07.html
while (( "$#" )); do
_safeEcho ' '"'""$1""'"
shift
done
}
_safeEcho_quoteAddSingle_newline() {
_safeEcho_quoteAddSingle "$@"
printf '\n'
}
_safeEcho_quoteAddDouble() {
#https://stackoverflow.com/questions/1668649/how-to-keep-quotes-in-bash-arguments
local currentCommandStringPunctuated
local currentCommandStringParameter
for currentCommandStringParameter in "$@"; do
currentCommandStringParameter="${currentCommandStringParameter//\\/\\\\}"
currentCommandStringPunctuated="$currentCommandStringPunctuated \"${currentCommandStringParameter//\"/\\\"}\""
done
_safeEcho "$currentCommandStringPunctuated"
}
_safeEcho_quoteAddDouble_newline() {
_safeEcho_quoteAddDouble "$@"
printf '\n'
}
#### from messaging.sh
##Parameters
#"--shell", ""
#"--profile"
#"--parent", "--embed", "--return", "--devenv"
#"--call", "--script" "--bypass"
#if [[ "$ub_import" != "" ]]
#then
#[[ "$ub_import" != "" ]] && export ub_import="" && unset ub_import
#[[ "$importScriptLocation" != "" ]] && export importScriptLocation= && unset importScriptLocation
#[[ "$importScriptFolder" != "" ]] && export importScriptFolder= && unset importScriptFolder
#fi
#[[ "$ub_import" != "" ]] && export ub_import="" && unset ub_import
#[[ "$ub_import_param" != "" ]] && export ub_import_param="" && unset ub_import_param
#[[ "$ub_import_script" != "" ]] && export ub_import_script="" && unset ub_import_script
#[[ "$ub_loginshell" != "" ]] && export ub_loginshell="" && unset ub_loginshell
ub_import=
ub_import_param=
ub_import_script=
ub_loginshell=
# ATTENTION: Apparently (Portable) Cygwin Bash interprets correctly.
[[ "${BASH_SOURCE[0]}" != "${0}" ]] && ub_import="true"
( [[ "$1" == '--profile' ]] || [[ "$1" == '--script' ]] || [[ "$1" == '--call' ]] || [[ "$1" == '--return' ]] || [[ "$1" == '--devenv' ]] || [[ "$1" == '--shell' ]] || [[ "$1" == '--bypass' ]] || [[ "$1" == '--parent' ]] || [[ "$1" == '--embed' ]] || [[ "$1" == '--compressed' ]] ) && ub_import_param="$1" && shift
( [[ "$0" == "/bin/bash" ]] || [[ "$0" == "-bash" ]] || [[ "$0" == "/usr/bin/bash" ]] || [[ "$0" == "bash" ]] ) && ub_loginshell="true" #Importing ubiquitous bash into a login shell with "~/.bashrc" is the only known cause for "_getScriptAbsoluteLocation" to return a result such as "/bin/bash".
[[ "$ub_import" == "true" ]] && ! [[ "$ub_loginshell" == "true" ]] && ub_import_script="true"
_messagePlain_probe_expr '$0= '"$0"'\n ''$1= '"$1"'\n ''ub_import= '"$ub_import"'\n ''ub_import_param= '"$ub_import_param"'\n ''ub_import_script= '"$ub_import_script"'\n ''ub_loginshell= '"$ub_loginshell" | _user_log-ub
# DANGER Prohibited import from login shell. Use _setupUbiquitous, call from another script, or manually set importScriptLocation.
# WARNING Import from shell can be detected. Import from script cannot. Asserting that script has been imported is possible. Asserting that script has not been imported is not possible. Users may be protected from interactive mistakes. Script developers are NOT protected.
if [[ "$ub_import_param" == "--profile" ]]
then
if ( [[ "$profileScriptLocation" == "" ]] || [[ "$profileScriptFolder" == "" ]] ) && _messagePlain_bad 'import: profile: missing: profileScriptLocation, missing: profileScriptFolder' | _user_log-ub
then
return 1 >/dev/null 2>&1
exit 1
fi
elif ( [[ "$ub_import_param" == "--parent" ]] || [[ "$ub_import_param" == "--embed" ]] || [[ "$ub_import_param" == "--return" ]] || [[ "$ub_import_param" == "--devenv" ]] )
then
if ( [[ "$scriptAbsoluteLocation" == "" ]] || [[ "$scriptAbsoluteFolder" == "" ]] || [[ "$sessionid" == "" ]] ) && _messagePlain_bad 'import: parent: missing: scriptAbsoluteLocation, missing: scriptAbsoluteFolder, missing: sessionid' | _user_log-ub
then
return 1 >/dev/null 2>&1
exit 1
fi
elif [[ "$ub_import_param" == "--call" ]] || [[ "$ub_import_param" == "--script" ]] || [[ "$ub_import_param" == "--bypass" ]] || [[ "$ub_import_param" == "--shell" ]] || [[ "$ub_import_param" == "--compressed" ]] || ( [[ "$ub_import" == "true" ]] && [[ "$ub_import_param" == "" ]] )
then
if ( [[ "$importScriptLocation" == "" ]] || [[ "$importScriptFolder" == "" ]] ) && _messagePlain_bad 'import: call: missing: importScriptLocation, missing: importScriptFolder' | _user_log-ub
then
return 1 >/dev/null 2>&1
exit 1
fi
elif [[ "$ub_import" != "true" ]] #"--shell", ""
then
_messagePlain_warn 'import: undetected: cannot determine if imported' | _user_log-ub
true #no problem
else #FAIL, implies [[ "$ub_import" == "true" ]]
_messagePlain_bad 'import: fall: fail' | _user_log-ub
return 1 >/dev/null 2>&1
exit 1
fi
_sep() {
echo '________________________________________'
}
#####Utilities
_test_getAbsoluteLocation_sequence() {
_start scriptLocal_mkdir_disable
local testScriptLocation_actual
local testScriptLocation
local testScriptFolder
local testLocation_actual
local testLocation
local testFolder
#script location/folder work directories
mkdir -p "$safeTmp"/sAL_dir
cp "$scriptAbsoluteLocation" "$safeTmp"/sAL_dir/script
ln -s "$safeTmp"/sAL_dir/script "$safeTmp"/sAL_dir/lnk
[[ ! -e "$safeTmp"/sAL_dir/script ]] && _stop 1
[[ ! -e "$safeTmp"/sAL_dir/lnk ]] && _stop 1
ln -s "$safeTmp"/sAL_dir "$safeTmp"/sAL_lnk
[[ ! -e "$safeTmp"/sAL_lnk/script ]] && _stop 1
[[ ! -e "$safeTmp"/sAL_lnk/lnk ]] && _stop 1
#_getScriptAbsoluteLocation
testScriptLocation_actual=$("$safeTmp"/sAL_dir/script _getScriptAbsoluteLocation)
[[ "$safeTmp"/sAL_dir/script != "$testScriptLocation_actual" ]] && echo 'crit: "$safeTmp"/sAL_dir/script != "$testScriptLocation_actual"' && _stop 1
testScriptLocation=$("$safeTmp"/sAL_dir/script _getScriptAbsoluteLocation)
[[ "$testScriptLocation" != "$testScriptLocation_actual" ]] && echo 'crit: ! location "$safeTmp"/sAL_dir/script' && _stop 1
testScriptLocation=$("$safeTmp"/sAL_dir/lnk _getScriptAbsoluteLocation)
[[ "$testScriptLocation" != "$testScriptLocation_actual" ]] && echo 'crit: ! location "$safeTmp"/sAL_dir/lnk' && _stop 1
testScriptLocation=$("$safeTmp"/sAL_lnk/script _getScriptAbsoluteLocation)
[[ "$testScriptLocation" != "$testScriptLocation_actual" ]] && echo 'crit: ! location "$safeTmp"/sAL_lnk/script' && _stop 1
testScriptLocation=$("$safeTmp"/sAL_lnk/lnk _getScriptAbsoluteLocation)
[[ "$testScriptLocation" != "$testScriptLocation_actual" ]] && echo 'crit: ! location "$safeTmp"/sAL_lnk/lnk' && _stop 1
#_getScriptAbsoluteFolder
testScriptFolder_actual=$("$safeTmp"/sAL_dir/script _getScriptAbsoluteFolder)
[[ "$safeTmp"/sAL_dir != "$testScriptFolder_actual" ]] && echo 'crit: "$safeTmp"/sAL_dir != "$testScriptFolder_actual"' && _stop 1
testScriptFolder=$("$safeTmp"/sAL_dir/script _getScriptAbsoluteFolder)
[[ "$testScriptFolder" != "$testScriptFolder_actual" ]] && echo 'crit: ! folder "$safeTmp"/sAL_dir/script' && _stop 1
testScriptFolder=$("$safeTmp"/sAL_dir/lnk _getScriptAbsoluteFolder)
[[ "$testScriptFolder" != "$testScriptFolder_actual" ]] && echo 'crit: ! folder "$safeTmp"/sAL_dir/lnk' && _stop 1
testScriptFolder=$("$safeTmp"/sAL_lnk/script _getScriptAbsoluteFolder)
[[ "$testScriptFolder" != "$testScriptFolder_actual" ]] && echo 'crit: ! folder "$safeTmp"/sAL_lnk/script' && _stop 1
testScriptFolder=$("$safeTmp"/sAL_lnk/lnk _getScriptAbsoluteFolder)
[[ "$testScriptFolder" != "$testScriptFolder_actual" ]] && echo 'crit: ! folder "$safeTmp"/sAL_lnk/lnk' && _stop 1
#_getAbsoluteLocation
testLocation_actual=$("$safeTmp"/sAL_dir/script _getAbsoluteLocation "$safeTmp"/sAL_dir/script)
[[ "$safeTmp"/sAL_dir/script != "$testLocation_actual" ]] && echo 'crit: "$safeTmp"/sAL_dir/script != "$testLocation_actual"' && _stop 1
testLocation=$("$safeTmp"/sAL_dir/script _getAbsoluteLocation "$safeTmp"/sAL_dir/script)
[[ "$testLocation" != "$testLocation_actual" ]] && echo 'crit: ! location "$safeTmp"/sAL_dir/script' && _stop 1
testLocation=$("$safeTmp"/sAL_dir/lnk _getAbsoluteLocation "$safeTmp"/sAL_dir/lnk)
[[ "$testLocation" != "$testLocation_actual" ]] && echo 'crit: ! location "$safeTmp"/sAL_dir/lnk' && _stop 1
testLocation=$("$safeTmp"/sAL_lnk/script _getAbsoluteLocation "$safeTmp"/sAL_lnk/script)
[[ "$testLocation" != "$testLocation_actual" ]] && echo 'crit: ! location "$safeTmp"/sAL_lnk/script' && _stop 1
testLocation=$("$safeTmp"/sAL_lnk/lnk _getAbsoluteLocation "$safeTmp"/sAL_lnk/lnk)
[[ "$testLocation" != "$testLocation_actual" ]] && echo 'crit: ! location "$safeTmp"/sAL_lnk/lnk' && _stop 1
#_getAbsoluteFolder
testFolder_actual=$("$safeTmp"/sAL_dir/script _getAbsoluteFolder "$safeTmp"/sAL_dir/script)
[[ "$safeTmp"/sAL_dir != "$testFolder_actual" ]] && echo 'crit: "$safeTmp"/sAL_dir != "$testFolder_actual"' && _stop 1
testFolder=$("$safeTmp"/sAL_dir/script _getAbsoluteFolder "$safeTmp"/sAL_dir/script)
[[ "$testFolder" != "$testFolder_actual" ]] && echo 'crit: ! folder "$safeTmp"/sAL_dir/script' && _stop 1
testFolder=$("$safeTmp"/sAL_dir/lnk _getAbsoluteFolder "$safeTmp"/sAL_dir/script)
[[ "$testFolder" != "$testFolder_actual" ]] && echo 'crit: ! folder "$safeTmp"/sAL_dir/lnk' && _stop 1
testFolder=$("$safeTmp"/sAL_lnk/script _getAbsoluteFolder "$safeTmp"/sAL_lnk/script)
[[ "$testFolder" != "$testFolder_actual" ]] && echo 'crit: ! folder "$safeTmp"/sAL_lnk/script' && _stop 1
testFolder=$("$safeTmp"/sAL_lnk/lnk _getAbsoluteFolder "$safeTmp"/sAL_lnk/script)
[[ "$testFolder" != "$testFolder_actual" ]] && echo 'crit: ! folder "$safeTmp"/sAL_lnk/lnk' && _stop 1
_stop
}
_test_getAbsoluteLocation() {
"$scriptAbsoluteLocation" _test_getAbsoluteLocation_sequence "$@"
[[ "$?" != "0" ]] && _stop 1
return 0
}
#https://unix.stackexchange.com/questions/293892/realpath-l-vs-p
_test_realpath_L_s_sequence() {
_start scriptLocal_mkdir_disable
local functionEntryPWD
functionEntryPWD="$PWD"
local testPath_actual
local testPath
mkdir -p "$safeTmp"/src
mkdir -p "$safeTmp"/sub
ln -s "$safeTmp"/src "$safeTmp"/sub/lnk
echo > "$safeTmp"/sub/point
ln -s "$safeTmp"/sub/point "$safeTmp"/sub/lnk/ref
#correct
#"$safeTmp"/sub/ref
#realpath -L "$safeTmp"/sub/lnk/../ref
#default, wrong
#"$safeTmp"/ref
#realpath -P "$safeTmp"/sub/lnk/../ref
#echo -n '>'
#readlink -f "$safeTmp"/sub/lnk/../ref
testPath_actual="$safeTmp"/sub/ref
cd "$functionEntryPWD"
testPath=$(_realpath_L "$safeTmp"/sub/lnk/../ref)
[[ "$testPath" != "$testPath_actual" ]] && echo 'crit: ! _realpath_L' && _stop 1
cd "$safeTmp"
testPath=$(_realpath_L ./sub/lnk/../ref)
[[ "$testPath" != "$testPath_actual" ]] && echo 'crit: ! _realpath_L' && _stop 1
#correct
#"$safeTmp"/sub/lnk/ref
#realpath -L -s "$safeTmp"/sub/lnk/ref
#default, wrong for some use cases
#"$safeTmp"/sub/point
#realpath -L "$safeTmp"/sub/lnk/ref
#echo -n '>'
#readlink -f "$safeTmp"/sub/lnk/ref
testPath_actual="$safeTmp"/sub/lnk/ref
cd "$functionEntryPWD"
testPath=$(_realpath_L_s "$safeTmp"/sub/lnk/ref)
[[ "$testPath" != "$testPath_actual" ]] && echo 'crit: ! _realpath_L_s' && _stop 1
cd "$safeTmp"
testPath=$(_realpath_L_s ./sub/lnk/ref)
[[ "$testPath" != "$testPath_actual" ]] && echo 'crit: ! _realpath_L_s' && _stop 1
cd "$functionEntryPWD"
_stop
}
_test_realpath_L_s() {
#Optional safety check. Nonconformant realpath solution should be caught by synthetic test cases.
#_compat_realpath
# ! [[ -e "$compat_realpath_bin" ]] && [[ "$compat_realpath_bin" != "" ]] && echo 'crit: missing: realpath' && _stop 1
"$scriptAbsoluteLocation" _test_realpath_L_s_sequence "$@"
[[ "$?" != "0" ]] && _stop 1
return 0
}
_test_realpath_L() {
_test_realpath_L_s "$@"
}
_test_realpath() {
_test_realpath_L_s "$@"
}
_test_readlink_f_sequence() {
_start scriptLocal_mkdir_disable
echo > "$safeTmp"/realFile
ln -s "$safeTmp"/realFile "$safeTmp"/linkA
ln -s "$safeTmp"/linkA "$safeTmp"/linkB
local currentReadlinkResult
currentReadlinkResult=$(readlink -f "$safeTmp"/linkB)
local currentReadlinkResultBasename
currentReadlinkResultBasename=$(basename "$currentReadlinkResult")
if [[ "$currentReadlinkResultBasename" != "realFile" ]]
then
#echo 'fail: readlink -f'
_stop 1
fi
_stop 0
}
_test_readlink_f() {
if ! "$scriptAbsoluteLocation" _test_readlink_f_sequence
then
# May fail through MSW network drive provided by '_userVBox' .
uname -a | grep -i cygwin > /dev/null 2>&1 && echo 'warn: broken (cygwin): readlink -f' && return 1
echo 'fail: readlink -f'
_stop 1
fi
return 0
}
_compat_realpath() {
[[ -e "$compat_realpath_bin" ]] && [[ "$compat_realpath_bin" != "" ]] && return 0
#Workaround, Mac. See https://github.com/mirage335/ubiquitous_bash/issues/1 .
export compat_realpath_bin=/opt/local/libexec/gnubin/realpath
[[ -e "$compat_realpath_bin" ]] && [[ "$compat_realpath_bin" != "" ]] && return 0
export compat_realpath_bin=$(type -p realpath)
[[ -e "$compat_realpath_bin" ]] && [[ "$compat_realpath_bin" != "" ]] && return 0
export compat_realpath_bin=/bin/realpath
[[ -e "$compat_realpath_bin" ]] && [[ "$compat_realpath_bin" != "" ]] && return 0
export compat_realpath_bin=/usr/bin/realpath
[[ -e "$compat_realpath_bin" ]] && [[ "$compat_realpath_bin" != "" ]] && return 0
# ATTENTION
# Command "readlink -f" or text processing can be used as fallbacks to obtain absolute path
# https://stackoverflow.com/questions/3572030/bash-script-absolute-path-with-osx
export compat_realpath_bin=""
return 1
}
_compat_realpath_run() {
! _compat_realpath && return 1
"$compat_realpath_bin" "$@"
}
_realpath_L() {
if ! _compat_realpath_run -L . > /dev/null 2>&1
then
readlink -f "$@"
return
fi
realpath -L "$@"
}
_realpath_L_s() {
if ! _compat_realpath_run -L . > /dev/null 2>&1
then
readlink -f "$@"
return
fi
realpath -L -s "$@"
}
_cygwin_translation_rootFileParameter() {
if ! uname -a | grep -i cygwin > /dev/null 2>&1
then
echo "$0"
return 0
fi
local currentScriptLocation
currentScriptLocation="$0"
# CAUTION: Lookup table is used to avoid pulling in any additional dependencies. Additionally, Cygwin apparently may ignore letter case of path .
[[ "$currentScriptLocation" == 'A:'* ]] && currentScriptLocation=${currentScriptLocation/A\://cygdrive/a} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'B:'* ]] && currentScriptLocation=${currentScriptLocation/B\://cygdrive/b} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'C:'* ]] && currentScriptLocation=${currentScriptLocation/C\://cygdrive/c} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'D:'* ]] && currentScriptLocation=${currentScriptLocation/D\://cygdrive/d} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'E:'* ]] && currentScriptLocation=${currentScriptLocation/E\://cygdrive/e} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'F:'* ]] && currentScriptLocation=${currentScriptLocation/F\://cygdrive/f} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'G:'* ]] && currentScriptLocation=${currentScriptLocation/G\://cygdrive/g} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'H:'* ]] && currentScriptLocation=${currentScriptLocation/H\://cygdrive/h} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'I:'* ]] && currentScriptLocation=${currentScriptLocation/I\://cygdrive/i} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'J:'* ]] && currentScriptLocation=${currentScriptLocation/J\://cygdrive/j} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'K:'* ]] && currentScriptLocation=${currentScriptLocation/K\://cygdrive/k} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'L:'* ]] && currentScriptLocation=${currentScriptLocation/L\://cygdrive/l} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'M:'* ]] && currentScriptLocation=${currentScriptLocation/M\://cygdrive/m} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'N:'* ]] && currentScriptLocation=${currentScriptLocation/N\://cygdrive/n} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'O:'* ]] && currentScriptLocation=${currentScriptLocation/O\://cygdrive/o} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'P:'* ]] && currentScriptLocation=${currentScriptLocation/P\://cygdrive/p} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'Q:'* ]] && currentScriptLocation=${currentScriptLocation/Q\://cygdrive/q} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'R:'* ]] && currentScriptLocation=${currentScriptLocation/R\://cygdrive/r} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'S:'* ]] && currentScriptLocation=${currentScriptLocation/S\://cygdrive/s} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'T:'* ]] && currentScriptLocation=${currentScriptLocation/T\://cygdrive/t} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'U:'* ]] && currentScriptLocation=${currentScriptLocation/U\://cygdrive/u} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'V:'* ]] && currentScriptLocation=${currentScriptLocation/V\://cygdrive/v} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'W:'* ]] && currentScriptLocation=${currentScriptLocation/W\://cygdrive/w} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'X:'* ]] && currentScriptLocation=${currentScriptLocation/X\://cygdrive/x} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'Y:'* ]] && currentScriptLocation=${currentScriptLocation/Y\://cygdrive/y} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'Z:'* ]] && currentScriptLocation=${currentScriptLocation/Z\://cygdrive/z} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'a:'* ]] && currentScriptLocation=${currentScriptLocation/a\://cygdrive/a} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'b:'* ]] && currentScriptLocation=${currentScriptLocation/b\://cygdrive/b} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'c:'* ]] && currentScriptLocation=${currentScriptLocation/c\://cygdrive/c} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'd:'* ]] && currentScriptLocation=${currentScriptLocation/d\://cygdrive/d} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'e:'* ]] && currentScriptLocation=${currentScriptLocation/e\://cygdrive/e} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'f:'* ]] && currentScriptLocation=${currentScriptLocation/f\://cygdrive/f} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'g:'* ]] && currentScriptLocation=${currentScriptLocation/g\://cygdrive/g} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'h:'* ]] && currentScriptLocation=${currentScriptLocation/h\://cygdrive/h} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'i:'* ]] && currentScriptLocation=${currentScriptLocation/i\://cygdrive/i} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'j:'* ]] && currentScriptLocation=${currentScriptLocation/j\://cygdrive/j} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'k:'* ]] && currentScriptLocation=${currentScriptLocation/k\://cygdrive/k} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'l:'* ]] && currentScriptLocation=${currentScriptLocation/l\://cygdrive/l} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'm:'* ]] && currentScriptLocation=${currentScriptLocation/m\://cygdrive/m} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'n:'* ]] && currentScriptLocation=${currentScriptLocation/n\://cygdrive/n} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'o:'* ]] && currentScriptLocation=${currentScriptLocation/o\://cygdrive/o} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'p:'* ]] && currentScriptLocation=${currentScriptLocation/p\://cygdrive/p} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'q:'* ]] && currentScriptLocation=${currentScriptLocation/q\://cygdrive/q} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'r:'* ]] && currentScriptLocation=${currentScriptLocation/r\://cygdrive/r} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 's:'* ]] && currentScriptLocation=${currentScriptLocation/s\://cygdrive/s} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 't:'* ]] && currentScriptLocation=${currentScriptLocation/t\://cygdrive/t} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'u:'* ]] && currentScriptLocation=${currentScriptLocation/u\://cygdrive/u} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'v:'* ]] && currentScriptLocation=${currentScriptLocation/v\://cygdrive/v} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'w:'* ]] && currentScriptLocation=${currentScriptLocation/w\://cygdrive/w} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'x:'* ]] && currentScriptLocation=${currentScriptLocation/x\://cygdrive/x} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'y:'* ]] && currentScriptLocation=${currentScriptLocation/y\://cygdrive/y} && echo "$currentScriptLocation" && return 0
[[ "$currentScriptLocation" == 'z:'* ]] && currentScriptLocation=${currentScriptLocation/z\://cygdrive/z} && echo "$currentScriptLocation" && return 0
echo "$currentScriptLocation" && return 0
}
#Critical prerequsites.
_getAbsolute_criticalDep() {
# ! type realpath > /dev/null 2>&1 && return 1
! type readlink > /dev/null 2>&1 && return 1
! type dirname > /dev/null 2>&1 && return 1
! type basename > /dev/null 2>&1 && return 1
#Known to succeed under BusyBox (OpenWRT), NetBSD, and common Linux variants. No known failure modes. Extra precaution.
! readlink -f . > /dev/null 2>&1 && return 1
! echo 'qwerty123.git' | grep '\.git$' > /dev/null 2>&1 && return 1
echo 'qwerty1234git' | grep '\.git$' > /dev/null 2>&1 && return 1
return 0
}
! _getAbsolute_criticalDep && exit 1
#Retrieves absolute path of current script, while maintaining symlinks, even when "./" would translate with "readlink -f" into something disregarding symlinked components in $PWD.
#However, will dereference symlinks IF the script location itself is a symlink. This is to allow symlinking to scripts to function normally.
#Suitable for allowing scripts to find other scripts they depend on. May look like an ugly hack, but it has proven reliable over the years.
_getScriptAbsoluteLocation() {
if [[ "$0" == "-"* ]]
then
return 1
fi
local currentScriptLocation
currentScriptLocation="$0"
uname -a | grep -i cygwin > /dev/null 2>&1 && type _cygwin_translation_rootFileParameter > /dev/null 2>&1 && currentScriptLocation=$(_cygwin_translation_rootFileParameter)
local absoluteLocation
if [[ (-e $PWD\/$currentScriptLocation) && ($currentScriptLocation != "") ]] && [[ "$currentScriptLocation" != "/"* ]]
then
absoluteLocation="$PWD"\/"$currentScriptLocation"
absoluteLocation=$(_realpath_L_s "$absoluteLocation")
else
absoluteLocation=$(_realpath_L "$currentScriptLocation")
fi
if [[ -h "$absoluteLocation" ]]
then
absoluteLocation=$(readlink -f "$absoluteLocation")
absoluteLocation=$(_realpath_L "$absoluteLocation")
fi
echo $absoluteLocation
}
alias getScriptAbsoluteLocation=_getScriptAbsoluteLocation
#Retrieves absolute path of current script, while maintaining symlinks, even when "./" would translate with "readlink -f" into something disregarding symlinked components in $PWD.
#Suitable for allowing scripts to find other scripts they depend on.
_getScriptAbsoluteFolder() {
if [[ "$0" == "-"* ]]
then
return 1
fi
dirname "$(_getScriptAbsoluteLocation)"
}
alias getScriptAbsoluteFolder=_getScriptAbsoluteFolder
#Retrieves absolute path of parameter, while maintaining symlinks, even when "./" would translate with "readlink -f" into something disregarding symlinked components in $PWD.
#Suitable for finding absolute paths, when it is desirable not to interfere with symlink specified folder structure.
_getAbsoluteLocation() {
if [[ "$1" == "-"* ]]
then
return 1
fi
if [[ "$1" == "" ]]
then
echo
return
fi
local absoluteLocation
if [[ (-e $PWD\/$1) && ($1 != "") ]] && [[ "$1" != "/"* ]]
then
absoluteLocation="$PWD"\/"$1"
absoluteLocation=$(_realpath_L_s "$absoluteLocation")
else
absoluteLocation=$(_realpath_L "$1")
fi
echo "$absoluteLocation"
}
alias getAbsoluteLocation=_getAbsoluteLocation
#Retrieves absolute path of parameter, while maintaining symlinks, even when "./" would translate with "readlink -f" into something disregarding symlinked components in $PWD.
#Suitable for finding absolute paths, when it is desirable not to interfere with symlink specified folder structure.
_getAbsoluteFolder() {
if [[ "$1" == "-"* ]]
then
return 1
fi
local absoluteLocation=$(_getAbsoluteLocation "$1")
dirname "$absoluteLocation"
}
alias getAbsoluteLocation=_getAbsoluteLocation
_getScriptLinkName() {
! [[ -e "$0" ]] && return 1
! [[ -L "$0" ]] && return 1
! type basename > /dev/null 2>&1 && return 1
local scriptLinkName
scriptLinkName=$(basename "$0")
[[ "$scriptLinkName" == "" ]] && return 1
echo "$scriptLinkName"
}
#https://unix.stackexchange.com/questions/27021/how-to-name-a-file-in-the-deepest-level-of-a-directory-tree?answertab=active#tab-top
_filter_lowestPath() {
awk -F'/' 'NF > depth {
depth = NF;
deepest = $0;
}
END {
print deepest;
}'
}
#https://stackoverflow.com/questions/1086907/can-find-or-any-other-tool-search-for-files-breadth-first
_filter_highestPath() {
awk -F'/' '{print "", NF, $F}' | sort -n | awk '{print $2}' | head -n 1
}
_recursion_guard() {
! [[ -e "$1" ]] && return 1
! type "$1" >/dev/null 2>&1 && return 1
local launchGuardScriptAbsoluteLocation
launchGuardScriptAbsoluteLocation=$(_getScriptAbsoluteLocation)
local launchGuardTestAbsoluteLocation
launchGuardTestAbsoluteLocation=$(_getAbsoluteLocation "$1")
[[ "$launchGuardScriptAbsoluteLocation" == "$launchGuardTestAbsoluteLocation" ]] && return 1
return 0
}
#Checks whether command or function is available.
# DANGER Needed by safeRMR .
_checkDep() {
if ! type "$1" >/dev/null 2>&1
then
echo "$1" missing
_stop 1
fi
}
_tryExec() {
type "$1" >/dev/null 2>&1 && "$1"
}
_tryExecFull() {
type "$1" >/dev/null 2>&1 && "$@"
}
#Fails if critical global variables point to nonexistant locations. Code may be duplicated elsewhere for extra safety.
_failExec() {
[[ ! -e "$scriptAbsoluteLocation" ]] && return 1
[[ ! -e "$scriptAbsoluteFolder" ]] && return 1
return 0
}
#Portable sanity checked "rm -r" command.
# DANGER Last line of defense against catastrophic errors where "rm -r" or similar would be used!
# WARNING Not foolproof. Use to guard against systematic errors, not carelessness.
# WARNING Do NOT rely upon outside of internal programmatic usage inside script!
# WARNING Consider using this function even if program control flow can be proven safe. Redundant checks just might catch catastrophic memory errors.
#"$1" == directory to remove
_safeRMR() {
! type _getAbsolute_criticalDep > /dev/null 2>&1 && return 1
! _getAbsolute_criticalDep && return 1
#Fail sooner, avoiding irrelevant error messages. Especially important to cases where an upstream process has already removed the "$safeTmp" directory of a downstream process which reaches "_stop" later.
! [[ -e "$1" ]] && return 1
[[ ! -e "$scriptAbsoluteLocation" ]] && return 1
[[ ! -e "$scriptAbsoluteFolder" ]] && return 1
_failExec || return 1
#if [[ ! -e "$0" ]]
#then
# return 1
#fi
if [[ "$1" == "" ]]
then
return 1
fi
if [[ "$1" == "/" ]]
then
return 1
fi
if [[ "$1" == "-"* ]]
then
return 1
fi
#Denylist.