{{/* Import any application manifests stored in the apps directory */}}
{{ $file_manifests := .Files.Glob "apps/*.yaml"}}
{{- $apps := .Values.applications}}
{{- range $path, $_ := $file_manifests}}
  {{- $file := ($.Files.Get $path | fromYaml)}}
  {{- if hasKey $file "name"}}
    {{- $file = set $file "appName" (get $file "name")}}
  {{- end}}
  {{- $base := base $path}}
  {{- $file =  set $file "name" ( regexReplaceAll "\\..+$" $base "" ) }}
  {{- $apps = append $apps $file}}
{{- end}}
{{- range $apps }}
{{- if .applicationSet}}
apiVersion: argoproj.io/v1alpha1
kind: ApplicationSet
metadata:
  name: {{.name}}
spec:
  generators: {{- nindent 2 (toYaml (default $.Values.generators .generators)) }}
  strategy:
    type: RollingSync
    rollingSync:
      steps:
        - matchExpressions:
          - key: env
            operator: In
            values:
              - staging
          maxUpdate: 25%
        - matchExpressions:
          - key: env
            operator: In
            values:
              - production
          maxUpdate: 10%
  template:
    metadata:
      name: "{{.appName}}"
      finalizers:
        - resources-finalizer.argocd.argoproj.io
    spec:
      {{- /* Start by applying any defaults defined in the Values file */}}
      {{- $app_with_defaults := mergeOverwrite (deepCopy $.Values.appDefaults) . }}
      {{- /* If there are any source refrences in the defined sources, apply any matching refs in .Values.sourceRefs */ -}}
      {{- $app_with_defaults = (include "source-refs" (dict "app" $app_with_defaults "context" $)) | fromYaml}}
      {{- /* Validate the final application manifest, ensuring all required values are present */}}
      {{- include "validation.app" (dict "app" $app_with_defaults)}}
      {{- /* Final step: simply template the application manifest*/}}
{{toYaml (omit $app_with_defaults "name" "applicationSet" "generators" "appName") | indent 6}}
{{- else }}
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
  name: {{.name}}
  labels:
    tenant: {{.name}}
  finalizers:
    - resources-finalizer.argocd.argoproj.io
spec:
  {{- /* Start by applying any defaults defined in the Values file */}}
  {{- $app_with_defaults := mergeOverwrite (deepCopy $.Values.appDefaults) . }}
  {{- /* If there are any template strings in the values file for this app, retemplate them */}}
  {{- $app_with_defaults = (include "retemplate" (dict "value" $app_with_defaults "context" $)) | fromYaml}}
  {{- /* If there are any source refrences in the defined sources, apply any matching refs in .Values.sourceRefs */ -}}
  {{- $app_with_defaults = (include "source-refs" (dict "app" $app_with_defaults "context" $)) | fromYaml}}
  {{- /* We retemplate again in case additional source refs containing templates were added */}}
  {{- $app_with_defaults = (include "retemplate" (dict "value" $app_with_defaults "context" $)) | fromYaml}}
  {{- /* Validate the final application manifest, ensuring all required values are present */}}
  {{- include "validation.app" (dict "app" $app_with_defaults)}}
  {{- /* Final step: simply template the application manifest*/}}
{{toYaml (omit $app_with_defaults "name") | indent 2}}
{{- end}}

---
{{- end}}
