ARG UPSTREAM_IMAGE_VERSION
ARG DEPS_IMAGE
FROM docker.io/library/postgres:${UPSTREAM_IMAGE_VERSION}-bookworm AS postgresql-upstream
FROM ${DEPS_IMAGE} AS deps

## ---

FROM deps AS postgresql
SHELL [ "/bin/sh", "-ec" ]

COPY /scripts/*  /usr/local/sbin/

ENV PG_UID=11111 PG_GID=11111
ENV PGHOME=/var/lib/postgresql
ENV PGDATA=${PGHOME}/data

RUN echo "postgres:x:${PG_UID}:${PG_GID}:postgres:${PGHOME}:/bin/bash" >> /etc/passwd ; \
    echo "postgres:x:${PG_GID}:" >> /etc/group ; \
    echo 'postgres:!:::::::' >> /etc/shadow ; \
    install -d -o postgres -g postgres -m 0755 "${PGHOME}"

RUN install -d -o postgres -g postgres -m 3755 /run/postgresql
VOLUME [ "/run/postgresql" ]

COPY /apt/sources.pgdg-ver.in      /tmp/pgdg-ver.sources
COPY /apt/preferences.pgdg-ver.in  /tmp/pgdg-ver.prefs
RUN sed "s/%{PG_MAJOR}/${PG_MAJOR}/g" < /tmp/pgdg-ver.sources > "/etc/apt/sources.list.d/pgdg-${PG_MAJOR}.sources" ; \
    sed "s/%{PG_MAJOR}/${PG_MAJOR}/g" < /tmp/pgdg-ver.prefs   > "/etc/apt/preferences.d/pgdg-${PG_MAJOR}" ; \
    rm -f /tmp/pgdg-ver.sources /tmp/pgdg-ver.prefs

COPY --from=postgresql-upstream  /etc/postgresql-common/createcluster.conf  /etc/postgresql-common/
RUN apt-install.sh postgresql-common ; \
    apt-install.sh \
      "postgresql-${PG_MAJOR}" \
    ; \
    apt-clean.sh ; \
    f="/usr/share/postgresql/${PG_MAJOR}/postgresql.conf.sample" ; \
    dpkg-divert --add --rename --divert "$f.dpkg" "$f" ; \
    ln -sv ../postgresql.conf.sample "/usr/share/postgresql/${PG_MAJOR}/"
COPY --from=postgresql-upstream  /usr/share/postgresql/postgresql.conf.sample  /usr/share/postgresql/

ENV PATH=${PATH}:/usr/lib/postgresql/${PG_MAJOR}/bin

COPY --from=postgresql-upstream  /docker-entrypoint-initdb.d/  /docker-entrypoint-initdb.d/
COPY --from=postgresql-upstream  /usr/local/bin/*.sh           /usr/local/bin/
## compatibility ;)
RUN ln -sv /usr/local/sbin/dumb-run-as.sh /usr/local/bin/gosu

## ---

FROM postgresql AS postgresql-extras
SHELL [ "/bin/sh", "-ec" ]

RUN apt-install.sh \
      pgbouncer \
      "postgresql-${PG_MAJOR}-cron" \
      "postgresql-${PG_MAJOR}-hll" \
      "postgresql-${PG_MAJOR}-hypopg" \
      "postgresql-${PG_MAJOR}-icu-ext" \
      "postgresql-${PG_MAJOR}-pg-catcheck" \
      "postgresql-${PG_MAJOR}-pg-checksums" \
      "postgresql-${PG_MAJOR}-pg-failover-slots" \
      "postgresql-${PG_MAJOR}-pg-hint-plan" \
      "postgresql-${PG_MAJOR}-pg-qualstats" \
      "postgresql-${PG_MAJOR}-pg-stat-kcache" \
      "postgresql-${PG_MAJOR}-pgextwlist" \
      "postgresql-${PG_MAJOR}-pgfaceting" \
      "postgresql-${PG_MAJOR}-pgfincore" \
      "postgresql-${PG_MAJOR}-pglogical" \
      "postgresql-${PG_MAJOR}-pglogical-ticker" \
      "postgresql-${PG_MAJOR}-pgpcre" \
      "postgresql-${PG_MAJOR}-powa" \
      "postgresql-${PG_MAJOR}-repack" \
      "postgresql-${PG_MAJOR}-roaringbitmap" \
      "postgresql-${PG_MAJOR}-rum" \
      "postgresql-${PG_MAJOR}-semver" \
      "postgresql-${PG_MAJOR}-show-plans" \
      "postgresql-${PG_MAJOR}-similarity" \
      "postgresql-${PG_MAJOR}-squeeze" \
      "postgresql-${PG_MAJOR}-tablelog" \
      "postgresql-${PG_MAJOR}-tdigest" \
      "postgresql-${PG_MAJOR}-timescaledb" \
      "postgresql-${PG_MAJOR}-toastinfo" \
      "postgresql-${PG_MAJOR}-unit" \
      "postgresql-${PG_MAJOR}-wal2json" \
    ; \
    apt-clean.sh ; \
    jdupes -1LSpr /usr/

## ---

FROM postgresql-extras AS citus
SHELL [ "/bin/sh", "-ec" ]

COPY /apt/sources.citus  /etc/apt/sources.list.d/citus.sources

RUN apt-env.sh apt-get update ; \
    citus_pkg=$(apt-cache search "^postgresql-${PG_MAJOR}-citus-[0-9.]+\$" | awk '{print $1}' | sort -rV | head -n1) ; \
    : "${citus_pkg:?}" ; \
    apt-install.sh \
      "${citus_pkg}" \
      "postgresql-${PG_MAJOR}-topn" \
    ; apt-clean.sh ; \
    jdupes -1LSpr /usr/

VOLUME [ "${PGHOME}" ]

## ---

FROM citus
SHELL [ "/bin/sh", "-ec" ]

COPY /Dockerfile  /usr/local/share/

COPY /ep.sh             /usr/local/sbin/
COPY /postgres-shim.sh  /usr/local/sbin/
## quirk
RUN ln -sv postgres-shim.sh /usr/local/sbin/postgres

WORKDIR "${PGHOME}"

## "Fast Shutdown mode" in PostgreSQL
## NB: override to SIGTERM in order to switch to "Smart Shutdown mode"
STOPSIGNAL SIGINT

ENV DUMB_INIT_SETSID=0 \
    MALLOC_ARENA_MAX=4 \
    GOMAXPROCS=4

ENTRYPOINT [ "ep.sh" ]
CMD [ "postgres" ]