{{- if or .Values.mariadb.enabled .Values.externalDatabase.host -}}

{{- if empty (include "phabricator.host" .) -}}
###############################################################################
### ERROR: You did not provide an external host in your 'helm install' call ###
###############################################################################

This deployment will be incomplete until you configure Phabricator with a resolvable
host. To configure Phabricator with the URL of your service:

1. Get the Phabricator URL by running:

  {{- if contains "NodePort" .Values.service.type }}

  export APP_PORT=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "common.names.fullname" . }} -o jsonpath="{.spec.ports[0].nodePort}")
  export APP_HOST=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")

  {{- else if contains "LoadBalancer" .Values.service.type }}

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace {{ .Release.Namespace }} -w {{ template "common.names.fullname" . }}'

  export APP_HOST=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "common.names.fullname" . }} --template "{{ "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}" }}")
  export APP_PASSWORD=$(kubectl get secret --namespace {{ .Release.Namespace }} {{ template "common.names.fullname" . }} -o jsonpath="{.data.phabricator-password}" | base64 --decode)
  export DATABASE_ROOT_PASSWORD=$(kubectl get secret --namespace {{ .Release.Namespace }} {{ template "phabricator.databaseSecretName" . }} -o jsonpath="{.data.mariadb-root-password}" | base64 --decode)
  {{- end }}

2. Complete your Phabricator deployment by running:

{{- if .Values.mariadb.enabled }}

  helm upgrade --namespace {{ .Release.Namespace }} {{ .Release.Name }} bitnami/{{ .Chart.Name }} \
    --set phabricatorHost=$APP_HOST,phabricatorPassword=$APP_PASSWORD,service.type={{ .Values.service.type }},{{- if contains "NodePort" .Values.service.type }}service.nodePort=$APP_PORT,{{- end }}mariadb.auth.rootPassword=$DATABASE_ROOT_PASSWORD

{{- else }}

  ## PLEASE UPDATE THE EXTERNAL DATABASE CONNECTION PARAMETERS IN THE FOLLOWING COMMAND AS NEEDED ##

  helm upgrade --namespace {{ .Release.Namespace }} {{ .Release.Name }} bitnami/{{ .Chart.Name }} \
    --set phabricatorHost=$APP_HOST,phabricatorPassword=$APP_PASSWORD,service.type={{ .Values.service.type }},{{- if contains "NodePort" .Values.service.type }}service.nodePort=$APP_PORT,{{- end }}mariadb.enabled=false{{- if not (empty .Values.externalDatabase.host) }},externalDatabase.host={{ .Values.externalDatabase.host }}{{- end }}{{- if not (empty .Values.externalDatabase.rootUser) }},externalDatabase.rootUser={{ .Values.externalDatabase.rootUser }}{{- end }}{{- if not (empty .Values.externalDatabase.rootPassword) }},externalDatabase.rootPassword={{ .Values.externalDatabase.rootPassword }}{{- end }}

{{- end }}

{{- else -}}

{{- if .Values.ingress.enabled }}

1. Get the Phabricator URL and associate its hostname to your cluster external IP:

   export CLUSTER_IP=$(minikube ip) # On Minikube. Use: `kubectl cluster-info` on others K8s clusters
   echo "Phabricator URL: http{{ if .Values.ingress.tls }}s{{ end }}://{{ .Values.ingress.hostname }}"
   echo "$CLUSTER_IP  {{ .Values.ingress.hostname }}" | sudo tee -a /etc/hosts

{{- else }}

1. Get the Phabricator URL by running:

{{- if eq .Values.service.type "ClusterIP" }}

  {{- if .Values.phabricatorEnableHttps }}
  echo "Phabricator URL: https://127.0.0.1/"
  kubectl port-forward --namespace {{ .Release.Namespace }} svc/{{ template "common.names.fullname" . }} 443:{{ .Values.service.httpsPort }}
  {{- else }}
  echo "Phabricator URL: http://127.0.0.1/"
  kubectl port-forward --namespace {{ .Release.Namespace }} svc/{{ template "common.names.fullname" . }} 80:{{ .Values.service.port }}
  {{- end }}

