#!/usr/bin/env bash

# shellcheck shell=bash

################################################################################
#################### External variables/functions and libs #####################
################################################################################

# Set of external files with variables, functions and other. The configuration
# of each of them is in lib directory. The fd_stack array stores the names of
# attached files. If you create a new file with some interesting function,
# name it appropriately and insert to the array.
# Example:
#   - lib/kill_process - file with your function
#   - _fd_stack=("kill_process") - says to include this file in the script
readonly _fd_stack=(init_config init_session load_modules init_cli \
                    cli_distributor cli_help cli_show init_module \
                    module_help module_show module_info show_config \
                    set_config init_search)

if [[ "${#_fd_stack[@]}" -ne 0 ]] ; then

  for _fd in "${_fd_stack[@]}" ; do

    # shellcheck disable=SC2154
    _fd_full_path="${_lib}/${_fd}"

    if [[ ! -z "$_fd_full_path" ]] && [[ -e "$_fd_full_path" ]] ; then

      # If the file exists is loaded.
      # shellcheck disable=SC1090
      source "$_fd_full_path"

    elif [[ -z "$_fd_full_path" ]] ; then

      printf "incorrectly loaded '%s' file (incorrect filename)" "$_fd_full_path"
      _exit_ "1"

    else

      printf "incorrectly loaded '%s' file (does not exist?)" "$_fd_full_path"
      _exit_ "1"

    fi

  done

fi
