#!/usr/bin/env bash

#
# ## command\_exists()
#
# Checks to see whether a command exists within the current environment and PATH
#
# ### Input Parameters
#
# First parameter is a command name.
#
# ### Stream Outputs
#
# None.
#
# ### Environmental effects
#
# none.
#
# ### return codes
#
# 0 if the command was found in the current environment
# 1 if the command was not found in the current environment
#
# ### failure scenarios
#
# Fails if no command name was given.
#
# ### usage examples
#
#     user$ command_exists adsf
#     user$ echo $?
#     1
#
#     user$ command_exists ls
#     user$ echo $?
#     0
#
command_exists()
{
  local _name="${1:-}"

  if variable_is_nonempty _name
  then
    if command -v "${_name}" > /dev/null 2>&1
    then
      return 0
    else
      return 1
    fi
  else
    fail "Cannot test if command exists as no command name was given."
  fi
}

#
# ## is\_a\_function()
#
# Checks to see whether a named function exists within the current environment
#
# ### Input Parameters
#
# First parameter is a function name.
#
# ### Stream Outputs
#
# None.
#
# ### Environmental effects
#
# none.
#
# ### return codes
#
# 0 if the function exists
# 1 if the function does not exist
#
# ### failure scenarios
#
# Fails if no function name was given.
#
# ### usage examples
#
#     user$ is_a_function rvm
#     user$ echo $?
#     0
#
#     user$ is_a_function asdf
#     user$ echo $?
#     1
#
#     user$ is_a_function ls
#     user$ echo $?
#     1
#
is_a_function()
{
  local _name="${1:-}"

  if variable_is_nonempty _name
  then
    if declare -f "${_name}" >/dev/null 2>&1
    then
      return 0
    else
      return 1
    fi
  else
    fail "Cannot test if function exists as no function name was given."
  fi
}