{{- else }}

  {{- if .Values.phabricatorEnableHttps }}
  {{- $port:=.Values.service.httpsPort | toString }}
  echo "Phabricator URL: https://{{ include "phabricator.host" . }}{{- if ne $port "443" }}:{{ .Values.service.httpsPort }}{{ end }}/"
  {{- else }}
  {{- $port:=.Values.service.port | toString }}
  echo "Phabricator URL: http://{{ include "phabricator.host" . }}{{- if ne $port "80" }}:{{ .Values.service.port }}{{ end }}/"
  {{- end }}

{{- end }}
{{- end }}

2. Get your Phabricator login credentials by running:

  echo Username: {{ .Values.phabricatorUsername }}
  echo Password: $(kubectl get secret --namespace {{ .Release.Namespace }} {{ template "common.names.fullname" . }} -o jsonpath="{.data.phabricator-password}" | base64 --decode)

{{- end }}

{{- else -}}

########################################################################################
### ERROR: You did not provide an external database host in your 'helm install' call ###
########################################################################################

This deployment will be incomplete until you configure Phabricator with a resolvable database
host. To configure Phabricator to use and external database host:

1. Complete your Phabricator deployment by running:

{{- if contains "NodePort" .Values.service.type }}

  export APP_HOST=$(kubectl get nodes --namespace {{ .Release.Namespace }} -o jsonpath="{.items[0].status.addresses[0].address}")

{{- else if contains "LoadBalancer" .Values.service.type }}

  NOTE: It may take a few minutes for the LoadBalancer IP to be available.
        Watch the status with: 'kubectl get svc --namespace {{ .Release.Namespace }} -w {{ template "common.names.fullname" . }}'

  export APP_HOST=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ template "common.names.fullname" . }} --template "{{ "{{ range (index .status.loadBalancer.ingress 0) }}{{ . }}{{ end }}" }}")

{{- else }}

  export APP_HOST=127.0.0.1

{{- end }}
  export APP_PASSWORD=$(kubectl get secret --namespace {{ .Release.Namespace }} {{ template "common.names.fullname" . }} -o jsonpath="{.data.phabricator-password}" | base64 --decode)

  ## PLEASE UPDATE THE EXTERNAL DATABASE CONNECTION PARAMETERS IN THE FOLLOWING COMMAND AS NEEDED ##

  helm upgrade --namespace {{ .Release.Namespace }} {{ .Release.Name }} bitnami/{{ .Chart.Name }} \
    --set phabricatorPassword=$APP_PASSWORD,phabricatorHost=$APP_HOST,service.type={{ .Values.service.type }},mariadb.enabled=false{{- if not (empty .Values.externalDatabase.rootUser) }},externalDatabase.rootUser={{ .Values.externalDatabase.rootUser }}{{- end }}{{- if not (empty .Values.externalDatabase.rootPassword) }},externalDatabase.rootPassword={{ .Values.externalDatabase.rootPassword }}{{- end }},externalDatabase.host=YOUR_EXTERNAL_DATABASE_HOST
{{- end }}

{{- include "phabricator.checkRollingTags" . }}
{{- $passwordValidationErrors := list -}}
{{- $secretName := include "common.names.fullname" . -}}
{{- $requiredPhabricatorPassword := dict "valueKey" "phabricatorPassword" "secret" $secretName "field" "phabricator-password" "context" $ -}}
{{- $requiredPhabricatorPasswordError := include "common.validations.values.single.empty" $requiredPhabricatorPassword -}}
{{- $passwordValidationErrors = append $passwordValidationErrors $requiredPhabricatorPasswordError -}}
{{- $mariadbSecretName := include "phabricator.databaseSecretName" . -}}
{{- $mariadbPasswordValidationErrors := include "common.validations.values.mariadb.passwords" (dict "secret" $mariadbSecretName "subchart" true "context" $) -}}
{{- $passwordValidationErrors = append $passwordValidationErrors $mariadbPasswordValidationErrors -}}
{{- include "common.errors.upgrade.passwords.empty" (dict "validationErrors" $passwordValidationErrors "context" $) -}}


