#!/usr/bin/env bash

searchLineInOutput() {
	needle="$1"
	shift
	haystack="$@"
	found=0
	while read line; do
		if [[ "$line" =~ ^$needle ]]; then
			found=1
		fi
	done <<< "$haystack"
	echo $found
	}


main() {
	nbScenariosPlayed=0
	while IFS='|' read option expectedOutput padding; do

		# ignore blank lines in the scenarios list
		[ -z "$option" -a -z "$expectedOutput" ] && continue

		# ignore comments in the scenarios list
		[[ "$option" =~ ^# ]] && continue
		cat <<-EOF

			option: '$option', expectedOutput: '$expectedOutput'"
			EOF

		receivedOutput=$(./getopts_demo.sh $option)
		result=$(searchLineInOutput "$expectedOutput" "$receivedOutput")
	#echo "RESULT='$result'"
	#	receivedOutput=$(./scriptUsingGetopts.sh $option | head -n 1)

	#	[ "$receivedOutput" = "$expectedOutput" ] \
		[ $result -eq 1 ] \
			&& echo 👍️ \
			|| {
				cat <<-EOF
					EXPECTED : |$expectedOutput|
					RECEIVED : |$receivedOutput|
					EOF
				echo 🪲️
				}
		((nbScenariosPlayed++))
	done < <(cat <<-EOF
		######## 'basic' scenarios ########
		# specifying 1 valid option
		-a|✅ Option 'a' was triggered|x
		-b|✅ Option 'b' was triggered|x
		-h|usage :|x

		# less 'basic' scenarios : specifying 2 valid options
		-a -v|✅ Option 'a' was triggered|x
		-b -v|✅ Option 'b' was triggered|x

		# concatenated options
		-av|✅ Option 'a' was triggered|x
		-ah|✅ Option 'a' was triggered|x
		-va|✅ Option 'a' was triggered|x
		-ha|usage :|x						# 'h' takes precedence over 'a'
		-bv|✅ Option 'b' was triggered|x
		-bh|✅ Option 'b' was triggered|x
		-vb|✅ Option 'b' was triggered|x
		-hb|usage :|x						# 'h' takes precedence over 'b'
		-vh|💬️ verbose mode enabled|x		# 'v' takes precedence over 'h'
		-hv|usage :|x						# 'h' takes precedence over 'v'

		# specifying the same option twice
		-a -a|✅ Option 'a' was triggered|x
		-aa|✅ Option 'a' was triggered|x
		-b -b|✅ Option 'b' was triggered|x
		-bb|✅ Option 'b' was triggered|x

		######## 'error' scenarios ########
		# specifying 2 mutually exclusive options
		-a -b|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-ab|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-b -a|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-ba|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x

		# 2 mutually exclusive options + 1 valid option specified
		-a -b -v|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-abv|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-avb|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-bav|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-bva|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-vab|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-vba|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x

		# 2 mutually exclusive options + 1 invalid option specified
		-a -b -z|⁉️  Unknown option '-z'|x
		-abz|⁉️  Unknown option '-z'|x
		-azb|⁉️  Unknown option '-z'|x
		-baz|⁉️  Unknown option '-z'|x
		-bza|⁉️  Unknown option '-z'|x
		-zab|⁉️  Unknown option '-z'|x
		-zba|⁉️  Unknown option '-z'|x

		# none of the mandatory options specified
		|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		-v|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		a|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x
		z|1️⃣️  please specify EXACTLY ONE of '-a', '-b'|x

		# invalid option specified
		-z|⁉️  Unknown option '-z'|x

		# empty string specified...  Don't know how to handle this 🤕️
#		''|⛔ invalid option|x

		# 1 valid + 1 invalid options specified
		-a -z|⁉️  Unknown option '-z'|x
		-b -z|⁉️  Unknown option '-z'|x

		EOF
		)
	echo -e "\n$nbScenariosPlayed scenarios played."
	}


main
