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

trigger-haproxy-vhosts-docker-args-process-deploy() {
  declare desc="nginx-vhosts core-post-deploy plugin trigger"
  declare trigger="docker-args-process-deploy"
  declare APP="$1" IMAGE_SOURCE_TYPE="$2" IMAGE_TAG="$3" PROC_TYPE="$4" CONTAINER_INDEX="$5"
  local app_domains haproxy_domains is_app_listening letsencrypt_email output proxy_container_port proxy_host_port port_map proxy_port_map proxy_scheme proxy_schemes scheme tls_internal
  local proxy_container_http_port proxy_container_http_port_candidate proxy_host_http_port_candidate
  local proxy_container_https_port proxy_container_https_port_candidate proxy_host_https_port_candidate
  local app_urls_path="$DOKKU_ROOT/$APP/URLS"
  local STDIN=$(cat)

  if [[ "$PROC_TYPE" != "web" ]]; then
    return
  fi

  if [[ "$(plugn trigger proxy-type "$APP")" != "haproxy" ]]; then
    return
  fi

  if [[ "$(plugn trigger proxy-is-enabled "$APP")" != "true" ]]; then
    return
  fi

  if ! plugn trigger domains-vhost-enabled "$APP" 2>/dev/null; then
    return
  fi

  # run this silently or the output will be set as a label
  plugn trigger domains-setup "$APP" >/dev/null

  # ensure we have a port mapping
  plugn trigger proxy-configure-ports "$APP"

  app_domains="$(plugn trigger domains-list "$APP")"
  if [[ -n "$app_domains" ]]; then
    haproxy_domains="$(echo "$app_domains" | xargs)"
    haproxy_domains="${haproxy_domains// /,}"
  fi

  output=""
  # gather port mapping information
  is_app_listening="false"
  proxy_port_map="$(plugn trigger config-get "$APP" DOKKU_PROXY_PORT_MAP)"
  for port_map in $proxy_port_map; do
    proxy_scheme="$(awk -F ':' '{ print $1 }' <<<"$port_map")"
    proxy_host_port="$(awk -F ':' '{ print $2 }' <<<"$port_map")"
    proxy_container_port="$(awk -F ':' '{ print $3 }' <<<"$port_map")"

    # ignore https for now
    if [[ "$proxy_scheme" != "http" ]] && [[ "$proxy_scheme" != "tcp" ]]; then
      continue
    fi

    # ignore 443 for now
    if [[ "$proxy_host_port" == "443" ]]; then
      continue
    fi

    # ignore everything but 80 for now
    if [[ "$proxy_host_port" != "80" ]]; then
      continue
    fi

    is_app_listening="true"
    output="$output --label haproxy.$APP-$PROC_TYPE.mode=$proxy_scheme"
    output="$output --label haproxy.$APP-$PROC_TYPE.port=$proxy_host_port"
    output="$output --label haproxy.$APP-$PROC_TYPE.localport=$proxy_container_port"
    output="$output --label haproxy.$APP-$PROC_TYPE.host=$haproxy_domains"
  done

  # add the labels for haproxy here
  # prefer the https:443 mapping to http:80 mapping
  if [[ -n "$is_app_listening" ]] && [[ -n "$haproxy_domains" ]]; then
    scheme="http"
    echo "# THIS FILE IS GENERATED BY DOKKU - DO NOT EDIT, YOUR CHANGES WILL BE OVERWRITTEN" >"$app_urls_path"
    xargs -I{} echo "$scheme://{}" <<<"$(echo "${app_domains}" | tr ' ' '\n' | sort -u)" >>"$app_urls_path"
  fi

  echo -n "$STDIN$output"
}

trigger-haproxy-vhosts-docker-args-process-deploy "$@"
