# SPDX-License-Identifier: MIT
#!BuildTag: suse/multi-linux-manager/5.1/%ARCH%/server-postgresql:5.1.2 suse/multi-linux-manager/5.1/%ARCH%/server-postgresql:5.1.2.%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 postgresql16-contrib && \
    mkdir -p /opt/contrib /opt/results && \
    find /var/cache/zypp/packages -name postgresql16-contrib* -exec cp {} /opt/contrib \; && \
    cd /opt/contrib && rpm2cpio postgresql16-contrib.rpm | cpio -idmv && \
    find . -name pg_trgm* -exec cp --parents {} ../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.2"

# 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.9
LABEL org.openbuildservice.disturl="%DISTURL%"
LABEL org.opensuse.reference="${REFERENCE_PREFIX}/%ARCH%/server-postgresql:5.1.2.%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"]
