---
apiVersion: v1
metadata:
  annotations:
    meta.helm.sh/release-name: postgresql
    meta.helm.sh/release-namespace: postgresql
  name: configmap-postgresql-init
  namespace: postgresql
kind: ConfigMap
data:
  00-init-hba.sh: |
    #!/bin/bash

    # import functions
    # ref: https://github.com/bitnami/bitnami-docker-postgresql/blob/master/12/debian-10/rootfs/opt/bitnami/scripts/libpostgresql.sh#L468
    . /opt/bitnami/scripts/libpostgresql.sh
    # . /opt/bitnami/scripts/postgresql-env.sh

    # update pg_hba.conf
    info "Updating $POSTGRESQL_PGHBA_FILE with postgres replication permissions"
    cat <<EOF >>"$POSTGRESQL_PGHBA_FILE"
    local     replication     postgres                                md5
    host      replication     postgres        0.0.0.0/0               md5
    host      replication     postgres        ::/0                    md5
    host      replication     postgres        127.0.0.1/32            md5
    host      replication     postgres        ::1/128                 md5
    EOF

    # if [[ -n "$POSTGRES_REPLICATION_USER" ]]; then
    #   info "Updating $POSTGRESQL_PGHBA_FILE with $POSTGRES_REPLICATION_USER replication permissions"
    #   postgresql_create_replication_user
    #   cat <<EOF >>"$POSTGRESQL_PGHBA_FILE"
    # local     replication     $POSTGRES_REPLICATION_USER                               md5
    # host      replication     $POSTGRES_REPLICATION_USER       0.0.0.0/0               md5
    # host      replication     $POSTGRES_REPLICATION_USER       ::/0                    md5
    # host      replication     $POSTGRES_REPLICATION_USER       127.0.0.1/32            md5
    # host      replication     $POSTGRES_REPLICATION_USER       ::1/128                 md5
    # EOF
    # fi

    postgresql_restrict_pghba

    debug "\n$(cat $POSTGRESQL_PGHBA_FILE)"

    info "Reloading configuration"
    echo "SELECT pg_reload_conf();" | postgresql_execute "" "postgres" "$POSTGRESQL_PASSWORD"

  # 02-init-conf.sh: |
  #   # update postgresql.conf
  #   postgresql_set_property "archive_mode" "on"
  #   postgresql_set_property "archive_command" 'test ! -f /mnt/backup/incremental.d/%f && mkdir -p /mnt/backup/incremental.d/ && cp %p /mnt/backup/incremental.d/%f'
  #   postgresql_set_property "archive_timeout" "900s"

  #   info "Reloading configuration"
  #   echo "SELECT pg_reload_conf();" | postgresql_execute "" "postgres" "$POSTGRESQL_PASSWORD"

  10-init_db.sh: |
    #!/bin/bash

    # import functions
    # ref: https://github.com/bitnami/bitnami-docker-postgresql/blob/master/12/debian-10/rootfs/opt/bitnami/scripts/libpostgresql.sh#L468
    . /opt/bitnami/scripts/libpostgresql.sh
    # . /opt/bitnami/scripts/postgresql-env.sh

    function initdb {
      local -r NAME="$1"
      local -r PWD="$2"

      info "Creating \"$1\" user and database"

      psql "postgresql://postgres:$POSTGRES_PASSWORD@localhost:5432" -e <<EOSQL
    CREATE USER $NAME WITH LOGIN PASSWORD '$PWD';
    CREATE DATABASE "$NAME";
    GRANT ALL PRIVILEGES ON DATABASE "$NAME" TO "$NAME";
    EOSQL
    }

    initdb "vaultwarden" "${SECRET_DB_USER_PWD}"
    initdb "authentik" "${SECRET_DB_USER_PWD}"
    initdb "keycloak" "${SECRET_DB_USER_PWD}"
    initdb "mealie" "${SECRET_DB_USER_PWD}"
    initdb "nextcloud" "${SECRET_DB_USER_PWD}"

  99-backup.sh: |
    #!/bin/bash

    # import functions
    # ref: https://github.com/bitnami/bitnami-docker-postgresql/blob/master/12/debian-10/rootfs/opt/bitnami/scripts/libpostgresql.sh#L468
    . /opt/bitnami/scripts/libpostgresql.sh
    # . /opt/bitnami/scripts/postgresql-env.sh

    info 'Making .pgpass file...'
    cat > /bitnami/postgresql/.pgpass << EOF
    # hostname:port:database:username:password
    *:*:replication:postgres:$POSTGRESQL_PASSWORD
    EOF
    chmod 0600 /bitnami/postgresql/.pgpass

    info 'Making backup directories if they do not exist...'
    BACKUPDIR="/mnt/backup/backup.d/backup-$(date +'%Y-%m-%d')"
    mkdir -p $BACKUPDIR

    info 'Checking backup directory...'
    if [ -z "$(ls -A $BACKUPDIR)" ]; then
      info "Base backup location $BACKUPDIR is empty, running full backup..."

      debug "\n$(cat $POSTGRESQL_PGHBA_FILE)"

      PGPASSFILE=/bitnami/postgresql/.pgpass pg_basebackup -U postgres --no-password -D $BACKUPDIR
      info "Base backup successful"
    fi

    info 'Removing .pgpass file...'
    rm -f /bitnami/postgresql/.pgpass
