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

APP="$1"
NO_VHOST=$(config_get $APP NO_VHOST || true)
RE_IPV4="$(get_ipv4_regex)"
RE_IPV6="$(get_ipv6_regex)"

[[ -f "$DOKKU_ROOT/VHOST" ]] && GLOBAL_VHOST=$(< "$DOKKU_ROOT/VHOST")

if [[ -n "$NO_VHOST" ]]; then
  echo true # bind to external ip. VHOST is disabled
elif [[ "$GLOBAL_VHOST" =~ $RE_IPV4 ]] || [[ "$GLOBAL_VHOST" =~ $RE_IPV6 ]]; then
  echo true # bind to external ip. GLOBAL_VHOST is somehow an IPv4 or IPv6 address
elif [[ -z "$GLOBAL_VHOST" ]] && [[ ! -f "$DOKKU_ROOT/$APP/VHOST" ]]; then
  echo true # bind to external ip. no GLOBAL_VHOST and no app vhost
elif [[ -f "$DOKKU_ROOT/$APP/VHOST" ]]; then
  echo false # bind to docker ip. this app has a vhost defined
else
  echo false
fi
