This repository has been archived on 2022-02-20. You can view files and clone it, but cannot push or open issues/pull-requests.
bash-shell-base/load.sh

96 lines
3.0 KiB
Bash
Executable File

#!/bin/bash
# don't bother if bash is not the shell
[ ! "$SHELL" = "/bin/bash" ] && return 1
DIR=${1:-$(dirname ${BASH_SOURCE[0]})}
# source the required directory source function
if [ ! -e "$BASH_SHELL_BASE/source-dir.func" ]; then
echo "!Unable to load sourcing function at $DIR/source-dir, shell not set up!"
return 1
else
source "$BASH_SHELL_BASE/source-dir.func"
fi
if [ ! -e "$BASH_SHELL_BASE/module.lib" ]; then
echo "!Unable to load module library functions function at $DIR/module.lib, shell not set up!"
return 1
else
source "$BASH_SHELL_BASE/module.lib"
fi
# REMOVED sourced via env directory
# [ -e "$DIR/options" ] && . $DIR/options
function shell_process_directory () {
local SUBDIRS
local DIR
local DSUBDIRS
DIR=${1:-$BASH_SHELL_BASE}
# echo soucring directory $DIR
if [ -d "$DIR" ]; then
if [ "$DIR" = "$BASH_SHELL_BASE" ] && [ "$BASH_SHELL_BASE_LOADED" = "true" ]; then
if [ -v PS1 ]; then
echo base directory already sourced
read -p "do you want to re-source the base (not recommended)? " -n 1 -r
echo
[[ ! $REPLY =~ ^[Yy]$ ]] && return 1
# else
# echo non-interactive shell
# return 1
fi
fi
if [ -f "$DIR/$BASH_SHELL_LOAD" ] && [ "$DIR" != "$BASH_SHELL_BASE" ]; then
# echo "$DIR/$BASH_SHELL_LOAD" found, running instead of default processing
source "$DIR/$BASH_SHELL_LOAD"
else
# default processing
local SUBDIRS
local DSUBDIRS
# default subdirectories to source
# TODO allow passing in additional directories
DSUBDIRS="function alias env misc lang app"
IFS=' ' read -r -a SUBDIRS <<< "${2:-$DSUBDIRS}"
# echo ${SUBDIRS[@]}
for SUBDIR in "${SUBDIRS[@]}"
do
if [ -e "$DIR/$SUBDIR" ]; then
# echo processing subdirectory $DIR/$SUBDIR
# must quote all globs to avoid bash glob expansion which is likely on
# TODO have default set of ignores plus check for .shell-ignore
source_dir -p "archive" -x '"*.off" "*.md" "*TODO*" "LICENSE" "*.sample"' -d 0 $DIR/$SUBDIR
# else
# echo no subdirectory $DIR/$SUBDIR to process, ignorning
fi
done
fi
# echo "done sourcing directory $DIR"
# else
# echo $DIR does not exist nothing to source
fi
}
# process the base directory by default
unset BASH_SHELL_BASE_LOADED
unset BASH_SHELL_LOADED
shell_process_directory # process base
export BASH_SHELL_BASE_LOADED=true
# process network
shell_process_directory "$BASH_SHELL_NETWORK/all"
[ $NETWORKNAME ] && shell_process_directory "$BASH_SHELL_NETWORK/$NETWORKNAME"
# process host
shell_process_directory "$BASH_SHELL_HOST/all"
shell_process_directory "$BASH_SHELL_HOST/$(hostname)"
export BASH_SHELL_LOADED=true
# process user
# Note: $HOME/shell or $HOME/BASH_SHELL_USER are sourced via $HOME/.bashrc
# echo $(envg BASH) | xargs -n 1