txtreset="$(tput -T "${TERM}" sgr0)"
readonly txtreset

txtbold="$(tput -T "${TERM}" bold)"
readonly txtbold

txtred="$(tput -T "${TERM}" setaf 1)"
readonly txtred

txtgreen="$(tput -T "${TERM}" setaf 2)"
readonly txtgreen

txtyellow="$(tput -T "${TERM}" setaf 3)"
readonly txtyellow

# unicode "✗"
readonly fancyx='\342\234\227'
# unicode "✓"
readonly checkmark='\342\234\223'
# PASS="\e[1m\e[38;5;2m✔\e[m"
# FAIL="\e[1m\e[38;5;1m✘\e[m"
readonly PASS="${txtbold}${txtgreen}${checkmark}${txtreset}"
readonly FAIL="${txtbold}${txtred}${fancyx}${txtreset}"

# default values (for case when decoration applied)
fail=${FAIL}
pass=${PASS}
wrapper_beg="${txtbold}${txtyellow}"
wrapper_end="${txtreset}"

REGEX_SIMPLE_VERSION="([[:digit:]]+\.?){2,3}"
version=$(ssh -V 2>&1 \
  | grep -Eo "OpenSSH_${REGEX_SIMPLE_VERSION}[[:alnum:]]*" | cut -d'_' -f2)
status=$?

# when unsafe processing is not allowed, the -1 signifies
if [ "${status}" -eq "-1" ]; then
  printf '%b %s not understood\n' "${fail}" "${command:-$@}" >"${OUTPUT}"
  KO=$((KO + 1))

# command not installed
elif [ "${status}" -eq 127 ]; then
  printf '%b %s\n' "${fail}" "${command}" >"${OUTPUT}"
  KO=$((KO + 1))

# successfully executed
elif [ "${status}" -eq 0 ] || [ "${status}" -eq 141 ]; then
  printf "%b %s %b\n" "${pass}" "${command}" \
    "${wrapper_beg}${version}${wrapper_end}" >"${OUTPUT}"
  OK=$((OK + 1))

# as long as its not 127, command is there,
# but we might not have been able to extract version
else
  printf '%b %s\n' "${pass}" "${command}" >"${OUTPUT}"
  OK=$((OK + 1))
fi

OPTIND=1
OUTPUT=/dev/stdout
