{{- /*
Copyright VMware, Inc.
SPDX-License-Identifier: APACHE-2.0
*/}}

{{- if or .Values.customPostInitScripts .Values.wordpressConfigureCache }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ printf "%s-postinit" (include "common.names.fullname" .) | trunc 63 | trimSuffix "-" }}
  namespace: {{ .Release.Namespace | quote }}
  labels: {{- include "common.labels.standard" ( dict "customLabels" .Values.commonLabels "context" $ ) | nindent 4 }}
  {{- if .Values.commonAnnotations }}
  annotations: {{- include "common.tplvalues.render" ( dict "value" .Values.commonAnnotations "context" $ ) | nindent 4 }}
  {{- end }}
data:
  {{- if .Values.wordpressConfigureCache }}
  {{- $memcachedFullname := include "wordpress.cacheHost" . }}
  {{- $memcachedPort := include "wordpress.cachePort" . | int }}
  00-configure-w3-total-cache.sh: |-
    #!/bin/bash

    # Add permissions to edit wp-config.php
    chmod +w /bitnami/wordpress/wp-config.php

    # Activate W3 Total Cache pairs
    wp plugin activate w3-total-cache
    wp total-cache fix_environment

    # Choose 'Memcached' as database and object cache method
    wp total-cache option set dbcache.engine memcached --type=string
    wp total-cache option set objectcache.engine memcached --type=string
    wp total-cache flush all
    wp total-cache option set dbcache.memcached.servers {{ $memcachedFullname }}:{{ $memcachedPort }} --type=string
    wp total-cache option set dbcache.enabled true --type=boolean
    wp total-cache option set objectcache.memcached.servers {{ $memcachedFullname }}:{{ $memcachedPort }} --type=string
    wp total-cache option set objectcache.enabled true --type=boolean
    wp total-cache flush all

    # Revoke permissions to edit wp-config.php
    chmod a-w /bitnami/wordpress/wp-config.php
  {{- end }}
  {{- if .Values.customPostInitScripts }}
  {{- include "common.tplvalues.render" (dict "value" .Values.customPostInitScripts "context" $) | nindent 2 }}
  {{- end }}
{{- end }}
