#!/bin/sh
#
# inno IP Info - Requests and shows the hosts public IPv4 and IPv6 address.
#
# Copyright (c) 2020 Innotronic Ingenieurbüro GmbH
# Inspired and kindly supported by @stoege
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

appVersion="1.8"

urlIPv4="https://ipv4.innotronic.net/api.php?txt"
urlIPv6="https://ipv6.innotronic.net/api.php?txt"

optIPv4=1
optIPv6=1
optTimeout=2
optBatch=
optHelp=

# Parse options
while [ $# -gt 0 ]; do

	case ${1} in
		-4) optIPv6= ;;
		-6) optIPv4= ;;
		-b|--batch)	optBatch=1 ;;
		-t|--timeout)
			if [[ -n ${2} ]]; then
				optTimeout=${2}
				shift
			else
				optHelp=1
			fi
			;;
		-V|--version)	optVersion=1 ;;
		-h|--help|*)	optHelp=1  ;;
	esac

	shift
done


# Show help
if [ -n "${optHelp}" ]; then
	cat <<EOS
  _                     ___ ____    ___        __
 (_)_ __  _ __   ___   |_ _|  _ \\  |_ _|_ __  / _| ___
 | | '_ \\| '_ \\ / _ \\   | || |_) |  | || '_ \\| |_ / _ \\
 | | | | | | | | (_) |  | ||  __/   | || | | |  _| (_) |
 |_|_| |_|_| |_|\\___/  |___|_|     |___|_| |_|_|  \\___/

Usage
  i3 ( [-4|-6] [--batch] [--timeout nn] | --version | --help )

Description
  Requests and shows the hosts public IPv4 and IPv6 address.

Options
            -4     IPv4 only
            -6     IPv6 only
   --batch, -b     Batch mode, prints only the plain address/es
 --timeout, -t nn  Connection timeout in seconds
    --help, -h     Show this help
 --version, -V     Show the version number

WWW
  https://ip.inno.ch/

Version
  v${appVersion}

Copyright
  (c) 2020 Innotronic Ingenieurbüro GmbH
  Inspired and kindly supported by @stoege
EOS
	exit
fi


# Show version
if [ -n "${optVersion}" ]; then
	printf "i3 v${appVersion}\n"
	exit
fi

 
# Check if CURL is available
if ! hash curl 2>/dev/null; then
	printf "Uups. CURL not found. Exiting...\n\n" >&2
	exit 1
fi


curlRetCodeTxt()
{
	case ${retCode} in
		 0)  return 0  ;;
		 5)  retCodeTxt="!PROXY"    ;;
		 6)  return 0  ;;
		 7)  retCodeTxt="!NETWORK"  ;;
		22)  retCodeTxt="!API"      ;;
		28)  retCodeTxt="!TIMEOUT"  ;;
		51|59)      retCodeTxt="!SSL"   ;;
		60|90|91)   retCodeTxt="!CERT"  ;;
		*)   retCodeTxt="!FAILED"   ;;
	esac

	return 1
}


# Handle IPv4
if [ -n "${optIPv4}" ]; then

	ipv4=$( curl -4 -s -f -m ${optTimeout} "${urlIPv4}" )
	retCode=$?
	
	# Check return code
	if ! curlRetCodeTxt; then
		ipv4=${retCodeTxt}
	fi

	if [ -z "${optBatch}" ]; then
		printf "IPv4: "
	fi

	printf -- "${ipv4}\n"

fi


# Handle IPv6
if [ -n "${optIPv6}" ]; then

	ipv6=$( curl -6 -s -f -m ${optTimeout} "${urlIPv6}" )
	retCode=$?

	# Check return code
	if ! curlRetCodeTxt; then
		ipv6=${retCodeTxt}
	fi

	if [ -z "${optBatch}" ]; then
		printf "IPv6: "
	fi

	printf -- "${ipv6}\n"

fi
