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

get_app_domains() {
  declare desc="return app domains"
  verify_app_name "$1"
  local APP=$1; local APP_VHOST_FILE="$DOKKU_ROOT/$APP/VHOST"
  local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"
  local GLOBAL_HOSTNAME_PATH="$DOKKU_ROOT/HOSTNAME"

  if [[ "$(is_app_vhost_enabled "$APP")" == "true" ]]; then
    if [[ -f "$APP_VHOST_FILE" ]]; then
      cat "$APP_VHOST_FILE"
    elif [[ -f "$GLOBAL_VHOST_PATH" ]]; then
      cat "$GLOBAL_VHOST_PATH"
    elif [[ -f "$GLOBAL_HOSTNAME_PATH" ]]; then
      cat "$GLOBAL_HOSTNAME_PATH"
    fi
  fi
}

get_default_vhost() {
  declare desc="return default vhost"
  verify_app_name "$1"
  local APP="$1"; local RE_IPV4="$(get_ipv4_regex)"; local RE_IPV6="$(get_ipv6_regex)"

  if [[ "$(is_global_vhost_enabled)" == "true" ]]; then
    local VHOST=$(get_global_vhost)
    if ! ([[ "$VHOST" =~ $RE_IPV4 ]] || [[ "$VHOST" =~ $RE_IPV6 ]]); then
      local SUBDOMAIN=${APP/%\.${VHOST}/}
      local hostname=$(: | plugn trigger nginx-hostname "$APP" "$SUBDOMAIN" "$VHOST")
      if [[ ! -n $hostname ]]; then
        if [[ "$APP" == *.* ]] && [[ "$SUBDOMAIN" == "$APP" ]]; then
          local hostname="${APP/\//-}"
        else
          local hostname="${APP/\//-}.$VHOST"
        fi
      fi
      local DEFAULT_VHOST="$hostname"
    fi
  fi
  echo "$DEFAULT_VHOST"
}

domains_setup() {
  declare desc="setup domains to default state"
  verify_app_name "$1"
  local APP="$1"; local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"; local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"
  local RE_IPV4="$(get_ipv4_regex)"; local RE_IPV6="$(get_ipv6_regex)"
  local DEFAULT_VHOST="$(get_default_vhost "$APP")"

  if [[ ! -f $APP_VHOST_PATH ]]; then
    if [[ -n "$DEFAULT_VHOST" ]]; then
      dokku_log_info1 "Creating new $APP_VHOST_PATH..."
      echo "$DEFAULT_VHOST" > "$APP_VHOST_PATH"
    else
      dokku_log_info2 "no global VHOST set. disabling vhost support"
      disable_app_vhost "$APP" --no-restart
    fi
  fi
}

domains_add() {
  declare desc="add list of domains to app"
  verify_app_name "$1"
  local APP="$1"; local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"

  shift 1
  for DOMAIN in "$@"; do
    if ! (is_valid_hostname "$DOMAIN"); then
      dokku_log_fail "$DOMAIN is invalid. exiting..."
    fi
  done

  for DOMAIN in "$@"; do
    if [[ $(egrep -w "^$DOMAIN$" "$APP_VHOST_PATH" > /dev/null 2>&1; echo $?) -eq 0 ]]; then
      dokku_log_info1 "Skipping: $DOMAIN already added to $APP"
    else
      echo "$DOMAIN" >> "$APP_VHOST_PATH"
      dokku_log_info1 "Added $DOMAIN to $APP"
    fi
  done

  if [[ "$(is_app_vhost_enabled "$APP")" == "false" ]];then
    domains_enable "$APP" --no-restart
  fi

  plugn trigger post-domains-update "$APP" "add" "$@"
}

domains_remove() {
  declare desc="remove list of app domains"
  verify_app_name "$1"
  local APP="$1"; local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
  local DEFAULT_VHOST="$(get_default_vhost "$APP")"
  shift 1

  for DOMAIN in "$@"; do
    sed -i "\|^$DOMAIN\$|d" "$DOKKU_ROOT/$APP/VHOST"
    dokku_log_info1 "Removed $DOMAIN from $APP"
  done
  plugn trigger post-domains-update "$APP" "remove" "$@"
}

domains_disable() {
  declare desc="disable domains/VHOST support"
  verify_app_name "$1"
  local APP="$1"; local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"

  if [[ "$(is_app_vhost_enabled "$APP")" == "true" ]];then
    disable_app_vhost "$APP"
  else
    dokku_log_info1 "domains (VHOST) support is already disabled for app ($APP)"
  fi
}

domains_enable() {
  declare desc="enable domains/VHOST support"
  verify_app_name "$1"
  local APP="$1"; local APP_VHOST_PATH="$DOKKU_ROOT/$APP/VHOST"
  local DEFAULT_VHOST="$(get_default_vhost "$APP")"

  if [[ "$(is_app_vhost_enabled "$APP")" == "false" ]];then
    if [[ -n "$DEFAULT_VHOST" ]]; then
      echo "$DEFAULT_VHOST" > "$APP_VHOST_PATH"
    fi
    [[ "$2" == "--no-restart" ]] && local ENABLE_APP_VHOST_ARGS=$2
    enable_app_vhost "$APP" "$ENABLE_APP_VHOST_ARGS"
  else
    dokku_log_info1 "domains (VHOST) support is already enabled for app ($APP)"
  fi
}

domains_set_global() {
  declare desc="set global domain name"
  local NEW_GLOBAL_VHOST="$1"; local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"

  if [[ -n "$NEW_GLOBAL_VHOST" ]]; then
    echo "$NEW_GLOBAL_VHOST" > "$GLOBAL_VHOST_PATH"
    dokku_log_info1 "Set global domain to $NEW_GLOBAL_VHOST"
  else
    dokku_log_fail "New global domain name must not be blank"
  fi
}
