confirm() {
  # make sure stdin is a terminal
  [ -t 0 ] || exec </dev/tty

  read -r "$1 [Y/n] " reply
  # if reply is empty
  [ -z "${reply}" ] && return 0
  # if reply is Yes
  case ${reply} in
    [yY] | [yY][eE][sS]) return 0 ;;
    *) return 1 ;;
  esac
}

ensure_confirmation() {
  # using echo to output the message and -n flag to allow following input
  # in the same line
  IFS=" "
  printf '%s' "Are you sure you want to proceed? [Y/n] "
  read -r confirmation
  # transform to lowercase to allow 'Y' or 'y' as a confirmation
  confirmation=$(echo "${confirmation}" | tr '[:upper:]' '[:lower:]')
  confirmation=${confirmation:-"y"}
  if [ "${confirmation}" != "y" ]; then
    exit_message "Operation cancelled. Exiting..."
    exit 1
  fi
}
