/
bash.origin
executable file
·2025 lines (1821 loc) · 72.2 KB
/
bash.origin
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
# Source: https://github.com/cadorn/bash.origin
# License: http://unlicense.org/
function init {
if echo "$@" | grep -q -Ee '(\$|\s)--debug(\s|\$)'; then
export BO_VERBOSE=1
export VERBOSE=1
elif echo "$npm_config_argv" | grep -q -Ee '"--debug"'; then
export BO_VERBOSE=1
export VERBOSE=1
fi
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] --- START --- (pwd: $(pwd))"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] SHELL: $SHELL"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] args: $@"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] which bash: $(which bash)"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] bash --version: $(bash --version)"
if [ ! -z "$BO_LOADED" ]; then
# If we loaded before we do not load again.
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] Skip due to BO_LOADED=1"
return;
fi
if ! which perl > /dev/null; then
echo -e >&2 "[BO] ERROR: You must have 'perl' installed!\n"
exit 1
fi
# If 'SHELL' is set to 'bash' that is good enough for us.
if [ "$(echo "$SHELL" | perl -pe 's/^.*(bash)$/$1/')" != "bash" ]; then
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] SHELL is not 'bash': $SHELL"
if [ ! -z "$BASH_VERSION" ]; then
local __BO_SHELL="bash"
SHELL="$(which bash)"
fi
# 'SHELL' is not what we need so we try and verify that we are running in `bash`
# by looking for `bash` in the process description.
# NOTE: This does not work reliably (e.g. on alpine docker image)
#set +e
#[ -z "$BO_VERBOSE" ] || echo "[bash.origin] Running: ps | perl -pe \"s/^\s*$$\s+.*?(bash).*?$/\$1/\" | grep '^bash$'"
#local __BO_SHELL="$(ps | perl -pe "s/^\s*$$\s+.*?(bash).*?$/\$1/" | grep '^bash$')"
#[ -z "$BO_VERBOSE" ] || echo "[bash.origin] __BO_SHELL: $__BO_SHELL"
#set -e
#if [ "$(ps -p $$ | tail -1 | awk '{ print $4 }' | perl -pe 's/^.*(bash)$/$1/')" != "bash" ]; then
# TODO: Use additional ways to detect if we are running in bash.
if [ "$__BO_SHELL" != "bash" ]; then
echo -e >&2 ""
echo -e >&2 "[BO] WARNING: Your 'SHELL' environment variable is set to '$SHELL' while it should be set to '$(which bash)'\n"
echo -e >&2 "[BO] ERROR: You must be using the 'bash' shell interpreter!\n"
echo -e >&2 "[BO] ACTION: Enter the bash shell for this session only first by running:\n"
echo -e >&2 " SHELL=$(which bash) $(which bash)\n"
echo -e >&2 "[BO] ACTION[macOS]: or change your default shell to the bash shell by running:\n"
echo -e >&2 " sudo dscl localhost -change /Local/Default/Users/$USER UserShell $(which zsh) $(which bash)"
echo -e >&2 " # Now open a new terminal\n"
exit 1
fi
fi
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] $SHELL --version: $($SHELL --version)"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] BO_BASH_ALLOW_3: $BO_BASH_ALLOW_3"
if [[ $BO_BASH_ALLOW_3 != 1 ]] && [[ "$($SHELL --version)" != "GNU bash, version 4."* ]] && [[ "$($SHELL --version)" != "GNU bash, version 5."* ]]; then
if [ "$(which bash)" != "$SHELL" ]; then
if [[ "$(bash --version)" == "GNU bash, version 4."* ]] || [[ "$(bash --version)" == "GNU bash, version 5."* ]]; then
# We update the SHELL to where 'bash' actually is.
SHELL="$(which bash)"
else
echo -e >&2 "[BO] ERROR: You are using bash version '$($SHELL --version)' instead of version 4+!\n"
echo -e >&2 "[BO] ACTION: Install bash version 4+. On OSX you can use:\n"
echo -e >&2 " brew install bash\n"
echo -e >&2 "[BO] ACTION: Then call your script again using:\n"
echo -e >&2 " SHELL=/usr/local/bin/bash /usr/local/bin/bash <ScriptPath>\n"
echo -e >&2 "[BO] ACTION: or change your default shell to the bash shell by running:\n"
echo -e >&2 " sudo dscl localhost -change /Local/Default/Users/$USER UserShell $(which zsh) /bin/bash"
echo -e >&2 " # Now open a new terminal\n"
exit 1
fi
fi
fi
if [ -z "$ORIGINAL_BO_CLI_ARGS" ]; then
export ORIGINAL_BO_CLI_ARGS="$@"
fi
local ORIGINAL_BO_ROOT_SCRIPT_PATH="$BO_ROOT_SCRIPT_PATH"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] ORIGINAL_BO_CLI_ARGS: $ORIGINAL_BO_CLI_ARGS"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] ORIGINAL_BO_ROOT_SCRIPT_PATH: $ORIGINAL_BO_ROOT_SCRIPT_PATH"
###############################
# Declare Boot Globals
###############################
BO_LOG_PREFIX="[BO] "
BO_READ_SELF_BASH_SOURCE="$""{BASH_SOURCE[0]:-$""0}"
###############################
# Declare Boot Functions
###############################
function BO_cecho {
# Escape sequence and resets
ESC_SEQ="\033["
RESET_ALL="${ESC_SEQ}0m"
RESET_BOLD="${ESC_SEQ}21m"
RESET_UL="${ESC_SEQ}24m"
NONE=""
# Foreground colours
FG_BLACK="30"
FG_RED="31"
FG_GREEN="32"
FG_YELLOW="33"
FG_BLUE="34"
FG_MAGENTA="35"
FG_CYAN="36"
FG_WHITE="37"
FG_BR_BLACK="90"
FG_BR_RED="91"
FG_BR_GREEN="92"
FG_BR_YELLOW="93"
FG_BR_BLUE="94"
FG_BR_MAGENTA="95"
FG_BR_CYAN="96"
FG_BR_WHITE="97"
# Background colours (optional)
BG_BLACK="40"
BG_RED="41"
BG_GREEN="42"
BG_YELLOW="43"
BG_BLUE="44"
BG_MAGENTA="45"
BG_CYAN="46"
BG_WHITE="47"
# Font styles
FS_REG="0"
FS_BOLD="1"
FS_UL="4"
message=${1}
foreground="NONE"
style="FS_REG"
background="NONE"
if [ ! -z "${2}" ]; then
foreground="FG_${2}"
fi
if [ ! -z "${3}" ]; then
style="FS_${3}"
fi
if [ ! -z "${4}" ]; then
background="BG_${4}"
fi
background="${!background}"
if [ "${background}" != "" ]; then
background=";${background}"
fi
echo -e "${ESC_SEQ}${!style};${!foreground}${background}m${message}${RESET_ALL}"
}
function BO_has {
if type "$1" > /dev/null 2>&1 || hash "$1" > /dev/null 2>&1; then
return 0
fi
return 1
}
function BO_hash {
echo -n "$1" | openssl sha1
}
function BO_log {
if [ "$1" != "1" ]; then
return;
fi
echo -e >&2 "${*:2}"
}
function BO_exit_error {
echo >&2 "$(BO_cecho "ERROR: $1" RED BOLD)"
exit 1
}
function BO_ensure_env_HOME {
# Should fail if not set or set to empty string.
if [ ! -n "${HOME}" ] || [ "${HOME}" == "" ]; then
echo >&2 "[BO] ERROR: 'HOME' environment variable is not set! Used by: $1"
exit 1
fi
}
function BO_setResult {
local __resultvar=$1
eval $__resultvar="'$2'"
return 0
}
function BO_deriveSelfDir {
# @source http://stackoverflow.com/a/246128/330439
local SOURCE="$2"
local DIR=""
while [ -h "$SOURCE" ]; do
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
SOURCE="$(readlink "$SOURCE")"
[[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
done
BO_setResult $1 "$( cd -P "$( dirname "$SOURCE" )" && pwd )"
return 0
}
function BO_sourceProfile {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceProfile] args: $@"
if [[ $BO_ENABLE_SOURCE_PROFILE == 0 ]]; then
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceProfile] return due to 'BO_ENABLE_SOURCE_PROFILE == 0'"
return 0
fi
if [ ! -z "$CIRCLE_SHA1" ]; then
# On circleci we do not load the profile again
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceProfile] return due to 'CIRCLE_SHA1' set"
return 0
fi
# TODO: Allow sourcing multiple times if 'force' argument provided.
if [ -z "$BO_sourceProfile__sourced" ]; then
export BO_sourceProfile__sourced="1"
BO_ensure_env_HOME "BO_sourceProfile"
local currentDirectory="$(pwd)"
local currentPATH="$PATH"
local currentNVM=`printenv | grep '^NVM_'`
if [ -f "$HOME/.bash_profile" ]; then
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceProfile] Source from '$HOME/.bash_profile' (pwd: ${currentDirectory})"
set +e
if [ -z "$BO_VERBOSE" ]; then
. "$HOME/.bash_profile" >/dev/null 2>&1
else
. "$HOME/.bash_profile"
fi
set -e
elif [ -f "$HOME/.profile" ]; then
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceProfile] Source from '$HOME/.profile' (pwd: ${currentDirectory})"
set +e
if [ -z "$BO_VERBOSE" ]; then
. "$HOME/.profile" >/dev/null 2>&1
else
. "$HOME/.profile"
fi
set -e
fi
if [ "$(pwd)" != "${currentDirectory}" ]; then
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceProfile] Changing directory back to '${currentDirectory}' (pwd: $(pwd))"
cd "${currentDirectory}"
fi
export PATH="${currentPATH}"
eval "$currentNVM"
fi
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceProfile] DONE"
}
function BO_has_cli_arg {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_has_cli_arg] args: $@"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_has_cli_arg] ORIGINAL_BO_CLI_ARGS: $ORIGINAL_BO_CLI_ARGS"
if echo "$ORIGINAL_BO_CLI_ARGS" | grep -q -Ee "(^|\s)$1(\s|\$)"; then
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_has_cli_arg] found arg '$1' in 'ORIGINAL_BO_CLI_ARGS'"
return 0
fi
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_has_cli_arg] npm_config_argv: $npm_config_argv"
if [[ "$npm_config_argv" =~ "$1" ]]; then
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_has_cli_arg] found arg '$1' in 'npm_config_argv'"
return 0
fi
#if echo "$npm_config_argv" | grep -q -Ee '"$1"'; then
# [ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_has_cli_arg] found arg '$1' in 'npm_config_argv'"
# return 0
#fi
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_has_cli_arg] Did not find arg '$1' in ORIGINAL_BO_CLI_ARGS ($ORIGINAL_BO_CLI_ARGS) nor npm_config_argv ($npm_config_argv)"
return 1
}
function BO_has_arg {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_has_arg] args: $@"
if echo "${*:2}" | grep -q -Ee "(^|\s)$1(\s|\$)"; then
return 0
fi
return 1
}
function BO_checkVerbose {
local _VERBOSE="$VERBOSE"
# Look for '-v', '--verbose', '-d' or '--debug' flags.
if echo "$@" | grep -q -Ee '(^|\s)--?([vd]|verbose|debug)(\s|\$)'; then
_VERBOSE="1"
BO_log "$BO_VERBOSE" "[BO_checkVerbose] enable due to verbose command arg: $@"
fi
if echo "$npm_config_argv" | grep -q -Ee '"--?([vd]|verbose|debug)"'; then
_VERBOSE="1"
BO_log "$BO_VERBOSE" "[BO_checkVerbose] enable due to verbose npm_config_argv arg: $npm_config_argv"
fi
# if [ "$_VERBOSE" == "1" ]; then
# BO_log "$_VERBOSE" "[BO_checkVerbose] Verbose Enabled: $_VERBOSE"
# fi
BO_setResult "$1" "$_VERBOSE"
}
function BO_resetLoaded {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_resetLoaded]"
export BO_LOADED=
export BO_IS_SOURCING=
export BO_sourceProfile__sourced=
}
###############################
# Boot self
###############################
if [ -z "$BO_PREFIX_DIR" ]; then
function useFallbackPrefix {
# We fall back to '~/.bash.origin.cache/'
BO_ensure_env_HOME "Use default '$BO_DIR/.bash.origin.cache' for BO_PREFIX_DIR"
export BO_PREFIX_DIR="$HOME/.bash.origin.cache"
[ -e "$BO_PREFIX_DIR" ] || mkdir "$BO_PREFIX_DIR"
}
# If we have a cache in our home dir we use that over all other
if [ -e "$HOME/.bash.origin.cache" ]; then
export BO_PREFIX_DIR="$HOME/.bash.origin.cache"
# If 'node' is from an NVM install we locate the cache into the active nvm 'lib' directory.
elif BO_has node && [ ! -z "$NVM_DIR" ] && [ ! -z "$NVM_BIN" ]; then
export BO_PREFIX_DIR="$(dirname $(dirname "$NVM_BIN"))"
elif [ -w "/usr/local" ]; then
# '/usr/local' is writable so we use that if we can create a cache directory below.
export BO_PREFIX_DIR="/usr/local"
if [ ! -e "/usr/local/lib/bash.origin.cache" ]; then
mkdir -p "/usr/local/lib/bash.origin.cache" || useFallbackPrefix
fi
else
useFallbackPrefix
fi
fi
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] BO_PREFIX_DIR: $BO_PREFIX_DIR"
# NOTE: We no longer source the profile automatically!
if [ -z "$BO_ENABLE_SOURCE_PROFILE" ]; then
BO_ENABLE_SOURCE_PROFILE=0;
fi
BO_sourceProfile
eval BO_SELF_BASH_SOURCE="$BO_READ_SELF_BASH_SOURCE"
BO_deriveSelfDir ___TMP___ "$BO_SELF_BASH_SOURCE"
export BO_DIR="$___TMP___"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] BO_DIR: $BO_DIR"
BO_checkVerbose "VERBOSE" "$@"
export VERBOSE
export BO_VERBOSE
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[bash.origin] BO_VERBOSE: ${BO_VERBOSE}"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[bash.origin] VERBOSE: ${VERBOSE}"
export BO_LOADED=1
# NOTE: Variables starting with '_BO_' are for primarily internal use
# and should not be set externally other than to force a specific
# internal state for debugging purposes.
if [ -z "$_BO_OPTIMIZED" ]; then
# If not set we set it based on BO_VERBOSE.
if [ -z "$BO_VERBOSE" ]; then
export _BO_OPTIMIZED=1
else
export _BO_OPTIMIZED=0
fi
fi
if echo "$@" | grep -q -Ee '(\$|\s)--compile(\s|\$)'; then
export _BO_OPTIMIZED=0
elif echo "$npm_config_argv" | grep -q -Ee '"--compile"'; then
export _BO_OPTIMIZED=0
fi
###############################
# Delegate if requested
###############################
if [ "$BO_ROOT_SCRIPT_PATH" != "" ]; then
if [ "$1" != "BO" ]; then
if [ "$2" != "install" ]; then
if [ -e "$BO_ROOT_SCRIPT_PATH" ]; then
local ORIGINAL_BO_ROOT_SCRIPT_DIR
BO_deriveSelfDir ORIGINAL_BO_ROOT_SCRIPT_DIR "$BO_ROOT_SCRIPT_PATH"
if [ "$BO_DIR" != "$ORIGINAL_BO_ROOT_SCRIPT_DIR" ]; then
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] Delegate to BO_ROOT_SCRIPT_PATH: $BO_ROOT_SCRIPT_PATH"
# Delegate to the Bash.Origin script at 'BO_ROOT_SCRIPT_PATH'
# which was set before calling us.
export BO_LOADED=
. "$BO_ROOT_SCRIPT_PATH"
return;
fi
else
BO_ensure_env_HOME "Delegate to BO_ROOT_SCRIPT_PATH"
if [ "$BO_ROOT_SCRIPT_PATH" != "$BO_PREFIX_DIR/bin/bash.origin" ]; then
echo >&2 "[bash.origin] ERROR: 'BO_ROOT_SCRIPT_PATH' ($BO_ROOT_SCRIPT_PATH) not found!"
exit 1
fi
fi
fi
fi
else
if [ -e "$BO_DIR/bash.origin" ]; then
BO_ROOT_SCRIPT_PATH="$BO_DIR/bash.origin"
elif [ -e "$BO_DIR/.bash.origin" ]; then
BO_ROOT_SCRIPT_PATH="$BO_DIR/.bash.origin"
else
BO_ROOT_SCRIPT_PATH="$BO_PREFIX_DIR/bin/bash.origin"
fi
fi
export BO_ROOT_SCRIPT_PATH
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] BO_ROOT_SCRIPT_PATH: $BO_ROOT_SCRIPT_PATH"
function BO_ensure_bo_script_bin {
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin][BO_ensure_bo_script_bin] args: $@"
# Ensure BO_ROOT_SCRIPT_PATH exists
if [ ! -e "$BO_ROOT_SCRIPT_PATH" ]; then
local _binBaseDir="$1"
# Ensure 'bash.origin.script' exists
if [ ! -e "$_binBaseDir/bash.origin.script" ]; then
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] Placing wrapper script for 'bash.origin.script' at '$_binBaseDir/bash.origin.script'"
# TODO: Dynamically feed based on 'BO_resetLoaded'
echo -e "#!/usr/bin/env bash\nBO_LOADED=\nBO_IS_SOURCING=\nBO_sourceProfile__sourced=\n__ON_INIT__BO_IS_SOURCING=\n. $BO_ROOT_SCRIPT_PATH BOS $""@" > "$_binBaseDir/bash.origin.script"
chmod u+x "$_binBaseDir/bash.origin.script"
fi
# Ensure 'bash.origin' exists
if [ ! -e "$_binBaseDir/bash.origin" ]; then
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] Placing wrapper script for 'bash.origin' at '$_binBaseDir/bash.origin'"
# TODO: Dynamically feed based on 'BO_resetLoaded'
echo -e "#!/usr/bin/env bash\nBO_LOADED=\nBO_IS_SOURCING=\nBO_sourceProfile__sourced=\n__ON_INIT__BO_IS_SOURCING=\n. $BO_ROOT_SCRIPT_PATH BOB $""@" > "$_binBaseDir/bash.origin"
chmod u+x "$_binBaseDir/bash.origin"
fi
fi
}
BO_ensure_bo_script_bin "$(dirname "$BO_ROOT_SCRIPT_PATH")"
###############################
# Declare Globals
###############################
if [ "$BO_IS_SOURCING" == "1" ]; then
__ON_INIT__BO_IS_SOURCING="1"
else
__ON_INIT__BO_IS_SOURCING="0"
fi
BO_ROOT_SOURCE_URL="https://raw.githubusercontent.com/bash-origin/bash.origin/master/bash.origin"
if [ -z "$BO_GLOBAL_SYSTEM_CACHE_DIR" ]; then
BO_ensure_env_HOME "Use default '$BO_PREFIX_DIR/lib/bash.origin.cache'"
BO_GLOBAL_SYSTEM_CACHE_DIR="$BO_PREFIX_DIR/lib/bash.origin.cache"
fi
export BO_GLOBAL_SYSTEM_CACHE_DIR
if [ -z "$BO_SYSTEM_CACHE_DIR" ]; then
BO_SYSTEM_CACHE_DIR="$BO_GLOBAL_SYSTEM_CACHE_DIR"
fi
export BO_SYSTEM_CACHE_DIR
if [ -z "$BO_VERSION_NVM" ]; then
BO_VERSION_NVM="0.33.4"
fi
function BO_major_node_version {
if ! BO_has node; then
#BO_exit_error "'node' command not found! If you want to provision it dynamically, set 'BO_VERSION_NVM_NODE'."
echo ""
else
echo "$(node --version 2>&1 | perl -pe 's/^v(\d+).+$/$1/')"
fi
}
if [[ $BO_ASSUME_NODE != 1 ]]; then
if [ ! -z "$BO_VERSION_NODE" ]; then
# NOTE: This code will only run ONCE!
# If 'BO_VERSION_NODE' is set we make sure node is found and
# the version matches and then never check it again.
if ! BO_has node; then
BO_exit_error "'node' command not found! If you want to provision it dynamically, set 'BO_VERSION_NODE' et.al."
fi
if [ "$(BO_major_node_version)" != "$BO_VERSION_NODE" ]; then
BO_exit_error "'node' version '$(BO_major_node_version)' from environment does not match version '$BO_VERSION_NODE' declared by 'BO_VERSION_NODE'. Make sure to only set the MAJOR version!"
fi
# From now on we do not check the 'node' version again
# and just assume we have the correct version set
# by the virture of:
export BO_ASSUME_NODE=1
export BO_VERSION_NVM_NODE="$BO_VERSION_NODE"
fi
fi
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] BO_VERSION_NODE: $BO_VERSION_NODE"
# Holds the node version that was detected from outside.
function BO_original_node_version {
if [ -z "$ORIGINAL_BO_VERSION_NODE" ]; then
ORIGINAL_BO_VERSION_NODE="$(BO_major_node_version)"
fi
echo "$ORIGINAL_BO_VERSION_NODE"
}
if [ -z "$BO_VERSION_NVM_NODE" ]; then
BO_VERSION_NVM_NODE="$(BO_original_node_version)"
fi
if [ -z "$BO_VERSION_RECENT_NODE" ]; then
# NOTE: The idea of 'BO_VERSION_RECENT_NODE' is to hold a most recent version
# of 'node' while 'BO_VERSION_NVM_NODE' holds the main *system* version.
BO_VERSION_RECENT_NODE="$BO_VERSION_NVM_NODE"
fi
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] ORIGINAL_BO_VERSION_NODE: $ORIGINAL_BO_VERSION_NODE"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] BO_VERSION_NVM_NODE: $BO_VERSION_NVM_NODE"
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] BO_VERSION_RECENT_NODE: $BO_VERSION_RECENT_NODE"
if [ -z "$BO_VERSION_NVM_IOJS" ]; then
# DEPRECATED
BO_VERSION_NVM_IOJS="iojs-v3.0.0"
fi
if [ -z "$BO_VERSION_SMI_CLI" ]; then
BO_VERSION_SMI_CLI="0.35.14"
fi
[ -z "$BO_VERBOSE" ] || echo >&2 "[bash.origin] BO_GLOBAL_SYSTEM_CACHE_DIR: $BO_GLOBAL_SYSTEM_CACHE_DIR"
###############################
# Declare Utility Functions
###############################
function BO_now {
BO_setResult "$1" "$(date +%s)"
}
function BO_format {
if [ "$1" != "1" ]; then
return;
fi
if [ "$2" == "HEADER" ]; then
echo >&2 "##################################################";
echo >&2 "# $3";
echo >&2 "##################################################";
fi
if [ "$2" == "FOOTER" ]; then
echo >&2 "##################################################";
echo >&2 ""
fi
}
function BO_replace {
echo -n "$(echo "$1" | perl -pe "s/$2/\$1/")"
}
function BO_test {
if [ "$(echo "$1" | perl -wnE "say for /($2)/")" == "" ]; then
return 1
fi
return 0
}
function BO_ensure_dir {
if [ ! -e "$1" ]; then
mkdir -p "$1"
fi
}
function BO_ensure_parent_dir {
if [ ! -e "$(dirname "$1")" ]; then
mkdir -p "$(dirname "$1")"
fi
}
function BO_ensure_empty_path {
if [ ! -e "$1" ]; then
# Path does not exist
BO_ensure_dir "$1"
return 0;
fi
# Path exists; check if empty
# @source https://stackoverflow.com/a/20456797/330439
if find "$1" -mindepth 1 -print -quit | grep -q .; then
# has content
return 1;
fi
# is empty
return 0;
}
function BO_find_path_up {
local path=$1
while [[ "$path" != "" && ! -e "$path/$2" ]]; do
path=${path%/*}
done
echo "$path/$2"
}
function BO_mark_time {
# TODO: Refactor to setup a rolling timer.
local cmd="$1"
local timer_format
if [[ -n "$2" ]]; then
timer_format="$2"
else
timer_format='%-Mm %-Ss'
fi
case $cmd in
start)
# This can't be local because we need it to persist across calls.
_timer_StartSecs=$(date +'%s')
;;
stop)
if [[ ! $_timer_StartSecs ]]; then
echo "$FUNCNAME did not record a start." >&2
return 1
fi
local _timer_StopSecs=$(date +'%s')
local DiffSecs=$(($_timer_StopSecs-$_timer_StartSecs))
local TimeLapse=$(date -u -d@"$DiffSecs" +"$timer_format")
echo "$TimeLapse"
;;
*)
echo "$FUNCNAME: Invalid command '$cmd'." >&2
return 1
;;
esac
}
if echo "$@" | grep -q -Ee '(\$|\s)--install-only(\s|\$)'; then
BO_FLAG_INSTALL_ONLY=1
elif echo "$npm_config_argv" | grep -q -Ee '"--install-only"'; then
BO_FLAG_INSTALL_ONLY=1
fi
function BO_exit_if_install_only {
installFunc="$1"
function doInstall {
if [ ! -z "$installFunc" ]; then
"$installFunc"
fi
}
function doExit {
doInstall
exit 0
}
if [[ $BO_FLAG_INSTALL_ONLY == 1 ]]; then
doExit
elif echo "$@" | grep -q -Ee '(\$|\s)--install-only(\s|\$)'; then
doExit
elif echo "$npm_config_argv" | grep -q -Ee '"--install-only"'; then
doExit
else
doInstall
fi
}
function BO_sudo {
# TODO: Detect if we even need to ask for sudo. If not just run command.
local NEED_TO_ASK="1"
if [ "$NEED_TO_ASK" == "1" ]; then
echo >&2 "$BO_LOG_PREFIX""WARNING: Asking for sudo access to run: $1"
fi
sudo $1
}
function BO_parse_args {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_parse_args] args: $@"
# TODO: Relocate this into external module so we can install 'minimist'
# or use pure bash implementation.
BO_ensureInSystemCache "__minimistPath" "github.com/substack/minimist" "1.2.0" "https://github.com/substack/minimist/archive/1.2.0.zip"
local parsedArgs=$(echo -e $(BO_run_silent_node --eval '
const PATH = require("path");
const FS = require("fs");
const MINIMIST = require("'$__minimistPath'");
const VAR_PREFIX = process.argv[1];
var vars = {};
function exportVar (name, value) {
name = name.replace(/-/g, "_");
vars[VAR_PREFIX + name] = ("" + value);
}
const rawArgsArray = process.argv.slice(2);
const argsArray = [];
var argState = null;
rawArgsArray.forEach(function (arg) {
if (argState === "concat") {
argsArray[argsArray.length-1] += " " + arg;
if (!/^"/.test(arg) && /"$/.test(arg)) {
argState = null;
argsArray[argsArray.length-1] = argsArray[argsArray.length-1].replace(/^"(.+)"$/, "$1");
}
} else {
argsArray.push(arg);
if (/^--/.test(arg)) {
argState = "opt";
}
if (argState === "opt") {
if (/^"/.test(arg) && !/"$/.test(arg)) {
argState = "concat";
}
}
}
});
exportVar("_0", argsArray.join(" "));
const args = MINIMIST(argsArray);
Object.keys(args).forEach(function (name) {
if (name === "_") {
args[name].forEach(function (value, i) {
exportVar("_" + (i + 1), value);
});
} else {
exportVar("_OPT_" + name, args[name]);
}
});
Object.keys(vars).forEach(function (name) {
process.stdout.write(name + "=\"" + vars[name] + "\"\\n");
});
' "$@"))
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "$parsedArgs"
eval "$parsedArgs" || {
BO_log "1" "ARGS: $parsedArgs"
}
}
function BO_compileModule {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_compileModule] args: $@"
# TODO: Resolve './' and aliased paths based on mappings.
local _raw_module_source_path="$2"
local _compiled_module_source_path="$(dirname "$2")/.~$(basename "$2")~compiled.module.bash.sh"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_compileModule] _raw_module_source_path: $(stat -f "%m%t%Sm %N" $_raw_module_source_path)"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_compileModule] _compiled_module_source_path: $(stat -f "%m%t%Sm %N" $_compiled_module_source_path) 2> /dev/null"
# If source file is newer than compiled file
# TODO: Compile if 'bash.origin.modules' plugin implementation directory is newer than compiled module.
if [ ! -e "${_compiled_module_source_path}" ] || [ $_BO_OPTIMIZED == 0 ] || [ ${_raw_module_source_path} -nt ${_compiled_module_source_path} ]; then
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_compileModule] Compile source '${_raw_module_source_path}' to '${_compiled_module_source_path}' (pwd: $(pwd), _BO_OPTIMIZED: $_BO_OPTIMIZED)"
BO_callPlugin "bash.origin.modules" compile ${_raw_module_source_path} ${_compiled_module_source_path}
else
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_compileModule] Skip compile as source has not changed."
fi
BO_setResult "$1" "${_compiled_module_source_path}"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_compileModule] DONE"
}
function BO_requireModule {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_requireModule] args: $@"
if [[ "${BASH_VERSION}" != "4."* ]] && [[ "${BASH_VERSION}" != "5."* ]]; then
echo >&2 "$BO_LOG_PREFIX""ERROR: You must be using bash version 4 or newer to use 'BO_requireModule' (yours is '${BASH_VERSION}')! Put this '#!/usr/bin/env bash' shebang at the top of the script file you are executing to get the 'bash' command from your PATH instead of default for your shell."
exit 1
fi
if [ "$2" != "as" ]; then
echo >&2 "$BO_LOG_PREFIX""ERROR: Second argument to BO_requireModule should be 'as'!"
exit 1
fi
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_requireModule] ___bo_module_instance_caller_dirname___: $___bo_module_instance_caller_dirname___"
varName=""
if [ ! -z "$___bo_module_instance_caller_dirname___" ]; then
varName="$3__CALLER_DIRNAME__"
export ${varName}="$___bo_module_instance_caller_dirname___"
fi
if [[ "${1}" == "."* ]]; then
# Relative include based on PWD is used from outside a module
# and based on path of parent module if used from inside module.
BO_sourceModule "$(pwd)/$1"
elif [[ "${1}" == "@"* ]]; then
if [[ "${1}" == "@com.github/"* ]] || [[ "${1}" == "@github.com~"* ]]; then
if [[ "${1}" == "@com.github/"* ]]; then
local PLUGIN_URI_=`BO_run_silent_node --eval '
var uriParts = process.argv[1].match(/^@([^\/]+)\/(.+)#(?:.+\/)?(.+)$/);
uriParts[1] = uriParts[1].split(".");
uriParts[1].reverse();
uriParts[1] = uriParts[1].join(".");
process.stdout.write(uriParts[1] + "/" + uriParts[2]);
' "${1}"`
local PLUGIN_URI_SUB_PATH_=""
local PLUGIN_VERSION_=`BO_run_silent_node --eval '
var uriParts = process.argv[1].match(/^@([^\/]+)\/(.+)#(?:.+\/)?(.+)$/);
process.stdout.write(uriParts[3]);
' "${1}"`
else
local PLUGIN_URI_=`BO_run_silent_node --eval '
var uriParts = process.argv[1].match(/^@([^\/]+)\/(.+)#(?:.+\/)?(.+)$/);
process.stdout.write(uriParts[1]);
' "${1}"`
local PLUGIN_URI_SUB_PATH_=`BO_run_silent_node --eval '
var uriParts = process.argv[1].match(/^@([^\/]+)\/(.+)#(?:.+\/)?(.+)$/);
process.stdout.write(uriParts[2]);
' "${1}"`
local PLUGIN_VERSION_=`BO_run_silent_node --eval '
var uriParts = process.argv[1].match(/^@([^\/]+)\/(.+)#(?:.+\/)?(.+)$/);
process.stdout.write(uriParts[3]);
' "${1}"`
fi
local PLUGIN_PATH_
BO_ensurePlugin "PLUGIN_PATH_" "$PLUGIN_URI_"
PLUGIN_PATH_="$(dirname "${PLUGIN_PATH_}")"
if [ "${PLUGIN_URI_SUB_PATH_}" != "" ]; then
PLUGIN_PATH_="${PLUGIN_PATH_}/${PLUGIN_URI_SUB_PATH_}"
fi
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_requireModule] Using ensured plugin path: $PLUGIN_PATH_"
local PLUGIN_MODULE_NAME_="_#_org.bashorigin_#_${PLUGIN_VERSION_}.sh"
local PLUGIN_MODULE_PATH_="${PLUGIN_PATH_}/${PLUGIN_MODULE_NAME_}"
if [ ! -e "$PLUGIN_MODULE_PATH_" ]; then
echo >&2 "$BO_LOG_PREFIX""ERROR: Module '$PLUGIN_MODULE_NAME_' does not exist in plugin at path '$PLUGIN_MODULE_PATH_'!"
# DEPRECATED
PLUGIN_MODULE_NAME_="org.bashorigin-${PLUGIN_VERSION_}.sh"
PLUGIN_MODULE_PATH_="${PLUGIN_PATH_}/${PLUGIN_MODULE_NAME_}"
if [ ! -e "$PLUGIN_MODULE_PATH_" ]; then
echo >&2 "$BO_LOG_PREFIX""ERROR: Module '$PLUGIN_MODULE_NAME_' does not exist in plugin at path '$PLUGIN_MODULE_PATH_'!"
exit 1
fi
fi
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_requireModule] Load module from plugin: $PLUGIN_MODULE_PATH_"
BO_sourceModule "$PLUGIN_MODULE_PATH_"
elif [[ "${1}" == "@/"* ]]; then
local PLUGIN_PATH_=`BO_run_silent_node --eval '
var uriParts = process.argv[1].match(/^@(.+)#(?:.+\/)?(.+)$/);
process.stdout.write(uriParts[1]);
' "${1}"`
local PLUGIN_VERSION_=`BO_run_silent_node --eval '
var uriParts = process.argv[1].match(/^@(.+)#(?:.+\/)?(.+)$/);
process.stdout.write(uriParts[2]);
' "${1}"`
local PLUGIN_MODULE_PATH_="$PLUGIN_PATH_/_#_org.bashorigin_#_${PLUGIN_VERSION_}.sh"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_requireModule] Load module from plugin: $PLUGIN_MODULE_PATH_"
BO_sourceModule "$PLUGIN_MODULE_PATH_"
else
echo >&2 "ERROR: No actor found for URI: ${1}"
exit 1
fi
elif [[ "${1}" == "/"* ]]; then
local PLUGIN_MODULE_PATH_="${1}"
BO_sourceModule "$PLUGIN_MODULE_PATH_"
else
if which lib.json > /dev/null; then
local PLUGIN_URI_=`BO_run_silent_node --eval '
var uriParts = process.argv[1].match(/^(.+)#(?:.+\/)?(.+)$/);
process.stdout.write(uriParts[1]);
' "${1}"`
local PLUGIN_VERSION_=`BO_run_silent_node --eval '
var uriParts = process.argv[1].match(/^(.+)#(?:.+\/)?(.+)$/);
process.stdout.write(uriParts[2]);
' "${1}"`
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_requireModule] Resolving PLUGIN_URI_ via lib.json"
PLUGIN_PATH_=$(lib.json resolve.js "$PLUGIN_URI_/package.json" || true)
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_requireModule] PLUGIN_PATH_: $PLUGIN_PATH_"
if [ "$PLUGIN_PATH_" == "" ]; then
echo >&2 "ERROR: Could not resolve '$PLUGIN_URI_' via lib.json!"
exit 1
fi
local PLUGIN_MODULE_PATH_="$(dirname "$PLUGIN_PATH_")/_#_org.bashorigin_#_${PLUGIN_VERSION_}.sh"
BO_sourceModule "$PLUGIN_MODULE_PATH_"
else
echo >&2 "TODO: Non lib.json alias-based includes that should resolve via mappings: $@"
exit 1
fi
fi
local func="$2"
shift
shift
"$func" "$@"
}
function BO_sourceModule {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceModule] args: $@"
local _raw_source_path="$1"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceModule] _raw_source_path: $_raw_source_path"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourceModule] pwd: $(pwd)"
BO_compileModule "_raw_source_path" "${_raw_source_path}"
BO_sourcePrototype "${_raw_source_path}" ${*:2}
}
function BO_sourcePrototype {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] args: $@"
local _raw_source_path="$1"
# Check if we have a module extension and if we do compile the module first.
if [[ "${_raw_source_path}" == *".bo.sh" ]]; then
BO_compileModule "_raw_source_path" "${_raw_source_path}"
fi
# Now load the module
local _proto_source_path="${_raw_source_path}"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] _proto_source_path (1): ${_proto_source_path}"
if [ -d "${_proto_source_path}" ]; then
_proto_source_path="${_proto_source_path}/$(basename "${_proto_source_path}")"
fi
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] _proto_source_path (2): ${_proto_source_path}"
if [ ! -e "${_proto_source_path}" ]; then
_proto_source_path="${_proto_source_path}.sh"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] _proto_source_path (3): ${_proto_source_path}"
if [ ! -e "${_proto_source_path}" ]; then
echo "$BO_LOG_PREFIX""ERROR: Cannot source '${_raw_source_path}' nor '${_proto_source_path}'. File not found!"
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] DONE:1"
return 1
fi
fi
local __ON_INIT__BO_IS_SOURCING
#echo "ENV: `( set -o posix ; set ) | less | grep BO_IS_SOURCING | while read line; do echo "___$line";done`"
local __PREVIOUS_BO_IS_SOURCING="$BO_IS_SOURCING"
local __PREVIOUS_BO_LOADED="$BO_LOADED"
# TODO: Reset all relevant variables.
BO_IS_SOURCING="1"
BO_LOADED=1
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] ${_proto_source_path}"
if [ "${*:2}" == "" ]; then
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] Sourcing '${_proto_source_path}' using call method 1"
# NOTE: If no arguments specified the source path becomes part of the arguments
# so we provide an empty one which fixes this for bash.
. "${_proto_source_path}" ""
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] done: Sourcing '${_proto_source_path}' using call method 1"
else
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] Sourcing '${_proto_source_path}' using call method 2"
. "${_proto_source_path}" ${*:2}
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] done: Sourcing '${_proto_source_path}' using call method 2"
fi
# TODO: Restore all relevant variables.
BO_IS_SOURCING="$__PREVIOUS_BO_IS_SOURCING"
BO_LOADED="$__PREVIOUS_BO_LOADED"
#while IFS= read -r -d $'\0' line; do
# export "$line"
#done < <(bash -s <<< 'export VARNAME=something; env -0')
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "[BO_sourcePrototype] DONE"
}
function BO_followPointer {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "BO_followPointer($1, $2, $3)"
_POINTER_RESULT_VAR="$1"
_POINTER_LOOKUP_FILENAME="$3"
_POINTER_RESOLVED="0"
function checkPath {
[ -z "$BO_VERBOSE" ] || BO_log "$BO_VERBOSE" "BO_followPointer:checkPath($1)"
if [ -d "$1/$_POINTER_LOOKUP_FILENAME" ] || [ -h "$1/$_POINTER_LOOKUP_FILENAME" ]; then
checkPath "$1/$_POINTER_LOOKUP_FILENAME"
elif [ -e "$1/$_POINTER_LOOKUP_FILENAME" ]; then
POINTER_VALUE=`cat $1/$_POINTER_LOOKUP_FILENAME`
if [ "$POINTER_VALUE" == "." ]; then
BO_realpath "$_POINTER_RESULT_VAR" "$1"
_POINTER_RESOLVED="1"
else
checkPath "$1/$POINTER_VALUE"
fi
fi
}
checkPath "$2"
if [ "$_POINTER_RESOLVED" == "0" ]; then
echo >&2 "ERROR: Could not resolve pointer '$_POINTER_LOOKUP_FILENAME' for path '$2'"
exit 1;
fi
}
function BO_realpath {
if BO_has "realpath"; then
BO_setResult "$1" "$(realpath "$2")"