#!/bin/sh -e
# SPDX-License-Identifier: WTFPL
# shellcheck enable=

usage () {
	cat <<- EOF
		usage: $0 [basename|dirname|find|fzf|grep|head|locate|ls|printenv|sed|sort|tail|uniq|xargs] ARGS...

		Executes the given utility and its ARGS along with the appropriate
		argument for accepting NUL-separated lines instead of
		newline-separated.

		For example:

		    $0 xargs rm -v

		is a shorthand for:

		    xargs -0 rm -v
	EOF
}

if [ $# -lt 1 ]
then
	usage >&2
	exit 64  # EX_USAGE
fi

cmd=$1
shift 1

case "$cmd" in
	find)
		exec find "$@" -print0
		;;
	locate|printenv|xargs)
		exec "$cmd" -0 "$@"
		;;
	basename|dirname|grep|sort|uniq|sed|head|tail)
		exec "$cmd" -z "$@"
		;;
	ls)
		exec "$cmd" --zero "$@"
		;;
	fzf)
		exec "$cmd" --read0 --print0 "$@"
		;;
	-h)
		usage
		exit 0
		;;
	*)
		printf "unrecognized command: %s\n\n" "$cmd" >&2
		usage >&2
		exit 64  # EX_USAGE
		;;
esac
