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

cmd-nginx-report() {
  declare desc="displays a nginx report for one or more apps"
  declare cmd="nginx:report"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" INFO_FLAG="$2"

  if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then
    INFO_FLAG="$APP"
    APP=""
  fi

  if [[ -z "$APP" ]] && [[ -z "$INFO_FLAG" ]]; then
    INFO_FLAG="true"
  fi

  if [[ -z "$APP" ]]; then
    for app in $(dokku_apps); do
      cmd-nginx-report-single "$app" "$INFO_FLAG" | tee || true
    done
  else
    cmd-nginx-report-single "$APP" "$INFO_FLAG"
  fi
}

cmd-nginx-report-single() {
  declare APP="$1" INFO_FLAG="$2"
  if [[ "$INFO_FLAG" == "true" ]]; then
    INFO_FLAG=""
  fi
  verify_app_name "$APP"
  local flag_map=(
    "--nginx-access-log-format: $(fn-nginx-access-log-format "$APP")"
    "--nginx-computed-access-log-format: $(fn-nginx-computed-access-log-format "$APP")"
    "--nginx-global-access-log-format: $(fn-nginx-global-access-log-format "$APP")"
    "--nginx-access-log-path: $(fn-nginx-access-log-path "$APP")"
    "--nginx-computed-access-log-path: $(fn-nginx-computed-access-log-path "$APP")"
    "--nginx-global-access-log-path: $(fn-nginx-global-access-log-path "$APP")"
    "--nginx-bind-address-ipv4: $(fn-nginx-bind-address-ipv4 "$APP")"
    "--nginx-computed-bind-address-ipv4: $(fn-nginx-computed-bind-address-ipv4 "$APP")"
    "--nginx-global-bind-address-ipv4: $(fn-nginx-global-bind-address-ipv4 "$APP")"
    "--nginx-bind-address-ipv6: $(fn-nginx-bind-address-ipv6 "$APP")"
    "--nginx-computed-bind-address-ipv6: $(fn-nginx-computed-bind-address-ipv6 "$APP")"
    "--nginx-global-bind-address-ipv6: $(fn-nginx-global-bind-address-ipv6 "$APP")"
    "--nginx-client-body-timeout: $(fn-nginx-client-body-timeout "$APP")"
    "--nginx-computed-client-body-timeout: $(fn-nginx-computed-client-body-timeout "$APP")"
    "--nginx-global-client-body-timeout: $(fn-nginx-global-client-body-timeout "$APP")"
    "--nginx-client-header-timeout: $(fn-nginx-client-header-timeout "$APP")"
    "--nginx-computed-client-header-timeout: $(fn-nginx-computed-client-header-timeout "$APP")"
    "--nginx-global-client-header-timeout: $(fn-nginx-global-client-header-timeout "$APP")"
    "--nginx-client-max-body-size: $(fn-nginx-client-max-body-size "$APP")"
    "--nginx-computed-client-max-body-size: $(fn-nginx-computed-client-max-body-size "$APP")"
    "--nginx-global-client-max-body-size: $(fn-nginx-global-client-max-body-size "$APP")"
    "--nginx-disable-custom-config: $(fn-nginx-disable-custom-config "$APP")"
    "--nginx-computed-disable-custom-config: $(fn-nginx-computed-disable-custom-config "$APP")"
    "--nginx-global-disable-custom-config: $(fn-nginx-global-disable-custom-config "$APP")"
    "--nginx-error-log-path: $(fn-nginx-error-log-path "$APP")"
    "--nginx-computed-error-log-path: $(fn-nginx-computed-error-log-path "$APP")"
    "--nginx-global-error-log-path: $(fn-nginx-global-error-log-path "$APP")"
    "--nginx-hsts-include-subdomains: $(fn-nginx-hsts-include-subdomains "$APP")"
    "--nginx-computed-hsts-include-subdomains: $(fn-nginx-computed-hsts-include-subdomains "$APP")"
    "--nginx-global-hsts-include-subdomains: $(fn-nginx-global-hsts-include-subdomains "$APP")"
    "--nginx-hsts-max-age: $(fn-nginx-hsts-max-age "$APP")"
    "--nginx-computed-hsts-max-age: $(fn-nginx-computed-hsts-max-age "$APP")"
    "--nginx-global-hsts-max-age: $(fn-nginx-global-hsts-max-age "$APP")"
    "--nginx-hsts-preload: $(fn-nginx-hsts-preload "$APP")"
    "--nginx-computed-hsts-preload: $(fn-nginx-computed-hsts-preload "$APP")"
    "--nginx-global-hsts-preload: $(fn-nginx-global-hsts-preload "$APP")"
    "--nginx-hsts: $(fn-nginx-hsts "$APP")"
    "--nginx-computed-hsts: $(fn-nginx-computed-hsts "$APP")"
    "--nginx-global-hsts: $(fn-nginx-global-hsts "$APP")"
    "--nginx-last-visited-at: $(fn-nginx-vhosts-last-visited-at "$APP")"
    "--nginx-keepalive-timeout: $(fn-nginx-keepalive-timeout "$APP")"
    "--nginx-computed-keepalive-timeout: $(fn-nginx-computed-keepalive-timeout "$APP")"
    "--nginx-global-keepalive-timeout: $(fn-nginx-global-keepalive-timeout "$APP")"
    "--nginx-lingering-timeout: $(fn-nginx-lingering-timeout "$APP")"
    "--nginx-computed-lingering-timeout: $(fn-nginx-computed-lingering-timeout "$APP")"
    "--nginx-global-lingering-timeout: $(fn-nginx-global-lingering-timeout "$APP")"
    "--nginx-nginx-conf-sigil-path: $(fn-nginx-nginx-conf-sigil-path "$APP")"
    "--nginx-computed-nginx-conf-sigil-path: $(fn-nginx-computed-nginx-conf-sigil-path "$APP")"
    "--nginx-global-nginx-conf-sigil-path: $(fn-nginx-global-nginx-conf-sigil-path "$APP")"
    "--nginx-proxy-buffer-size: $(fn-nginx-proxy-buffer-size "$APP")"
    "--nginx-computed-proxy-buffer-size: $(fn-nginx-computed-proxy-buffer-size "$APP")"
    "--nginx-global-proxy-buffer-size: $(fn-nginx-global-proxy-buffer-size "$APP")"
    "--nginx-proxy-buffering: $(fn-nginx-proxy-buffering "$APP")"
    "--nginx-computed-proxy-buffering: $(fn-nginx-computed-proxy-buffering "$APP")"
    "--nginx-global-proxy-buffering: $(fn-nginx-global-proxy-buffering "$APP")"
    "--nginx-proxy-buffers: $(fn-nginx-proxy-buffers "$APP")"
    "--nginx-computed-proxy-buffers: $(fn-nginx-computed-proxy-buffers "$APP")"
    "--nginx-global-proxy-buffers: $(fn-nginx-global-proxy-buffers "$APP")"
    "--nginx-proxy-busy-buffers-size: $(fn-nginx-proxy-busy-buffers-size "$APP")"
    "--nginx-computed-proxy-busy-buffers-size: $(fn-nginx-computed-proxy-busy-buffers-size "$APP")"
    "--nginx-global-proxy-busy-buffers-size: $(fn-nginx-global-proxy-busy-buffers-size "$APP")"
    "--nginx-proxy-connect-timeout: $(fn-nginx-proxy-connect-timeout "$APP")"
    "--nginx-computed-proxy-connect-timeout: $(fn-nginx-computed-proxy-connect-timeout "$APP")"
    "--nginx-global-proxy-connect-timeout: $(fn-nginx-global-proxy-connect-timeout "$APP")"
    "--nginx-proxy-read-timeout: $(fn-nginx-proxy-read-timeout "$APP")"
    "--nginx-computed-proxy-read-timeout: $(fn-nginx-computed-proxy-read-timeout "$APP")"
    "--nginx-global-proxy-read-timeout: $(fn-nginx-global-proxy-read-timeout "$APP")"
    "--nginx-proxy-send-timeout: $(fn-nginx-proxy-send-timeout "$APP")"
    "--nginx-computed-proxy-send-timeout: $(fn-nginx-computed-proxy-send-timeout "$APP")"
    "--nginx-global-proxy-send-timeout: $(fn-nginx-global-proxy-send-timeout "$APP")"
    "--nginx-send-timeout: $(fn-nginx-send-timeout "$APP")"
    "--nginx-computed-send-timeout: $(fn-nginx-computed-send-timeout "$APP")"
    "--nginx-global-send-timeout: $(fn-nginx-global-send-timeout "$APP")"
    "--nginx-underscore-in-headers: $(fn-nginx-underscore-in-headers "$APP")"
    "--nginx-computed-underscore-in-headers: $(fn-nginx-computed-underscore-in-headers "$APP")"
    "--nginx-global-underscore-in-headers: $(fn-nginx-global-underscore-in-headers "$APP")"
    "--nginx-x-forwarded-for-value: $(fn-nginx-x-forwarded-for-value "$APP")"
    "--nginx-computed-x-forwarded-for-value: $(fn-nginx-computed-x-forwarded-for-value "$APP")"
    "--nginx-global-x-forwarded-for-value: $(fn-nginx-global-x-forwarded-for-value "$APP")"
    "--nginx-x-forwarded-port-value: $(fn-nginx-x-forwarded-port-value "$APP")"
    "--nginx-computed-x-forwarded-port-value: $(fn-nginx-computed-x-forwarded-port-value "$APP")"
    "--nginx-global-x-forwarded-port-value: $(fn-nginx-global-x-forwarded-port-value "$APP")"
    "--nginx-x-forwarded-proto-value: $(fn-nginx-x-forwarded-proto-value "$APP")"
    "--nginx-computed-x-forwarded-proto-value: $(fn-nginx-computed-x-forwarded-proto-value "$APP")"
    "--nginx-global-x-forwarded-proto-value: $(fn-nginx-global-x-forwarded-proto-value "$APP")"
    "--nginx-x-forwarded-ssl: $(fn-nginx-x-forwarded-ssl "$APP")"
    "--nginx-computed-x-forwarded-ssl: $(fn-nginx-computed-x-forwarded-ssl "$APP")"
    "--nginx-global-x-forwarded-ssl: $(fn-nginx-global-x-forwarded-ssl "$APP")"
  )

  if [[ -z "$INFO_FLAG" ]]; then
    dokku_log_info2_quiet "${APP} nginx information"
    for flag in "${flag_map[@]}"; do
      key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
      dokku_log_verbose "$(printf "%-30s %-25s" "${key^}" "${flag#*: }")"
    done
  else
    local match=false
    local value_exists=false
    for flag in "${flag_map[@]}"; do
      valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
      if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
        value=${flag#*: }
        size="${#value}"
        if [[ "$size" -ne 0 ]]; then
          echo "$value" && match=true && value_exists=true
        else
          match=true
        fi
      fi
    done
    [[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
    [[ "$value_exists" == "true" ]] || dokku_log_fail "not deployed"
  fi
}

cmd-nginx-show-config() {
  declare desc="display app nginx config"
  declare cmd="nginx:show-config"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1"
  local proxy_type

  verify_app_name "$APP"
  proxy_type="$(plugn trigger proxy-type "$APP")"

  local DOKKU_SCHEDULER=$(get_app_scheduler "$APP")
  if [[ "$DOKKU_SCHEDULER" != "docker-local" ]] && [[ "$DOKKU_SCHEDULER" != "null" ]]; then
    plugn trigger scheduler-proxy-config "$DOKKU_SCHEDULER" "$APP" "$proxy_type"
    return $?
  fi

  if [[ "$proxy_type" != "nginx" ]]; then
    dokku_log_fail "This command is only available for apps with the nginx proxy type"
  fi

  if [[ ! -f "$DOKKU_ROOT/$APP/nginx.conf" ]]; then
    dokku_log_fail "No nginx.conf exists for $APP"
  fi

  cat "$DOKKU_ROOT/$APP/nginx.conf"
}

cmd-nginx-start() {
  declare desc="starts the nginx server"
  declare cmd="nginx:start"
  [[ "$1" == "$cmd" ]] && shift 1

  fn-plugin-property-write "nginx" "--global" "proxy-status" "started"
  fn-nginx-vhosts-nginx-init-cmd "enable"
  fn-nginx-vhosts-nginx-init-cmd "start"
}

cmd-nginx-stop() {
  declare desc="stops the nginx server"
  declare cmd="nginx:stop"
  [[ "$1" == "$cmd" ]] && shift 1

  fn-plugin-property-write "nginx" "--global" "proxy-status" "stopped"
  fn-nginx-vhosts-nginx-init-cmd "stop"
  fn-nginx-vhosts-nginx-init-cmd "disable"
}

cmd-nginx-validate-config() {
  declare desc="validates and optionally cleans up invalid nginx configurations"
  declare cmd="nginx:validate-config"
  [[ "$1" == "$cmd" ]] && shift 1
  declare APP="$1" FLAG="$2"

  validate_nginx "$APP" "$FLAG"
}
