#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  getip [-4|-6]
		  getip -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -4            get IPv4
		  -6            get IPv6 (default)
		  -h, --help    show this message


		The the internet facing IP address of the current machine.


		Examples:

		  Expose IPv6::

		    $ echo "http://$(getip -6)/sub/" >> index.html
	EOF
}


for flag in "$@"; do
	case "$flag" in
		(--)
			break
			;;
		(--help)
			usage
			help
			exit
			;;
		(*)
			;;
	esac
done

V=6
while getopts '46h' flag; do
	case "$flag" in
		(4)
			V=4
			;;
		(6)
			V=6
			;;
		(h)
			usage
			help
			exit
			;;
		(*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))


curl -s https://euandre.org/b/ipv"$V"
