#!/usr/bin/env bash

set -e
set -u
set -o pipefail

source ".medic/_support/doctor.sh"

# run doctor in home directory if present
if [[ -f "${HOME}/bin/dev/doctor" ]]; then
  if ! step "Found a doctor script in home directory" "pushd ${HOME} > /dev/null && ./bin/dev/doctor && popd > /dev/null"; then
    exit 1
  fi
fi

cecho --green "\n▸" --bright-bold-cyan "Running initial doctor checks..."

check "tool-versions: installed" \
  "command -v mise >/dev/null || command -v asdf >/dev/null" \
  "open https://mise.jdx.dev; open 'https://asdf-vm.com/#/core-manage-asdf'"

if command -v mise >/dev/null; then
  TOOLS="mise"
elif command -v asdf >/dev/null; then
  TOOLS="asdf"
fi

check "tool-versions: erlang plugin installed" \
  "${TOOLS} plugin list | grep erlang" \
  "${TOOLS} plugin add erlang"

check "tool-versions: elixir plugin installed" \
  "${TOOLS} plugin list | grep elixir" \
  "${TOOLS} plugin add elixir"

check "tool-versions: tools are installed" \
  "${TOOLS} which erl > /dev/null && ${TOOLS} which elixir > /dev/null" \
  "${TOOLS} install"


echo ""

elixir -r .medic/require.exs -e "Medic.Doctor.run()" $*
