.lib/sh/stubexec.sh

Sun, 09 Jan 2022 22:41:10 -0600

author
Meredith Howard <mhoward@roomag.org>
date
Sun, 09 Jan 2022 22:41:10 -0600
changeset 971
7f391ae6b12b
parent 960
4921edc4b518
child 992
4e3e37ce84bc
permissions
-rw-r--r--

set to use my asdf-perl fork for now

realpath() {
  local dir="$(dirname -- "$1")"
  local file="$(basename -- "$1")"
  (cd "$dir" 2>/dev/null && printf '%s/%s\n' "$(pwd -P)" "$file")
}

shim_filter() {
  local binpath=''
  while read binpath; do
    case "$binpath" in
      */.plenv/shims/*)
        plenv which $1 >/dev/null 2>&1 || continue
        ;;
      */.asdf/shims/*)
        asdf which $1 >/dev/null 2>&1 || continue
        ;;
    esac
    echo "$binpath"
  done
}

realbin() {
  local bn="$(basename $1)"
  which -a "$bn" |
    grep -v "$(realpath $1)" |
    shim_filter "$bn" |
    head -n 1
}

stubexec() {
  local real_bin="$(realbin "$0")"
  if [ -x "$real_bin" ]; then
    exec "$real_bin" $*
  fi
  install_it
  stubexec $*
}

mercurial