# vim: ft=sh:noml:
whence -w "$@" | while read -r line; do
  # shellcheck disable=SC2249
  case ${line##*: } in
    alias) alias "${line%:*}" ;;
    builtin | function | keyword)
      whence -afv "${line%:*}"
      ;;
    command)
      man "${line%:*}" || info "${line%:*}"
      if [ $# -ne 0 ]; then
        FILE=$(command -v "${line%:*}")
        case $(file --mime --dereference --brief "${FILE}") in
          text/* | application/x-sh)
            # shellcheck disable=SC2312
            if [ 50 -gt "$(wc -l <"${FILE}")" ]; then
              cat "${FILE}"
            else
              echo "${FILE} is quite a long text file. I will not cat it."
            fi
            ;;
          application/*)
            echo "${FILE} is a compiled executable or unsupported format."
            echo "It does not have a man page installed. Try '${line%:*} --help' or '-h'."
            ;;
        esac
      fi
      ;;
    none)
      echo "Not found: ${line%:*}"
      ;;
  esac
done
