#!/bin/sh
#
# rust-analyzer-wrapper
#
# for use in vim/nvim: lsp setup
#   `let g:ale_rust_analyzer_executable = "~/bin/dev/rust-analyzer-wrapper"`

runra() {
  bin="$1"
  if [ -n "${RA_LOG_STDERR}" ]; then
    "${bin}" 2>"${RA_LOG_STDERR}"
  else
    "${bin}"
  fi
}

brew_bin="/opt/homebrew/opt/rustup/bin/rust-analyzer"
cargo_bin="${CARGO_INSTALL_ROOT:-}/bin/rust-analyzer"
# coc_bin="${HOME}/.config/coc/extensions/coc-rust-analyzer-data/rust-analyzer"
bin=""
if [ -x "${cargo_bin}" ]; then
  bin="${cargo_bin}"
elif [ -x "${brew_bin}" ]; then
  bin="${brew_bin}"
# elif [ -x "${coc_bin}" ]; then
#   bin="${coc_bin}"
else
  echo "rust-analyzer binary not found" >&2
  exit 1
fi

echo "choice: ${bin}" >&2
runra "${bin}"
