# SPDX-License-Identifier: Apache-2.0
#!BuildTag: rancher/elemental-channel:latest
#!BuildTag: rancher/elemental-channel:%OPERATOR_VERSION%
#!BuildTag: rancher/elemental-channel:%OPERATOR_VERSION%-%RELEASE%
ARG SLEMICRO_VERSION
ARG SLE_VERSION

FROM suse/sle-micro/$SLEMICRO_VERSION:latest AS os

ADD channel.json /channel.json

# This is just to pull elemental-register package in OBS build env
# and use it in replace_using_package_version service to actually tag
# the current image with it
RUN zypper in -y -- elemental-register1.5 jq

# Align the channel with the latest OS version in the project
RUN source /etc/os-release && \
    IMG_TAG=${IMAGE_TAG%%-*} && \
    sed -i -e "s|%IMG_TAG%|${IMG_TAG}|g" /channel.json

# Mark latest OS versions as unstable if the build happens in OBS
RUN source /etc/os-release && \
    if [[ ${IMAGE_REPO} == *"registry.opensuse.org"* ]]; then \
      TMPFILE=$(mktemp) && \
      jq '.[0].spec.metadata.displayName += " (unstable)"' /channel.json > ${TMPFILE} && \
      jq '.[1].spec.metadata.displayName += " (unstable)"' ${TMPFILE} > /channel.json && \
      jq '.[0].metadata.name += "-unstable"' /channel.json > ${TMPFILE} && \
      jq '.[1].metadata.name += "-unstable"' ${TMPFILE} > /channel.json ; \
    fi

ARG SLE_VERSION
FROM bci/bci-busybox:$SLE_VERSION

ADD channel.json /channel.json
COPY --from=os /channel.json /channel.json

ARG BUILD_REPO=%%IMG_REPO%%
ARG IMAGE_REPO=$BUILD_REPO/rancher/elemental-channel

# Define labels according to https://en.opensuse.org/Building_derived_containers
# labelprefix=com.rancher.elemental
LABEL org.opencontainers.image.title="Rancher Elemental Channel"
LABEL org.opencontainers.image.description="Includes the Elemental Channel"
LABEL org.opencontainers.image.version="%OPERATOR_VERSION%"
LABEL org.opencontainers.image.url="https://build.opensuse.org"
LABEL org.opencontainers.image.created="%BUILDTIME%"
LABEL org.opencontainers.image.vendor="SUSE LLC"
LABEL org.opensuse.reference=$IMAGE_REPO
LABEL org.openbuildservice.disturl="%DISTURL%"
LABEL com.suse.supportlevel="techpreview"
# endlabelprefix

USER 10010:10010

ENTRYPOINT ["busybox", "cp"]
CMD ["/channel.json", "/data/output"]
