#!/bin/sh
name="whiched"
version="1.2"
help_message=$(/usr/bin/cat <<-EOF
Open a program with your editor
Usage: whiched [OPTIONS] PROGRAM
Options:
-h, --help print this message
-q, --quiet suppress error messages
-v, --version print version number and exit
EOF
)
. /usr/local/lib/myminiparse.sh
# NOTE: intentionally does not mirror `whichcat`/`whichhead`
# 1. doesn't make sense to handle multiple files\
# 2. when given no arguments, whiched should follow the lead of which NOT cat
for arg; do
case "$arg" in
-q|--quiet)
quiet=1
;;
*)
target=$(command -v "$arg")
if [ -z "$target" ]; then
if [ "$quiet" -eq 0 ]; then
(>&2 /usr/bin/printf "%s: No such program '%s'\n" "$name" "$arg")
fi
exit 1
else
my_editor=${EDITOR:=/usr/bin/ed}
if ! command -v "$my_editor" >/dev/null 2>&1; then
if [ "$quiet" -eq 0 ]; then
(>&2 /usr/bin/printf "%s: cannot call \$EDITOR '%s', falling back to '/usr/bin/ed'\n" "$name" "$my_editor")
fi
my_editor=/usr/bin/ed
fi
$my_editor "$target"
exit $?
fi
;;
esac
done
# if have not exited, no program was specified
if [ "$quiet" -eq 0 ]; then
(>&2 /usr/bin/printf "Usage: whiched [OPTIONS] PROGRAM\n")
exit 1
fi
exit 0