#!/usr/bin/env bash

usage() {
	cat <<-EOF
	usage :
	  $0 (-a|-b) OPTION

	  one of these MUST be specified :
	   -a : do something with [a]
	   -b : do something with [b]

	  with OPTION :
	   -h : display this [h]elp and exit
	   -v : [v]erbose mode
	EOF
	}


checkCliParameters() {
	# The purpose is to ensure all test scenarios
	#	- are handled
	#	- get a meaningfull error message
	# while avoiding adding unnecessary tests/if blocks. This mostly means
	#	- using generic (multi-purpose/1SFA) error messages
	#	- rather than adding code.
	local mutuallyExclusiveOptionA=0
	local mutuallyExclusiveOptionB=0

	while getopts ':abhv' arg; do
		case "$arg" in
			a)
				echo "✅ Option 'a' was triggered"
				mutuallyExclusiveOptionA=1
				;;
			b)
				echo "✅ Option 'b' was triggered"
				mutuallyExclusiveOptionB=1
				;;
			h)
				usage
				exit 0
				;;
			v)
				echo "💬️ verbose mode enabled"
				;;
			*)	# it this equivalent to '\?)' ?
				echo "⁉️  Unknown option '-$OPTARG'"
				usage
				exit 1
				;;
		esac
	done

#	echo "optind: '$OPTIND', '$arg'"

	# if sum ==
	#	0 : none specified :		KO
	#	1 : exactly 1 specified :	OK
	#	2 : 2 specified :			KO
	[ $((mutuallyExclusiveOptionA+mutuallyExclusiveOptionB)) -ne 1 ] && {
		echo "1️⃣️  please specify EXACTLY ONE of '-a', '-b'"
		usage
		exit 1
		}

	# detect undashed options
	[ ${1:0:1} != '-' ] && {
		echo "⛔ invalid option '$1'"
		usage
		exit 1
		}

	}


main() {
	checkCliParameters "$@"
	echo "now running the script..."
	}


main "$@"
