shell-base/load.sh

123 lines
3.5 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]})}
# uncomment for debuggin
# echo $USER running load script in $DIR
# echo callers
# caller
# echo $(ps -o comm= $PPID)
# echo -----
# echo $BASH_SHELL_DIRS
# 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
function shell_get_ignores () (
local file=${1:-"$BASH_SHELL_BASE"}/.bash-shell-ignore
[[ ! -f "$file" ]] && [[ $1 ]] && return 1
local excludes=''
local ignores=$(cat "$file")
for exclude in $ignores ; do
excludes+='"'$exclude'" '
done
echo $excludes
return 0
)
function shell_process_directory () {
local SUBDIRS
local DIR
local DSUBDIRS
local excludes
DIR=${1:-$BASH_SHELL_BASE}
# echo soucring directory $DIR
if [[ $DIR = "$BASH_SHELL_BASE" ]]; then
BASH_SHELL_IGNORE=$(shell_get_ignores)
excludes=$BASH_SHELL_IGNORE
else
excludes=$(shell_get_ignores $DIR)
[[ $? -ne 0 ]] && excludes=$BASH_SHELL_IGNORE
fi
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/load.sh" ] && [ "$DIR" != "$BASH_SHELL_BASE" ]; then
# echo "$DIR/$BASH_SHELL_LOAD" found, running instead of default processing
source "$DIR/load.sh"
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
source_dir -p "archive" -x "$excludes" -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
# echo bash shell dirs: $BASH_SHELL_DIRS
DIRS=${1:-$BASH_SHELL_DIRS}
for dir in $DIRS; do
# echo $dir
shell_process_directory $dir
[[ $dir == "$BASH_SHELL_BASE" ]] && BASH_SHELL_BASE_LOADED=true
done
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