#!/bin/sh
set -o errexit

debug_mode='0'


msg() {
  printf '%b\n' "$1" >&2
}

success() {
  if [ "${ret}" -eq '0' ]; then
    msg "\33[32m[✔]\33[0m ${1}${2}"
  fi
}

error() {
  msg "\33[31m[✘]\33[0m ${1}${2}"
  exit 1
}

# print_error() {
#   echo "Error occurred:"
#   awk 'NR>L-4 && NR<L+4 { printf "%-5d%3s%s\n",NR,(NR==L?">>>":""),$0 }' L="$1" "$0"
# }
# trap 'print_error "$LINENO"' INT

debug() {
  if [ "${debug_mode}" -eq '1' ] && [ "${ret}" -gt '1' ]; then
    # msg "An error occurred in function \"${FUNCNAME[$i + 1]}\" on line ${BASH_LINENO[$i + 1]}, we're sorry for that."
    awk 'NR>L-4 && NR<L+4 { printf "%-5d%3s%s\n",NR,(NR==L?">>>":""),$0 }' L="${LINENO}" "$0"
  fi
}

program_exists() {
  ret='0'
  command -v "$1" >/dev/null 2>&1 || { ret='1'; }

  # fail on non-zero return value
  if [ "${ret}" -ne 0 ]; then
    return 1
  fi

  return 0
}

program_must_exist() {
  program_exists "$1"

  # throw error on non-zero return value
  if [ "$?" -ne 0 ]; then
    error "You must have '$1' installed to continue."
  fi
}

variable_set() {
  if [ -z "$1" ]; then
    error "You must have your HOME environmental variable set to continue."
  fi
}

# lnif() {
#   if [ -e "$1" ]; then
#     ln -sf "$1" "$2"
#   fi
#   ret="$?"
#   debug
# }
