{{- /*
Copyright Broadcom, Inc. All Rights Reserved.
SPDX-License-Identifier: APACHE-2.0
*/}}

apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ printf "%s-replicaset-entrypoint" (include "common.names.fullname" .) }}
  namespace: {{ include "common.names.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:
  replicaset-entrypoint.sh: |-
    #!/bin/bash

    sleep 5

    . /liblog.sh

    # Perform adaptations depending on the host name
    if [[ $HOSTNAME =~ (.*)-0$ ]]; then
      info "Setting node as primary"
      export MONGODB_REPLICA_SET_MODE=primary
    else
      info "Setting node as secondary"
      export MONGODB_REPLICA_SET_MODE=secondary
      {{- if .Values.auth.usePasswordFiles }}
      export MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD_FILE="$MONGODB_ROOT_PASSWORD_FILE"
      unset MONGODB_ROOT_PASSWORD_FILE
      {{- else }}
      export MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD="$MONGODB_ROOT_PASSWORD"
      unset MONGODB_ROOT_PASSWORD
      {{- end }}
    fi

    exec /entrypoint.sh /run.sh
