#!/usr/bin/env bash

port=443
tlsVersion=1
host=""

die(){
	echo "$@" >&2
	exit 1
}

print_help(){
	echo "Attempts to connect using different tls versions"
	echo ""
	echo "tlsversionconnect [options] <host>"
	echo ""
	echo "-p | --port    port number (default 443)"
	echo "-v | --version tls version (default 1)"
}

while [ "$#" -gt 0 ]; do
	case "$1" in
		-p|--port)
			port="$2"
			shift; shift
			;;
		-v|--version)
			case "$2" in
				1|1.0|1-0|1_0)
					tlsVersion="1"
					;;
				1.1|1-1|1_1)
					tlsVersion="1_1"
					;;
				1.2|1-2|1_2)
					tlsVersion="1_2"
					;;
				1.3|1-3|1_3)
					tlsVersion="1_3"
					;;
			esac
			shift; shift
			;;
		-h|--help)
			print_help
			exit 0
			;;
		*)
			host="$1"
			shift
			;;
	esac
done

if [ -z "$host" ]; then
	die "No host provided"
fi

echo \$ openssl s_client "-tls${tlsVersion}" -connect "${host}:${port}"
echo "" | openssl s_client "-tls${tlsVersion}" -connect "${host}:${port}"
