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

proxy_ports_add_cmd() {
  declare desc="add proxy port mappings from an app"
  local cmd="proxy:add"
  local APP="$2"; verify_app_name "$APP"
  local PROXY_PORT_MAP="$(config_get "$APP" DOKKU_PROXY_PORT_MAP || true)"
  shift 2

  local INPUT_PORTS="$*"
  if [[ -n "$INPUT_PORTS" ]]; then
    PROXY_PORT_MAP="$(merge_dedupe_list "$PROXY_PORT_MAP $INPUT_PORTS" " ")"
    config_set --no-restart "$APP" DOKKU_PROXY_PORT_MAP="$PROXY_PORT_MAP"
  else
    dokku proxy:help
    dokku_log_fail "No port mapping specified. Exiting..."
  fi
  plugn trigger post-proxy-ports-update "$APP" "add"
}

proxy_ports_add_cmd "$@"
