{{- $suitecrmSecretName := include "suitecrm.fullname" . -}}

{{- if or .Values.mariadb.enabled .Values.externalDatabase.host -}}
{{- $mariadbSecretName := include "suitecrm.mariadb.fullname" . -}}

1. Get the SuiteCRM URL by running:

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

  echo "SuiteCRM URL: http://127.0.0.1:8080/"
  kubectl port-forward --namespace {{ .Release.Namespace }} svc/{{ include "suitecrm.fullname" . }} $POD_NAME 8080:{{ .Values.service.port }}

{{- else }}

{{- if not .Values.suitecrmHost }}

  This deployment will be incomplete until you configure SuiteCRM with a resolvable
  host.

  ############################################################
  # Complete the deployment running the following commands
  ############################################################

  export SUITECRM_HOST=$(kubectl get svc --namespace {{ .Release.Namespace }} {{ include "suitecrm.fullname" . }} --template "{{"{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}"}}")
  export SUITECRM_PASSWORD=$(kubectl get secret --namespace {{ .Release.namespace }} {{ include "suitecrm.fullname" . }} -o jsonpath="{.data.suitecrm-password}" | base64 --decode)
  export MARIADB_PASSWORD=$(kubectl get secret --namespace {{ .Release.namespace }} {{ include "suitecrm.fullname" . }} -o jsonpath="{.data.mariadb-password}" | base64 --decode)

  helm upgrade {{ include "suitecrm.fullname" . }} bitnami/suitecrm \
    --set service.type=LoadBalancer,suitecrmHost=$SUITECRM_HOST,suitecrmPassword=$SUITECRM_PASSWORD,mariadb.auth.password=$MARIADB_PASSWORD

{{- else }}
  echo "SuiteCRM URL: http://{{ include "suitecrm.host" . }}:{{ .Values.service.port }}/"
{{- end }}

{{- end }}

2. Get your SuiteCRM login credentials by running:

  echo Username : {{ .Values.suitecrmUsername }}
  echo Password : $(kubectl get secret --namespace {{ .Release.Namespace }} {{ include "suitecrm.fullname" . }} -o jsonpath="{.data.suitecrm-password}" | base64 --decode)

{{- else -}}
{{-   print "MariaDB must be enabled or you must provide an external database" | fail -}}
{{- end }}

{{- include "common.warnings.rollingTag" .Values.image }}
{{- include "common.warnings.rollingTag" .Values.metrics.image }}


{{- $passwordValidationErrors := list -}}
{{- if not .Values.existingSecret -}}
    {{- $secretName := include "suitecrm.fullname" . -}}
    {{- $requiredSuiteCRMPassword := dict "valueKey" "suitecrmPassword" "secret" $secretName "field" "suitecrm-password" "context" $ -}}
    {{- $requiredSuiteCRMPasswordError := include "common.validations.values.single.empty" $requiredSuiteCRMPassword -}}
    {{- $passwordValidationErrors = append $passwordValidationErrors $requiredSuiteCRMPasswordError -}}
{{- end -}}
{{- $mariadbSecretName := include "suitecrm.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" $) -}}
