#!/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_vhosts() {
  declare desc="return default vhosts"
  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 VHOSTS=$(get_global_vhosts)

    while read -r VHOST; do
      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
        echo "$hostname"
      fi
    done <<< "$VHOSTS"
  fi
}

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_VHOSTS="$(get_default_vhosts "$APP")"

  if [[ ! -f $APP_VHOST_PATH ]]; then
    if [[ -n "$DEFAULT_VHOSTS" ]]; then
      dokku_log_info1 "Creating new $APP_VHOST_PATH..."
      echo "$DEFAULT_VHOSTS" > "$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"
  shift 1

  for DOMAIN in "$@"; do
    sed -i "\|^$DOMAIN\$|d" "$APP_VHOST_PATH"
    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_VHOSTS="$(get_default_vhosts "$APP")"

  if [[ "$(is_app_vhost_enabled "$APP")" == "false" ]];then
    if [[ -n "$DEFAULT_VHOSTS" ]]; then
      echo "$DEFAULT_VHOSTS" > "$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_add_global() {
  declare desc="add list of global domains"
  local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"

  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$" "$GLOBAL_VHOST_PATH" > /dev/null 2>&1; echo $?) -eq 0 ]]; then
      dokku_log_info1 "Skipping: $DOMAIN already added"
    else
      echo "$DOMAIN" >> "$GLOBAL_VHOST_PATH"
      dokku_log_info1 "Added $DOMAIN"
    fi
  done
}

domains_remove_global() {
  declare desc="remove list of domains"
  local GLOBAL_VHOST_PATH="$DOKKU_ROOT/VHOST"

  for DOMAIN in "$@"; do
    sed -i "\|^$DOMAIN\$|d" "$GLOBAL_VHOST_PATH"
    dokku_log_info1 "Removed $DOMAIN"
  done
}
