shell-base/setup.sh

78 lines
2.4 KiB
Bash
Executable File

#!/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/setup.sh" ] && [ "$DIR" != "$BASH_SHELL_BASE" ]; then
echo "$DIR/setup.sh" found, running instead of default processing
. "$DIR/setup.sh"
else
# default processing
local SUBDIRS
local DSUBDIRS
# default subdirectories to source
DSUBDIRS="env function 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
source_dir -p "archive" -x '"*.off" "*.md"' -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