#!/usr/bin/env bash

# shellcheck shell=bash

# ``````````````````````````````````````````````````````````````````````````````
# Function name: load_modules()
#
# Description:
#   Load modules.
#
# Usage:
#   load_modules
#
# Examples:
#   load_modules
#

function load_modules() {

  # shellcheck disable=SC2034
  local _FUNCTION_ID="load_modules"
  local _STATE=0

  _profiles_list=()

  _sprintf "head" "Please wait, loading modules..."

  # shellcheck disable=SC2154
  for i in $(find "${_modules}" -name '*.mod' -exec ls {} + | sort -k 10) ; do

    _fpath=$(basename "$i")
    # shellcheck disable=SC2034
    _fname=$(echo "$_fpath" | cut -d "." -f1)

    modules_list+=("$_fname")
    modules_full_list+=("$_fname":"$_fpath")

    # shellcheck disable=SC1090
    source "${_modules}/${_fpath}"

    ${_fname}

    for j in "${_module_commands[@]}" ; do

      # shellcheck disable=SC2034
      _key_alias=$(echo "$j" | awk -v FS="(;|;)" '{print $3}')
      # shellcheck disable=SC2034
      _key_cmd=$(echo "$j" | awk -v FS="(;|;)" '{print $4}')

      _profiles_list+=("${_key_alias};${_key_cmd};${_fname}")

    done

  done

  return $_STATE

}
