shell-base/module.base

57 lines
1.4 KiB
Plaintext
Raw Permalink Normal View History

module_confirm() {
local FILE
local DIR
local NAME
DIR=$1
NAME=$2
FILE="$($(which find) -L ${DIR} -type f -name "${NAME}.mod" -o -name "${NAME}".lib -o -name "${NAME}".func -o -name "${NAME}".sh -o -name "${NAME}".env)"
COUNT=0
if [ "$FILE" ]; then
COUNT=$(echo $FILE | xargs -n 1 | wc -l)
fi
if [ $COUNT -gt 1 ]; then
>&2 echo two or more modules of same name found, aborting
>&2 echo $FILE | xargs -n 1
return 1
fi
[[ $COUNT == 1 ]] && echo $FILE
}
module_find() {
[ ! $1 ] && >&2 echo "no module specified" && return 1
local MDIRS
local MDIR
local DIRS
local MODULE=$1
DIRS=( ${BASH_SHELL_DIRS:-$BASH_SHELL_BASE} ${BASH_SHELL_NETWORKS_LOADED} )
[ -d BASH_SHELL_USER_DIR ] && DIRS=("${DIRS[@]}" "BASH_SHELL_USER_DIR")
[ -d $BASH_SHELL_DEV ] && DIRS=("${DIRS[@]}" "$BASH_SHELL_DEV")
DIRS=("${DIRS[@]/%/\/modules}")
DIRS=("${MODULE_DIRS[@]}" "${DIRS[@]}")
cnt=${#DIRS[@]}
for ((i=1;i<=cnt;i++)); do
DIR="${DIRS[cnt-i]}"
if [[ -d $DIR ]]; then
RES=$(module_confirm "$DIR" "$MODULE")
[ $? -eq 0 ] && echo $RES && return 0
fi
done
>&2 echo no module found anywhere
return 1
}
module_load() {
local module;local mpath
[ ! $1 ] && >&2 echo "no module specified" && return 1
local FILE
for module in "$@"
do
eval "_${module}_ () { echo "$fname test"; }"
mpath=$(module_find $module)
[ $? -ne 0 ] && >&2 echo extreme warning: no module $1 found, so was not loaded
source $mpath "$@"
done
}
shell_run () {
bash -ci ". ${1}"
}