shell-base/load.sh

148 lines
4.5 KiB
Bash
Raw Normal View History

#!/bin/bash
# [[ ! $- == *i* ]] && exec >> ~/logs/load.log && exec 2>&1
# export BASH_DEBUG=true
function nilog () {
[[ ! $- == *i* ]] && echo -e "-----\n $USER $*" &>> ~/logs/load.log
}
nilog "called load.sh $(date)"
# nilog caller: $(caller)
# nilog pid: $(ps -o comm= $PPID)
# don't bother if bash is not the shell
[ ! "$SHELL" = "/bin/bash" ] && return 1
DIR=${1:-$(dirname ${BASH_SOURCE[0]})}
[[ $(declare -F | grep module_load) ]] || source "$BASH_SHELL_BASE/module.lib"
# uncomment for debuggin
# echo $USER running load script in $DIR
# echo callers
# caller
# echo $(ps -o comm= $PPID)
# echo -----
# echo $BASH_SHELL_DIRS
module_load file
[[ $? -ne 0 ]] && echo unable to access the file module, aboarting load && return 1
module_load debug
if [[ $? -ne 0 ]]; then
echo "unable to load a 'debug' module using a noop for debug function"
# noop
function debug () {
:
}
fi
([[ $BASH_ENV ]] && [[ ! $- == *i* ]] && [[ $BASH_USE_ALIAS ]]) && shopt -s expand_aliases
function shell_process_directory () {
local SUBDIRS
local DIR
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 [[ $DIR != "$BASH_SHELL_BASE" ]] && [[ -f $DIR/load.sh ]]; then
# echo "$DIR/load.sh" found, running instead of default load
source "$DIR/load.sh"
else
# default processing
local SUBDIRS
local IGNORE_FILE
SUBDIRS=$([[ -f "$DIR/.bash-shell-include" ]] && cat "$DIR/.bash-shell-include" || echo $BASH_SHELL_BASE_SUBDIRS)
[[ $SSH_SESSION ]] && SUBDIRS+=" ssh/session"
IGNORE_FILE="$([[ -f "$DIR/.bash-shell-ignore" ]] && echo "$DIR" || echo "$BASH_SHELL_BASE")/.bash-shell-ignore"
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
source_dir -f "$IGNORE_FILE" -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
# add in any network directories
declare networks
source "$HOME/.bashrc"
networks="$HOME/${BASH_SHELL_USER:-"bash/shell"}/.networks"
NETWORK_DOMAINS="$([[ -f $networks ]] && cat $networks) "
# These will be loaded for all users
networks="$BASH_SHELL_HOST/$(hostname)/.networks"
NETWORK_DOMAINS+="$([[ -f $networks ]] && cat $networks)"
NETWORK_DOMAINS=(${NETWORK_DOMAINS})
if [[ ! $NETWORK_DOMAINS = "" ]]; then
declare domain
BASH_SHELL_NETWORK_DIRS=""
for domain in "${NETWORK_DOMAINS[@]}"; do
# echo processing ${BASH_SHELL_NETWORK}/${domain}
if [[ ${domain} = !* ]]; then
domain=${domain/#!/}
export NETWORK_HOME=$domain
fi
if [[ -d ${BASH_SHELL_NETWORK}/${domain} ]]; then
BASH_SHELL_NETWORK_DIRS+="${BASH_SHELL_NETWORK}/${domain} "
fi
done
unset domain
export BASH_SHELL_NETWORK_DIRS
BASH_SHELL_DIRS+=" ${BASH_SHELL_NETWORK_DIRS}"
fi
# echo bash shell dirs: $BASH_SHELL_DIRS
dirs=${1:-$BASH_SHELL_DIRS}
BASH_SHELL_BASE_SUBDIRS=$(cat "$BASH_SHELL_BASE/.bash-shell-include")
# echo subdir includes: $BASH_SHELL_BASE_SUBDIRS
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