#!/bin/sh
set -eu

usage() {
	cat <<-'EOF'
		Usage:
		  forever [-n SECONDS] [-q] COMMAND...
	EOF
}


_SECONDS=5
QUIET=false
while getopts 'n:q' flag; do
	case "$flag" in
		(n)
			_SECONDS="$OPTARG"
			;;
		(q)
			QUIET=true
			;;
		(*)
			usage >&2
			exit 2
			;;
	esac
done
shift $((OPTIND - 1))

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


while true; do
	STATUS=0
	"$@" || STATUS=$?
	if [ "$QUIET" = false ]; then
		printf 'Exitted with code %s.\n' "$STATUS" >&2
	fi
	sleep "$_SECONDS"
done
