#!/usr/bin/env bash

# shellcheck shell=bash

readonly __version="1.2.0"

readonly __cli_prompt="cli"
# shellcheck disable=SC2034
__cli_banner="main"
# shellcheck disable=SC2034
_cli_state=0
_in_module=0

_motd_status=0

_cmd_state=0

# shellcheck disable=SC2034
_ld_module=""
# shellcheck disable=SC2034
_mstate=0
# shellcheck disable=SC2034
_module_cfg=""

# shellcheck disable=SC2034
_pushd_stack=()

export _session_variables=()
export _variables_stack=()

export _profiles_list=()

# shellcheck disable=SC2034
# Store xterm params (for external type of terminal).
readonly _xterm="xterm -hold -fa monaco -fs 10 -bg black -e"

readonly _und=$(tput smul)
readonly _nound=$(tput rmul)
readonly _bold=$(tput bold)
readonly _normal=$(tput sgr0)


# ``````````````````````````````````````````````````````````````````````````````
# Function name: motd_cli()
#
# Description:
#   Message of the day.
#
# Usage:
#   motd_cli
#
# Examples:
#   motd_cli
#

function motd_cli() {

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

  tput cup 4 0
  printf "${_bold}\\e[1;30m%s\\e[m${_normal}" "
             _..__.          .__.._
           .^\"-.._ '-(\\__/)-' _..-\"^.
                  '-.' "
  printf "${_bold}\\e[1;36m%s\\e[m${_normal}" \
         "oo"
  printf "${_bold}\\e[1;30m%s\\e[m${_normal}" \
         " '.-'"

  printf "${_bold}\\e[1;30m%s\\e[m${_normal}" "
                     \`-..-'"

  tput cup 8 0
  printf "\\e[0;32m%s\\e[m" "
                 _
                / \\        _
              , | | ,     / \\
             ((_| |_))  , | | ,
             \`--, ,--\` ((_| |_))
                | |    \`--, ,--\`"
  printf "\\e[1;33m%s\\e[m" "
            ^^^^"
  printf "\\e[0;32m%s\\e[m" "| |"
  printf "\\e[1;33m%s\\e[m" "^^^^^^^"
  printf "\\e[0;32m%s\\e[m" "| |"
  printf "\\e[1;33m%s\\e[m" "^^^^^^^
               \`\"\"\"\`"
  printf "\\e[0;32m%s\\e[m" "      | |"
  printf "\\e[1;33m%s\\e[m" "
                         \`\"\"\"\`"

  _t=24

  tput cup 1 $((23 + _t)) ; printf "%s" " _______                 __"
  tput cup 2 $((23 + _t)) ; printf "%s" "|     __|.---.-.-----.--|  |.--------.---.-.-----."
  tput cup 3 $((23 + _t)) ; printf "%s" "|__     ||  _  |     |  _  ||        |  _  |  _  |"
  tput cup 4 $((23 + _t)) ; printf "%s" "|_______||___._|__|__|_____||__|__|__|___._|   __|"
  tput cup 5 $((66 + _t)) ; printf "%s" "|__|"

  tput cup 6 $((40 + _t)) ; printf "%s: \\e[3;36m%s\\e[m" "Author" "trimstray"
  tput cup 6 $((59 + _t)) ; printf "%s: \\e[3;36m%s\\e[m" "Version" "${__version}"

  tput cup 8 $((26 + _t)) ; printf "%s \\e[1;38m%s\\e[m" "Project:" "https://github.com/trimstray/sandmap"

  tput cup 10 $((26 + _t)) ; printf "%s" "This program comes with ABSOLUTELY NO WARRANTY."
  tput cup 11 $((26 + _t)) ; printf "%s" "This is free software, and you are welcome to redistribute it"
  tput cup 12 $((26 + _t)) ; printf "%s" "under certain conditions; for more details please see"
  tput cup 13 $((26 + _t)) ; printf "%s" "<http://www.gnu.org/licenses/>."

  tput cup 15 $((26 + _t)) ; printf "%s" "Hint:"
  tput cup 17 $((28 + _t)) ; printf "%s '${_und}\\e[1;38m%s\\e[m${_normal}' %s" \
                                    "Try" "help" "to show all cli commands and start!"
  tput cup 20 0

  return $_STATE

}

# ``````````````````````````````````````````````````````````````````````````````
# Function name: init_cli()
#
# Description:
#   Init sandmap cli.
#
# Usage:
#   init_cli
#
# Examples:
#   init_cli
#

function init_cli() {

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

  if [[ "$_motd_status" -eq 0 ]] ; then

    motd_cli
    _motd_status=1

  fi

  # shellcheck disable=SC1090,SC2154
  source "${_cfg}/sandmap-completion"

  # shellcheck disable=SC2034
  local input_array=()

  while [[ "${input_array[0]}" != "exit" ]] ; do

    cstate="1"

    # shellcheck disable=SC2034
    _cmd_state="0"

    # shellcheck disable=SC2354
    # > or »
    _ansi_prompt=$(printf "\\e[1;38m%s\\e[m(\\e[0;38m%s\\e[m)\\e[4;32m>\\e[m " "${__cli_prompt}" "${__cli_banner}")
    read -r -p "$_ansi_prompt" -e -a input_array

    case ${input_array[0]} in

      clear)

        clear

        if [[ "$_cli_state" -eq 1 ]] ; then

          init_cli
          _cli_state=1

        else

          init_cli

        fi
        cstate="0" ;;

      main)

        # shellcheck disable=SC2034
        __cli_banner="main"
        # shellcheck disable=SC2034
        _in_module=0

        init_cli
        cstate="0" ;;

      banner)

        clear

        _motd_status=0
        motd_cli

        init_cli
        _motd_status=1

        cstate="0" ;;

      nmap)

        _init_cmd "${input_array[@]:1}"
        cstate="0" ;;

      exit)

        _exit_ 0 ;;

      "")

        echo -en ""
        cstate="0" ;;

      *)

        cli_distributor "${input_array[@]}"
        # shellcheck disable=SC2034
        cstate="0"

      ;;

    esac

  done

  return $_STATE

}
