#!/usr/bin/env bash

# Bash completion script that defers to a cached completion script representing
# the state of the current QIIME 2 deployment.
#
# This script is intended to be executed on the command-line or in
# .bashrc/.bash_profile:
#
#     source tab-qiime
#

_qiime_completion()
{
  # Attempt to find the cached completion script. If q2cli isn't installed, or
  # is an incompatible version, don't attempt completion.
  local completion_path="$(python -c "import q2cli.util; print(q2cli.util.get_completion_path())" 2> /dev/null)"

  if [[ $? != 0 ]]; then
    unset COMPREPLY
    return 0
  fi

  # If the completion script exists, attempt completion by invoking the script
  # in a subshell, supplying COMP_WORDS and COMP_CWORD. Capture the output as
  # the completion reply. If the completion script failed, don't attempt
  # completion.
  if [[ -f "$completion_path" ]] ; then
    COMPREPLY=( $(COMP_WORDS="${COMP_WORDS[*]}" COMP_CWORD="${COMP_CWORD}" "$completion_path" 2> /dev/null) )

    if [[ $? != 0 ]]; then
      unset COMPREPLY
      return 0
    fi
  else
    unset COMPREPLY
    return 0
  fi

  return 0
}

# Enable default readline and bash completion behavior when `_qiime_completion`
# doesn't have a reply.
complete -F _qiime_completion -o default -o bashdefault qiime

# Execute a `qiime` command (any command will do) so that tab-completion will
# work out-of-the-box (e.g. with a fresh installation of q2cli). Running a
# command will create or refresh the cache if necessary, which contains the
# actual completion script.
#
# Ignore stdout to avoid displaying help text to users enabling tab-completion.
# stderr displays the note about cache refreshing, as that can take a few
# moments to complete.
qiime > /dev/null
