2020-11-06 14:24:10 -08:00
|
|
|
#!/bin/bash
|
2020-11-20 08:48:48 -08:00
|
|
|
# echo loading module library
|
2021-02-05 13:00:57 -08:00
|
|
|
|
|
|
|
module_confirm() {
|
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
local FILE
|
|
|
|
local DIR
|
|
|
|
local NAME
|
|
|
|
DIR=$1
|
|
|
|
NAME=$2
|
|
|
|
# echo finding $NAME in $DIR
|
2022-03-28 10:02:17 -07:00
|
|
|
# TODO use file instead for valid extensions
|
|
|
|
# allow explicit extension
|
|
|
|
FILE="$(command find -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
|
2020-11-06 14:24:10 -08:00
|
|
|
# echo files found $FILE
|
|
|
|
COUNT=0
|
|
|
|
if [ "$FILE" ]; then
|
|
|
|
COUNT=$(echo $FILE | xargs -n 1 | wc -l)
|
|
|
|
fi
|
|
|
|
# echo Number: $COUNT
|
|
|
|
if [ $COUNT -gt 1 ]; then
|
|
|
|
echo two or more modules of same name found, aborting
|
|
|
|
echo $FILE | xargs -n 1
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
[ $COUNT == 1 ] && echo $FILE && return 0
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
|
|
|
|
# if succesfull returns the path
|
2021-02-05 13:00:57 -08:00
|
|
|
module_find() {
|
2020-11-06 14:24:10 -08:00
|
|
|
|
|
|
|
[ ! $1 ] && echo "no module specified" && return 1
|
|
|
|
|
|
|
|
local MDIRS
|
|
|
|
local MDIR
|
|
|
|
local DIRS
|
|
|
|
local MODULE=$1
|
|
|
|
|
|
|
|
|
2022-02-20 12:44:29 -08:00
|
|
|
# MODULE_DIRS is array set in the environment and is an array of additional directories to
|
|
|
|
# search for modules. This makes is easy to include a custom module libraries outside
|
2022-03-09 17:02:39 -08:00
|
|
|
# the shell system. These take precedence over any modules found in shell directories below.
|
|
|
|
# Precedence is first in list to last.
|
2020-11-06 14:24:10 -08:00
|
|
|
|
2022-02-20 12:44:29 -08:00
|
|
|
if [[ $MODULE_DIRS ]]; then
|
|
|
|
for DIR in "${MODULE_DIRS[@]}"
|
|
|
|
do
|
|
|
|
if [[ -d $DIR ]]; then
|
|
|
|
RES=$(module_confirm "$DIR" "$MODULE")
|
|
|
|
[ $? -eq 0 ] && echo $RES && return 0
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
fi
|
2020-11-06 14:24:10 -08:00
|
|
|
|
2022-02-20 12:44:29 -08:00
|
|
|
# BASH_SHELL_DIRS holds shell directories to search for modules. If will be searched
|
|
|
|
# in revsere order, user or network repos over host over base
|
|
|
|
DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} )
|
|
|
|
[ -d $HOME/$BASH_SHELL_USER ] && DIRS=("${DIRS[@]}" "$HOME/$BASH_SHELL_USER")
|
2022-03-09 17:02:39 -08:00
|
|
|
[ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV")
|
2020-11-06 14:24:10 -08:00
|
|
|
|
2022-02-20 12:44:29 -08:00
|
|
|
cnt=${#DIRS[@]}
|
|
|
|
for ((i=1;i<=cnt;i++)); do
|
|
|
|
# find modules in reverse order so more specific ones override
|
|
|
|
DIR="${DIRS[cnt-i]}/modules"
|
|
|
|
if [[ -d $DIR ]]; then
|
|
|
|
RES=$(module_confirm "$DIR" "$MODULE")
|
2020-11-06 14:24:10 -08:00
|
|
|
[ $? -eq 0 ] && echo $RES && return 0
|
2022-02-20 12:44:29 -08:00
|
|
|
fi
|
2020-11-06 14:24:10 -08:00
|
|
|
done
|
2022-02-20 12:44:29 -08:00
|
|
|
# no module found anywhere
|
2020-11-06 14:24:10 -08:00
|
|
|
return 1
|
2022-02-20 12:44:29 -08:00
|
|
|
|
2020-11-06 14:24:10 -08:00
|
|
|
}
|
|
|
|
|
2021-02-05 13:00:57 -08:00
|
|
|
module_load() {
|
2020-11-06 14:24:10 -08:00
|
|
|
[ ! $1 ] && echo "no module specified" && return 1
|
2020-11-10 14:09:08 -08:00
|
|
|
# (return 0 2>/dev/null) && echo "module_load was sourced" || echo "module_log was executed"
|
2020-11-06 14:24:10 -08:00
|
|
|
local FILE
|
|
|
|
FILE=$(module_find $1)
|
|
|
|
[ $? -ne 0 ] && echo no module $1 found && return 1
|
|
|
|
# source $FILE "$0"
|
2022-02-26 17:34:33 -08:00
|
|
|
# [[ $BASHPID -eq $$ ]] || echo $FILE
|
2022-03-28 10:02:17 -07:00
|
|
|
shift 1
|
|
|
|
source $FILE "$@"
|
2020-11-06 14:24:10 -08:00
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2022-02-26 17:34:33 -08:00
|
|
|
module_print() {
|
|
|
|
[ ! $1 ] && echo "no module specified" && return 1
|
|
|
|
# (return 0 2>/dev/null) && echo "module_load was sourced" || echo "module_log was executed"
|
|
|
|
local FILE
|
|
|
|
FILE=$(module_find $1)
|
|
|
|
[ $? -ne 0 ] && echo no module $1 found && return 1
|
|
|
|
cat $FILE
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2021-02-05 13:00:57 -08:00
|
|
|
module_loaded() {
|
2020-11-06 14:24:10 -08:00
|
|
|
[ ! $1 ] && echo "no module specified" && return 1
|
|
|
|
local func
|
|
|
|
local ret
|
|
|
|
local file
|
|
|
|
ret=0
|
|
|
|
# function_list | grep module
|
|
|
|
file=$(module_find $1)
|
|
|
|
[ ! $file ] && echo no module $1 found && return 1
|
|
|
|
# echo "$(function_list $file)"
|
|
|
|
for func in $(function_list $file)
|
|
|
|
do
|
2020-11-07 10:01:17 -08:00
|
|
|
[ ! $(function_list | grep $func) ] && ret=1
|
|
|
|
[[ $2 = -vv ]] && ([[ $ret = 0 ]] && echo "$func loaded" || echo "$func not loaded")
|
2020-11-06 14:24:10 -08:00
|
|
|
done
|
2020-11-07 10:01:17 -08:00
|
|
|
[[ $2 = -v ]] && echo "module: $1" && ([[ $ret = 0 ]] && echo "all functions " || echo "some or all functions not loaded")
|
|
|
|
[[ $ret = 0 ]] && echo "loaded"
|
|
|
|
return $ret
|
2020-11-06 14:24:10 -08:00
|
|
|
}
|
|
|
|
|
2020-11-13 10:25:04 -08:00
|
|
|
# exported for login environment
|
2021-02-05 13:00:57 -08:00
|
|
|
# declare -f -x module_load
|
|
|
|
# declare -f -x module_find
|
|
|
|
# declare -f -x module_confirm
|
2020-11-13 10:25:04 -08:00
|
|
|
|
2021-02-05 13:00:57 -08:00
|
|
|
# uncomment for testing
|
|
|
|
# function _test_modules {
|
|
|
|
# RED='\033[0;31m'
|
|
|
|
# NC='\033[0m' # No Color
|
|
|
|
# echo module to test: $1
|
|
|
|
# echo --------------------
|
|
|
|
# echo -e "testing: ${RED}module_find${NC}"
|
|
|
|
# FILE=$(module_find $1)
|
|
|
|
# [ $? -ne 0 ] && echo no module $1 found && return 1
|
|
|
|
# echo module $FILE found
|
|
|
|
# # . $FILE
|
|
|
|
# echo ---------------
|
|
|
|
# echo -e "testing: ${RED}module_load${NC}"
|
|
|
|
# module_load $1
|
|
|
|
# [ $? -ne 0 ] && echo no module $1 found && return 1
|
|
|
|
# echo loaded module $1
|
|
|
|
# echo ----------------------
|
|
|
|
# echo -e "testing: ${RED}module_loaded${NC}"
|
|
|
|
# module_loaded $1
|
|
|
|
# }
|
2020-11-13 10:25:04 -08:00
|
|
|
|