---
apiVersion: v1
metadata:
  annotations:
    meta.helm.sh/release-name: mariadb
    meta.helm.sh/release-namespace: backend
  name: configmap-mariadb-init
  namespace: backend
kind: ConfigMap
data:
  init_db.sh: |
    #!/bin/bash
    # check if socket is initialized (only happens on first boot)
    # ref: https://github.com/bitnami/charts/issues/1395#issuecomment-578066552
    if [[ -S /opt/bitnami/mariadb/tmp/mysql.sock ]]; then
      echo "Running DB initialization..."
    else
      echo "Not first boot, skipping initialization. Exiting..."
      exit 0
    fi

    export ROOT_PWD="${SECRET_DB_ROOT_PWD}"
    export USER_PWD="${SECRET_DB_USER_PWD}"
    # echo "ROOT_PWD=$ROOT_PWD"
    # echo "USER_PWD=$USER_PWD"

    function initdb {
      echo "Creating \"$1\" user and database"

      # note: if this presents problems in the future, look into heredoc indentations
      /opt/bitnami/mariadb/bin/mysql --port=3306 --user="root" --password="$ROOT_PWD" -v <<EOSQL
    CREATE USER '$1'@'%' IDENTIFIED BY '$USER_PWD';
    GRANT USAGE ON *.* TO '$1'@'%' REQUIRE NONE WITH MAX_QUERIES_PER_HOUR 0 MAX_CONNECTIONS_PER_HOUR 0 MAX_UPDATES_PER_HOUR 0 MAX_USER_CONNECTIONS 0;
    CREATE DATABASE IF NOT EXISTS \`$1\`;
    GRANT ALL PRIVILEGES ON \`$1\`.* TO '$1'@'%';
    FLUSH PRIVILEGES;
    EOSQL
    }

    # initialize users + databases
    initdb "vaultwarden"
