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

cmd-certs-show() {
  declare desc="Show the server.crt or server.key on stdout"
  declare cmd="certs:show"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" KEY_TYPE="$2"

  verify_app_name "$APP"
  local APP_SSL_PATH="$DOKKU_ROOT/$APP/tls"

  if [[ "$KEY_TYPE" != "key" ]] && [[ "$KEY_TYPE" != "crt" ]]; then
    dokku_log_fail "specify either 'key' or 'crt'"
  fi

  if ! is_ssl_enabled "$APP"; then
    dokku_log_fail "$APP doesn't have an SSL endpoint defined"
  fi

  cat "$APP_SSL_PATH/server.$KEY_TYPE"
}

cmd-certs-show "$@"
