{{- if .Values.initPassword.enabled }}
{{- if not .Values.initPassword.passwordSecret }}
apiVersion: v1
kind: Secret
metadata:
  name: {{ template "starrockscluster.initpassword.secret.name" . }}
  namespace: {{ template "starrockscluster.namespace" . }}
data:
  password: {{ .Values.initPassword.password | b64enc }}
---
{{- end }}
apiVersion: v1
kind: ConfigMap
metadata:
  name: {{ template "starrockscluster.name" . }}-initpwd-shell
  namespace: {{ template "starrockscluster.namespace" . }}
data:
  fe_initpwd.sh: |-
    #!/bin/bash

    # ensure `mysql` command can be ended with 30 seconds
    # Change the root password during initial installation
    init_pwd_sql="SET PASSWORD = PASSWORD('$INIT_PWD');"
    success=`mysql --connect-timeout 30 -h $1 -P $2 -u root --skip-column-names --batch -e "$init_pwd_sql" 2>&1`
    if [ $? -ne 0 ] ; then
      echo $success
      errcode=`echo $success | awk -F " " '{print $2}'`
      echo "error code: $errcode"

      # Password error, believed to have been changed, exiting normally
      if [[ $errcode = '1045' || $errcode = '1064' ]] ; then
        echo "Password error, believed to have been changed, exiting normally"
        exit 0
      fi

      # Other unsuccessful errors, abnormal exit
      echo "Other unsuccessful errors, abnormal exit"
      exit 1

    fi

    echo "Successfully modified password"
    exit 0
{{- end }}
