{{- $selector := dict -}}
{{- range $name := (list "main" "nextcloud-cron" "preview-cron") -}}
  {{/* creates main: {} */}}
  {{- $_ := set $selector $name dict -}}
  {{/* sets main: {} to main: { main: {} } */}}
  {{- $_ := set (get $selector $name) $name dict -}}
{{- end -}}

{{- range $item := .Values.persistenceList -}}
  {{- $_ := set $item "targetSelector" $selector -}}
{{- end -}}

{{/* Make sure all variables are set properly */}}
{{- include "tc.v1.common.loader.init" . -}}

{{- include "nextcloud.validation" $ -}}

{{/* Render configmaps for all pods */}}
{{- $configmaps := include "nextcloud.configmaps" . | fromYaml -}}
{{- if $configmaps -}}
  {{- $_ := mustMergeOverwrite .Values.configmap $configmaps -}}
{{- end -}}

{{/* Create hostAliases (resolve ingress host to Node/LB IP) */}}
{{- $hostAlias := (list (dict
    "ip" .Values.nextcloud.general.accessIP
    "hostnames" (
      list (include "nextcloud.accesshost" $)
    )
  )) -}}

{{/* Add [hostAliases] to nextcloud and collabora pod */}}
{{- $_ := set .Values.workload.main.podSpec "hostAliases" $hostAlias -}}
{{- $_ := set .Values.workload.collabora.podSpec "hostAliases" $hostAlias -}}

{{/* Add [init perms] container to nextcloud */}}
{{- if not (get .Values.workload.main.podSpec "initContainers") -}}
  {{- $_ := set .Values.workload.main.podSpec "initContainers" dict -}}
{{- end -}}

{{- $initPerms := (include "nextcloud.init.perms" . | fromYaml) -}}
{{- $_ := set .Values.workload.main.podSpec.initContainers "init-perms" $initPerms -}}

{{/* Add [wait nextcloud] container to nginx */}}
{{- if not (get .Values.workload.nginx.podSpec "initContainers") -}}
  {{- $_ := set .Values.workload.nginx.podSpec "initContainers" dict -}}
{{- end -}}
{{- $waitNextcloud := (include "nextcloud.wait.nextcloud" . | fromYaml) -}}
{{- $_ := set .Values.workload.nginx.podSpec.initContainers "wait-nextcloud" $waitNextcloud -}}

{{/* Disable [notify push] if requested */}}
{{- if not .Values.nextcloud.notify_push.enabled -}}
  {{- $_ := set .Values.workload.notify "enabled" false -}}
  {{- $_ := set .Values.service.notify "enabled" false -}}
{{- else -}}
  {{/* Add [wait nextcloud] container to notify push */}}
  {{- if not (get .Values.workload.notify.podSpec "initContainers") -}}
    {{- $_ := set .Values.workload.notify.podSpec "initContainers" dict -}}
  {{- end -}}
  {{- $waitNextcloud := (include "nextcloud.wait.nextcloud" . | fromYaml) -}}
  {{- $_ := set .Values.workload.notify.podSpec.initContainers "wait-nextcloud" $waitNextcloud -}}
{{- end -}}

{{/* Disable [clamav] if requested */}}
{{- if not .Values.nextcloud.clamav.enabled -}}
  {{- $_ := set .Values.workload.clamav "enabled" false -}}
  {{- $_ := set .Values.service.clamav "enabled" false -}}
{{- end -}}

{{/* Disable [previews] if requested */}}
{{- if or (not .Values.nextcloud.previews.imaginary) (not .Values.nextcloud.previews.enabled) -}}
  {{- $_ := set .Values.workload.imaginary "enabled" false -}}
  {{- $_ := set .Values.service.imaginary "enabled" false -}}
{{- end -}}

{{/* Disable [collabora] if requested */}}
{{- if not .Values.nextcloud.collabora.enabled -}}
  {{- $_ := set .Values.workload.collabora "enabled" false -}}
  {{- $_ := set .Values.service.collabora "enabled" false -}}
{{- end -}}

{{/* Create [cronjobs] defined */}}
{{- $cronjobs := include "nextcloud.cronjobs" . | fromYaml -}}
{{- if $cronjobs -}}
  {{- $_ := mustMergeOverwrite .Values.workload $cronjobs -}}
{{- end -}}

{{/* TODO: Do we have to cleanup when something (eg Collabora) is disabled? */}}
{{- include "nextcloud.ingressInjector" $ -}}

{{/* Render the templates */}}
{{- include "tc.v1.common.loader.apply" . -}}
