{{- $domains := .Values.domains -}}
{{- if .Values.global }}
    {{- if .Values.global.domains }}
        {{- $domains = .Values.global.domains -}}
    {{- end -}}
{{- end -}}

{{- if not $domains -}}
{{- $name := (dict "name" (.Values.environment.ADDRESS | replace "https://" "" | replace "http://" "")) -}}
{{- $domains = (list (merge .Values.environment $name)) -}}
{{- end -}}

{{- range $domains }}
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: portowncloudconfig-{{ .name | replace "." "-" | replace ":" "-" }}
  namespace: {{ $.Values.global.namespace.name | default $.Release.Namespace  }}
data:
  OWNCLOUD_OAUTH_CLIENT_ID: {{ .OAUTH_CLIENT_ID | quote }}
  OWNCLOUD_INSTALLATION_URL: {{ .ADDRESS | quote }}
  OWNCLOUD_OAUTH_CLIENT_SECRET: {{ .OAUTH_CLIENT_SECRET | quote }}
  OWNCLOUD_DISPLAYNAME: {{ .DISPLAYNAME | quote }}
  OWNCLOUD_INFO_URL: {{ .INFO_URL | quote }}
  OWNCLOUD_HELP_URL: {{ .HELP_URL | quote }}
  OWNCLOUD_ICON: {{ .ICON | quote }}
  SERVICENAME: {{ .name | replace "." "-" | replace ":" "-" }}
  {{ if not .INTERNAL_ADDRESS }}
  OWNCLOUD_INTERNAL_INSTALLATION_URL: {{ .ADDRESS | quote }}
  {{ end }}
  {{ if .INTERNAL_ADDRESS }}
  OWNCLOUD_INTERNAL_INSTALLATION_URL: {{ .INTERNAL_ADDRESS | quote }}
  {{ end }}
{{- end }}
