#!/usr/bin/env echo These script helpers should be imported into scripts with: source
#
# Refs:
#   https://www.linuxjournal.com/content/return-values-bash-functions
#   https://askubuntu.com/questions/926450/how-do-i-assign-a-variable-in-bash-whose-name-is-expanded-from-another-varia
#
# Define a variable with the contents of a HEREDOC
#
# Example:
#   define helptext <<HELPTEXT
#     Helper script for installing ruby versions into rbenv.
#
#     Foo Bar Baz with some ${interpreted_foo}
#   HELPTEXT
#
#   echo "${helptext}"
#
# Refs:
#   https://stackoverflow.com/questions/1167746/how-to-assign-a-heredoc-value-to-a-variable-in-bash/8088167#8088167
#   https://unix.stackexchange.com/questions/76481/cant-indent-heredoc-to-match-nestings-indent

define() { IFS='\n' read -r -d '' ${1} || true; }

matches_regex() {
  regex=$1
  shift
  to_check=$*
  awk_cmd='BEGIN { RC=1; if (to_check ~ regex) { RC=0 }; exit RC }'

  awk -v "regex=$regex" -v "to_check=$*" ${awk_cmd}
}

matches_arg() {
  arg_regex=$1
  shift
  before_regex='(^|[ ])'
  after_regex='([ ]|$)'

  matches_regex "${before_regex}${arg_regex}${after_regex}" "$*"
}

# Example
#   if matches_arg_help "$*"; then
#     echo "Usage: `basename "$0"` string"
#     exit 0
#   fi
matches_arg_help() {
  matches_arg '(-h|--help)' "$*"
}

# TODO: make this also able to handle arrays
#  local -a suggested_fix=(a bc d)
#  for line in "${suggested_fix[@]}"; do echo "${line}"; done
# Refs:
#  https://linuxconfig.org/how-to-use-arrays-in-bash-script
#  https://opensource.com/article/18/5/you-dont-know-bash-intro-bash-arrays
prefix_lines() {
  IFS=$'\n'
  prefix
  lines
  if [ "$#" -gt 1 ]; then
    prefix="${1}"
    shift
    lines="${@}"
  else
    prefix=""
    lines="${@}"
  fi
  # while read -r line; do
  #   echo "${prefix}${line}"
  # done < <(printf '%s\n' "${lines}")
}

# Check if program is installed
# check_installed() {
#   command="$1"
#   only_return="$2"
#
#   # Check if command is installed
#   command_path=$(command -v "${command}")
#   command_realpath=$(realpath "${command_path}" 2> /dev/null)
#
#   # Check if package is installed in Homebrew
#   # Note: The '|| true' after the grep command ensures that the command's failure doesn't exit the script, but instead stores an empty string in the variable.
#   is_brew_installed=$(brew list | grep -w "${command}" || true)
#
#   if [[ ( -z "${command_path}" || ! -x "${command_realpath}" ) && -z "${is_brew_installed}" ]]; then
#     echo "[ERROR] Couldn't find '${command}'. Try:"
#
#     if [[ "${command}" == "brew" ]]; then
#       echo "  installing homebrew from https://brew.sh/"
#     else
#       brew_info=$(brew info ${command} 2>&1 || true)
#
#       echo "  brew install ${command}"
#       echo ""
#       echo "[brew-info]"
#       prefix_lines "  " "${brew_info}"
#     fi
#
#     if [[ "${only_return}" == "return" ]]; then
#       return 1
#     else
#       exit 1
#     fi
#   fi
# }

is_installed() {
  check_installed "$1" "return"
}

require_installed_brew() {
  command="$1"
  logfile="/tmp/brew-install-${command}.log"

  if ! is_installed "${command}" >/dev/null 2>&1; then
    echo "[require_installed_brew::${command}]"
    echo "  Required command '${command}' missing. Installing..."
    brew install "${command}" | tee "${logfile}"
    echo "  Install log: ${logfile}"
  else
    echo "[require_installed_brew::${command}] already installed. Skipping..."
  fi
}

require_installed_brew_cask() {
  command="$1"
  logfile="/tmp/brew-cask-install-${command}.log"

  if ! brew list --cask "${command}" &>/dev/null; then
    echo "[require_installed_brew_cask::${command}]"
    echo "  Required cask '${command}' missing. Installing..."
    brew install --cask "${command}" | tee "${logfile}"
    echo "  Install log: ${logfile}"
  fi
}

# Function to create symlink if it doesn't exist
create_symlink() {
  target=$1
  link_name=$2

  if [ ! -e "$link_name" ]; then
    if [ -e "$target" ]; then
      ln -s "$target" "$link_name"
      echo "[create_symlink] Created symlink: $link_name -> $target"
    else
      echo "[create_symlink] Target not found: $target"
    fi
  else
    echo "[create_symlink] Symlink already exists: $link_name"
  fi
}
