#!/usr/bin/env bash
# Summary: List all Java versions that contain the given executable
# Usage: jenv whence [--path] <command>

set -e
[ -n "$JENV_DEBUG" ] && set -x

# Provide jenv completions
if [ "$1" = "--complete" ]; then
  echo --path
  exec jenv shims --short
fi

if [ "$1" = "--path" ]; then
  print_paths="1"
  shift
else
  print_paths=""
fi

whence() {
  local command="$1"
  jenv-versions --bare | while read version; do
    path="$(jenv-prefix "$version")/bin/${command}"
    if [ -x "$path" ]; then
      [ "$print_paths" ] && echo "$path" || echo "$version"
    fi
  done
}

JENV_COMMAND="$1"
if [ -z "$JENV_COMMAND" ]; then
  jenv-help --usage whence >&2
  exit 1
fi

result="$(whence "$JENV_COMMAND")"
[ -n "$result" ] && echo "$result"
