#!/usr/bin/env bash

# shellcheck source=lib/utils.sh
source "$(dirname "$(dirname "$(dirname "$0")")")/lib/utils.sh"

plugin_name=$1
executable_path=$2

plugin_path=$(get_plugin_path "$plugin_name")
check_if_plugin_exists "$plugin_name"

full_version=$(get_preset_version_for "$plugin_name")

if [ "$full_version" == "" ]; then
  display_no_version_set "$plugin_name"
  exit 126
fi

# shellcheck disable=SC2162
IFS=' ' read -a versions <<< "$full_version"

for version in "${versions[@]}"; do
  install_path=$(find_install_path "$plugin_name" "$version")

  if [ "$version" != "system" ] && [ ! -d "$install_path" ]; then
    echo "$plugin_name $version not installed"
    exit 127
  fi

  # custom plugin hook for executable path
  if [ -f "${plugin_path}/bin/exec-path" ]; then
    cmd=$(basename "$executable_path")
    executable_path="$("${plugin_path}/bin/exec-path" "$install_path" "$cmd" "$executable_path")"
  fi

  if full_executable_path=$(get_executable_path "$plugin_name" "$version" "$executable_path"); then
    if [ -f "$full_executable_path" ]; then
      if [ -f "${plugin_path}/bin/exec-env" ]; then
        export ASDF_INSTALL_TYPE=$install_type
        export ASDF_INSTALL_VERSION=$version
        export ASDF_INSTALL_PATH=$install_path

        # shellcheck source=/dev/null
        source "${plugin_path}/bin/exec-env"

        # unset everything, we don't want to pollute
        unset ASDF_INSTALL_TYPE
        unset ASDF_INSTALL_VERSION
        unset ASDF_INSTALL_PATH
      fi

      exec "$full_executable_path" "${@:3}"
    fi
  fi
done

echo "No such command in $full_version of $plugin_name"
exit 1
