apiVersion: v1
kind: ConfigMap
metadata:
  name: noobaa-postgres-initdb-sh
  labels:
    app: noobaa
data:
  initdb.sh: |
          # If the config file is present, the DB is initiazed
          # and we're out of here
          export PGDATA=$HOME/data/userdata
          if [ -f $PGDATA/postgresql.conf ]; then
            echo postgresql.conf file is found
            exit 0
          fi

          # Wrap the postgres binary, force huge_pages=off for initdb
          # see https://bugzilla.redhat.com/show_bug.cgi?id=1946792
          p=/opt/rh/rh-postgresql12/root/usr/bin/postgres

          # Latest RH images moved the postgres binary
          # from /opt/rh/rh-postgresql12/root/usr/bin/postgres to /usr/bin/postgres
          # see https://bugzilla.redhat.com/show_bug.cgi?id=2051249
          if [ ! -x $p ]; then
            p=/usr/bin/postgres
          fi

          mv $p $p.orig
          echo exec $p.orig \"\$@\" -c huge_pages=off > $p
          chmod 755 $p

          # The NooBaa DB runs with UID 10001 GID 0
          sed -i -e 's/^\(postgres:[^:]\):[0-9]*:[0-9]*:/\1:10001:0:/' /etc/passwd

          # Init the DB and exit once the DB is ready to run
          sed -i -e 's/^exec.*$/exit 0/' \
                 -e 's/^pg_ctl\sstart.*/pg_ctl start || true/'                                   \
                    /usr/bin/run-postgresql
          su postgres -c "bash -x /usr/bin/run-postgresql"
