#!/bin/sh -e
#
# certcheck - check domains certificate

if [ -z "$1" ] || [ "$1" = "-h" ] || [ "$1" = "--help" ]; then
  cat <<'EOF'
Usage:
  certcheck host.domain[:PORT] (defaults to :443)

Example:
  certcheck host1.domain
  certcheck host2.domain:1234
EOF
  exit 1
fi

host="$1"

if [ "${host}" = "${host%:*}" ]; then
  host="${host}:443"
fi

echo "checking ${host}..."
result="$(</dev/null openssl s_client -connect "${host}")"
printf '%s\n' "${result}"
printf '%s\n' "${result}" | grep -i -o 'notafter.*' --color=auto
