#!/usr/bin/env bash

# shellcheck shell=bash

# ``````````````````````````````````````````````````````````````````````````````
# Function name: init_module()
#
# Description:
#   Init modules.
#
# Usage:
#   init_module
#
# Examples:
#   init_module
#

export module_name=""
export module_args=""
export module_path=""

function init_module() {

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

  # shellcheck disable=SC2034
  local _argv=("$@")

  # shellcheck disable=SC2154
  if [[ "$_in_module" -eq 0 ]] ; then

    # shellcheck disable=SC2034
    module_name="${_argv[1]}"
    # shellcheck disable=SC2034
    module_args=("${_argv[@]:2}")
    # shellcheck disable=SC2154
    module_path="${_modules}/${module_name}.mod"

    if [[ -e "${module_path}" ]] ; then

      # shellcheck disable=SC1090
      source "${module_path}"

      ${module_name}

    fi

  elif [[ "$_in_module" -eq 1 ]] ; then

    # shellcheck disable=SC2034
    module_name="$_ld_module"
    # shellcheck disable=SC2034
    module_args=("${_argv[@]:1}")
    # shellcheck disable=SC2154
    module_path="${_modules}/${module_name}.mod"

    if [[ -e "${module_path}" ]] ; then

      # shellcheck disable=SC1090
      source "${module_path}"

      ${module_name}

    fi

  else

    printf "stdout: %s\\n" "unknown command"

  fi

  return $_STATE

}
