# SPDX-License-Identifier: MIT
#!BuildTag: suse/multi-linux-manager/5.1/%ARCH%/server-postgresql:5.1.3 suse/multi-linux-manager/5.1/%ARCH%/server-postgresql:5.1.3.%RELEASE% suse/multi-linux-manager/5.1/%ARCH%/server-postgresql:latest

ARG BASE=registry.suse.com/bci/bci-base:15.6
ARG PG_BASE=registry.opensuse.org/opensuse/postgres:16

FROM $BASE AS builder
RUN zypper --non-interactive install --download-only --force --no-recommends postgresql16-contrib tar gzip && \
    mkdir -p /opt/contrib /opt/results && \
    find /var/cache/zypp/packages \( -name postgresql16-contrib* -o -name tar* -o -name gzip* \) -exec cp {} /opt/contrib \; && \
    cd /opt/contrib && rpm2cpio postgresql16-contrib.rpm | cpio -idmv && rpm2cpio tar*.rpm | cpio -idmv && rpm2cpio gzip*.rpm | cpio -idmv && \
    find . -name pg_trgm* -exec cp --parents {} /opt/results/ \; && \
    cp --parents usr/bin/tar usr/bin/gzip /opt/results/

FROM $PG_BASE
COPY --from=builder /opt/results/ /
ADD --chown=root:root root.tar.gz /

# LABELs
ARG PRODUCT="SUSE Multi-Linux Manager"
ARG VENDOR="SUSE LLC"
ARG URL="https://www.uyuni-project.org/"
ARG REFERENCE_PREFIX="registry.opensuse.org/uyuni"
ARG PRODUCT_VERSION="5.1.3"

# Build Service required labels
# labelprefix=com.suse.multilinuxmanager.server-postgresql
LABEL com.suse.supportlevel="l3"
LABEL com.suse.lifecycle-url="https://www.suse.com/lifecycle/"
LABEL com.suse.release-stage="released"
LABEL com.suse.eula="sle-eula"
LABEL org.opencontainers.image.name=server-postgresql-image
LABEL org.opencontainers.image.title="${PRODUCT} PostgreSQL container image"
LABEL org.opencontainers.image.description="${PRODUCT} PostgreSQL container image"
LABEL org.opencontainers.image.created="%BUILDTIME%"
LABEL org.opencontainers.image.vendor="${VENDOR}"
LABEL org.opencontainers.image.url="${URL}"
LABEL org.opencontainers.image.version=5.1.11
LABEL org.openbuildservice.disturl="%DISTURL%"
LABEL org.opensuse.reference="${REFERENCE_PREFIX}/%ARCH%/server-postgresql:5.1.3.%RELEASE%"
# endlabelprefix
LABEL org.uyuni.version="${PRODUCT_VERSION}"

# Hard code the postgres UID / GID to the upstream image value and what was in the big container
# The files are chown'ed in the entrypoint
RUN /usr/sbin/groupmod -g 999 postgres && \
    /usr/sbin/usermod -u 999 -g 999 postgres

ENTRYPOINT ["/usr/local/bin/docker-entrypoint.sh"]
CMD ["postgres"]
