shell-base/load.sh

80 lines
2.6 KiB
Bash
Raw Normal View History

#!/bin/bash
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!"
else
. $BASH_SHELL_BASE/source-dir.func
# source the default shopt options
[ -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_SOURCED" = "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 env alias 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_SOURCED
shell_process_directory
export BASH_SHELL_BASE_SOURCED=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)"
# process user
# Note: $HOME/shell or $HOME/BASH_SHELL_USER are sourced via $HOME/.bashrc
# echo $(envg BASH) | xargs -n 1
fi