#!/bin/bash

# PRIMARY SHELL LOAD SCRIPT - RUN FOR ALL INTERACTIVE SHELLS

# don't bother if bash is not the shell
[ ! "$SHELL" = "/bin/bash" ] && return 1

# ************ be sure module_load function is availABLE ***************************
[[ $(declare -F | grep module_load) ]] || source "$BASH_SHELL_BASE/module.lib"

module_load debug
if [[ $? -ne 0 ]]; then
echo "unable to load a 'debug' module using a noop for debug function"
# noop
function debug () {
:
}
fi

reload_shell () {
  source $BASH_SHELL_BASE/load.sh $@
}

reload_user() {
  save=$PWD
  exec su -l $USER
  cd $save
}

# remove, loading in shell.env
# load_custom_shell_env () {
#   [[ -f $BASH_SHELL_HOSTNAME/shell.env ]] && source $BASH_SHELL_HOSTNAME/shell.env
#   [[ -f $BASH_SHELL_USER_DIR/shell.env ]] && source $BASH_SHELL_USER_DIR/shell.env 
#   [[ -f $BASH_SHELL_DEV/shell.env ]] && shell_process_directory $BASH_SHELL_DEV/shell.env 
# }

load_shell_host () {
  module_load shell-process-directory
  [[ -d $BASH_SHELL_HOSTNAME ]] && shell_process_directory $BASH_SHELL_HOSTNAME
}

load_shell_user () {
  module_load shell-process-directory
  BASH_SHELL_USER_DIR=${BASH_SHELL_USER_DIR:-$HOME/shell}
  [[ -d $BASH_SHELL_USER_DIR ]] && shell_process_directory $BASH_SHELL_USER_DIR 
}

load_shell_dev () {
  module_load shell-process-directory
  [[ -d $BASH_SHELL_DEV ]] && shell_process_directory $BASH_SHELL_DEV 
}

load_shell_host_user_dev () {
  load_shell_host
  load_shell_user
  load_shell_dev
}

######################  START LOAD SCRIPT #############################

([[ $BASH_ENV ]] && [[ ! $- == *i* ]] && [[ $BASH_USE_ALIAS ]]) && shopt -s expand_aliases  

# process the base directory by default
unset BASH_SHELL_BASE_LOADED
unset BASH_SHELL_LOADED

module_load shell-process-directory

if ! command -v which > /dev/null; then 
echo missing "which" command install package
which () { command -v "$1"; }
fi 


# if [[ $SSH_SESSION ]]; then 
#   # ssh session only process base by default
#   # env | grep SSH
#   # echo ssh session bash load
#   # TODO process network dirs if environment variable is set
#   # process only base, nothing (i.e. BASH_SAFE_MODE, SSH_SHELL_CLEAN) if env variables are set
#   # see https://stackoverflow.com/a/59461195
#   shell_process_directory $BASH_SHELL_BASE
#   shell_process_directory $BASH_SHELL_ANY_HOST
#   shell_process_directory $BASH_SHELL_HOST
#   export BASH_SHELL_BASE_LOADED=true

  # process base and any host and any network directories  
  # as set in shell.env
for dir in ${1:-$BASH_SHELL_DIRS}; do
  # env | grep BASH
  # echo loading dir $dir
  shell_process_directory $dir
  [[ "$dir" = "$BASH_SHELL_BASE" ]] && export BASH_SHELL_BASE_LOADED=true
done

# env | grep NETWORK

# # set one or the other of these in shell.env if you want to load networks with every shell
if [[ $BASH_SHELL_HOME_NETWORK_LOAD ]] || [[ $BASH_SHELL_ALL_NETWORKS_LOAD ]] || [[ $BASH_SHELL_NETWORKS_LOAD ]] ; then
  module_load networks
  if [[ $BASH_SHELL_ALL_NETWORKS_LOAD ]]; then
     networks_load -a
    else 
    [[ $BASH_SHELL_HOME_NETWORK_LOAD ]] && networks_load -h
  fi
  [[ $BASH_SHELL_NETWORKS_LOAD ]] && networks_load $BASH_SHELL_NETWORKS_LOAD
fi

load_shell_host
[[ $BASH_LOAD_SHELL_DEV ]] && load_shell_dev
 
export BASH_SHELL_LOADED=true

# Note: $HOME/shell or $HOME/BASH_SHELL_USER are processed via $HOME/.bashrc