#!/bin/bash -pe
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright 2024, Gerrit-John Los <los@lugons.org>

# - setup -
shopt -s extglob

# - options -
log() { echo "$*" 1>&2 ; }
msg() { log "${0##*/}: $*" ; }
rc=0
warn() { msg "$*" ; rc=1 ; }
fail() { msg "$*" ; exit 2 ; }

usage() {
	[ $# -eq 0 ] || msg "$*"
	cat 1>&2 <<-EOT
	usage: ${0##*/} [-h] [-v]... [-X VALUE]...
	  -h|--help      : show this help
	  -v|--verbose   : increase verbosity
	     --no-verbose: be quiet
	  -X|--example   : parameter with value
	EOT
	exit 3
}

verbose=0
example=()
declare -A optmap
optmap[h]='help'
optmap[v]='verbose'
optmap[X:]='example'
while getopts "${optmap[*]}-:" opt "$@" ;do
	if [ -v optmap["$opt"] ] ;then
		option="${optmap["$opt"]}"
	elif [ -v optmap["$opt":] ] ;then
		option="${optmap["$opt":]}=${OPTARG}"
	elif [ "$opt" = '-' ] ;then
		option="$OPTARG"
	else
		usage
	fi
	value="${option#*=}"
	case "$option" in
		help)       usage ;;
		verbose)    let ++verbose ;;
		no-verbose) verbose=0 ;;
		example=*)  example+=("$value") ;;
		*) usage "illegal option -- $option" ;;
	esac
done
shift $((OPTIND-1))
[ $# -eq 0 ] || usage


# - functions -


# -- main --


exit $((rc))

#
# vim: noexpandtab
