#!/bin/sh

set -o errexit
set -o nounset

all() {
  if builtin which "${@-}" >/dev/null 2>&1; then
    builtin which -a -s -x 2 "${@-}"
  elif command '/opt/homebrew/bin/which' "${@-}" >/dev/null 2>&1; then
    command '/opt/homebrew/bin/which' "${@-}"
  elif command '/opt/homebrew/bin/gwhich' "${@-}" >/dev/null 2>&1; then
    command '/opt/homebrew/bin/gwhich' "${@-}"
  elif command '/usr/bin/which' "${@-}" >/dev/null 2>&1; then
    command '/usr/bin/which' "${@-}"
  elif command '/bin/which' "${@-}" >/dev/null 2>&1; then
    command '/bin/which' "${@-}"
  else
    exit 1
  fi
}

name=$(basename "$0")
executable=$(all -a "${name}" | sed -n '2p')
if [ -x "${executable}" ]; then
  exec "${executable}" "$@"
else
  exec llvm "${name}" "$@"
fi
