#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/plugin/functions"
source "$PLUGIN_AVAILABLE_PATH/plugin/internal-functions"

cmd-plugin-install() {
  declare desc="installs a plugin from URL and calls install plugin trigger via command line"
  declare cmd="plugin:install"
  [[ "$1" == "$cmd" ]] && shift 1
  declare FLAG="$1"

  case "$FLAG" in
    --core)
      [[ "$#" -gt 1 ]] && dokku_log_info1_quiet "Cannot install additional core plugins, running core plugin install trigger"
      PLUGIN_PATH="$PLUGIN_CORE_PATH" plugn trigger install
      ;;
    https:* | git* | ssh:* | file:* | *.tar.gz | *.tgz)
      download_and_enable_plugin "$@"
      plugn trigger install
      ;;
    *)
      plugn trigger install
      ;;
  esac
  plugin_prime_bash_completion
}

cmd-plugin-install "$@"
