#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  prompt STRING|-
		  prompt -h
	EOF
}

help() {
	cat <<-'EOF'


		Options:
		  -h, --help    show this message

		  STRING        the text to be displayed in the prompt


		Display a prompt and return a value corresponding to the
		response.


		Examples:

		  Conditionally run download command:

		    if prompt 'Download files?'; then
			run_download;
		    fi
	EOF
}


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

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

STRING="${1:-}"
eval "$(assert-arg -- "$STRING" 'STRING')"

printf '%s' "$STRING"
printf ' [Y/n]: '
read -r yesno
if [ "$yesno" != 'n' ] && [ "$yesno" != 'N' ]; then
	exit 0
else
	exit 1
fi
