#!/bin/sh
#set -x

usage()
{
    cat <<EOF
usage:
  yorn [-h] ["Do you want to run command?" command]

options:
  -h            Show this help text
  -p            Show plain output, no bells or whistles

Displays the yes-or-no question and runs command on yes.
EOF
}

if [ -z "$1" ]; then
    usage
    exit 1
fi

case $1 in
    -h)
	usage
	exit 0
	;;
    -p)
	plain=1
	shift
	;;
    *)
	;;
esac

question=$1
shift
command=$*
if [ -z "$command" ]; then
    usage
    exit 1
fi

if [ -z "$plain" ]; then
    if dialog --erase-on-exit --colors --defaultno --yesno "\Zb$question\ZB" 0 0; then
	yorn=y
    fi
else
    # shellcheck disable=SC2162,SC3045
    read -n 1 -p "$question (y/N): " yorn
fi

if [ "$yorn" = "y" ] || [ "$yorn" = "Y" ]; then
    $command
fi
