~dricottone/my-utils

ref: 94adb652c1b1c49d31ae1abd75b1b03867ed1382 my-utils/core/whichvi -rwxr-xr-x 1.2 KiB
94adb652Dominic Ricottone Added to mkbak test suite 2 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/bin/sh

name="whichvi"
version="1.2"
help_message=$(/usr/bin/cat <<-EOF
	Open a program with your visual editor
	Usage: whichvi [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)
    #handled in myminiparse.sh
    ;;

  *)
    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_visual=${VISUAL:=/usr/bin/vi}
      if ! command -v "$my_visual" >/dev/null 2>&1; then
        if [ "$quiet" -eq 0 ]; then
          (>&2 /usr/bin/printf "%s: cannot call \$VISUAL '%s', falling back to '/usr/bin/vi'\n" "$name" "$my_visual")
        fi
        my_visual=/usr/bin/vi
      fi
      $my_visual "$target"
      exit $?
    fi
    ;;
  esac
done

if [ "$quiet" -eq 0 ]; then
  (>&2 /usr/bin/printf "Usage: whichvi [OPTIONS] PROGRAM\n")
fi

exit 1