Joey Armstrong | 7f382ef | 2023-01-25 12:00:08 -0500 | [diff] [blame] | 1 | # -*- sh -*- |
| 2 | ## ----------------------------------------------------------------------- |
| 3 | ## Intent: |
| 4 | ## o This script can be used as a one-liner for sourcing common scripts. |
| 5 | ## o Preserve command line arguments passed. |
| 6 | ## o Accept common.sh arguments specifying a set of libraries to load. |
| 7 | ## o Dependent common libraries will automatically be sourced. |
| 8 | ## ----------------------------------------------------------------------- |
| 9 | ## Usage: |
| 10 | ## o source common.sh --common-args-begin-- --tempdir |
| 11 | ## o source common.sh --common-args-begin-- --stacktrace |
| 12 | ## ----------------------------------------------------------------------- |
| 13 | |
| 14 | # __DEBUG_COMMON__=1 |
| 15 | [[ -v __DEBUG_COMMON__ ]] && echo " ** ${BASH_SOURCE[0]}: BEGIN" |
| 16 | |
| 17 | ## ----------------------------------------------------------------------- |
| 18 | ## Intent: Anonymous function used to source common shell libs |
| 19 | ## ----------------------------------------------------------------------- |
| 20 | ## Usage: source common.sh '--stacktrace' |
| 21 | ## ----------------------------------------------------------------------- |
| 22 | function __anon_func__() |
| 23 | { |
| 24 | local iam="${BASH_SOURCE[0]%/*}" |
| 25 | local cab='--common-args-begin--' |
| 26 | |
| 27 | declare -a args=($*) |
| 28 | |
| 29 | local raw |
| 30 | raw="$(readlink --canonicalize-existing --no-newline "${BASH_SOURCE[0]}")" |
| 31 | local top="${raw%/*}" |
| 32 | local common="${top}/common/sh" |
| 33 | |
| 34 | local arg |
| 35 | for arg in "${args[@]}"; |
| 36 | do |
| 37 | case "$arg" in |
| 38 | --tempdir) source "${common}"/tempdir.sh ;; |
| 39 | --traputils) source "${common}"/traputils.sh ;; |
| 40 | --stacktrace) source "${common}"/stacktrace.sh ;; |
| 41 | *) echo "ERROR ${BASH_SOURCE[0]}: [SKIP] unknown switch=$arg" ;; |
| 42 | esac |
| 43 | done |
| 44 | |
| 45 | return |
| 46 | } |
| 47 | |
| 48 | ##----------------## |
| 49 | ##---] MAIN [---## |
| 50 | ##----------------## |
| 51 | source "${BASH_SOURCE[0]%/*}/preserve_argv.sh" # pushd @ARGV |
| 52 | |
| 53 | if [ $# -gt 0 ] && [ "$1" == '--common-args-begin--' ]; then |
| 54 | shift # remove arg marker |
| 55 | fi |
| 56 | |
| 57 | if [ $# -eq 0 ]; then |
| 58 | # common.sh defaults |
| 59 | set -- '--tempdir' '--traputils' '--stacktrace' |
| 60 | fi |
| 61 | |
| 62 | __anon_func__ "$@" |
| 63 | unset __anon_func__ |
| 64 | source "${BASH_SOURCE[0]%/*}/preserve_argv.sh" # popd @ARGV |
| 65 | |
| 66 | [[ -v __DEBUG_COMMON__ ]] && echo " ** ${BASH_SOURCE[0]}: END" |
| 67 | : # NOP |
| 68 | |
| 69 | # [EOF] |