# https://github.com/hassio-addons/addon-debian-base/releases
# hadolint ignore=DL3006
ARG BUILD_FROM
FROM ${BUILD_FROM}

# https://github.com/mikefarah/yq/releases
ARG YQ_VERSION=4.44.3
ARG PROMTAIL_VERSION=3.2.1

ARG BUILD_ARCH

COPY rootfs /

# Add yq and tzdata (required for the timestamp stage)
RUN set -eux; \
    apk update; \
    \
    apk add --no-cache --virtual .build-deps \
        tar;
RUN apk add --no-cache \
        ca-certificates;
RUN update-ca-certificates;
RUN case "${BUILD_ARCH}" in \
        amd64)  BINARCH='amd64' ;; \
        armhf)   BINARCH='arm' ;; \
        armv7)   BINARCH='arm' ;; \
        aarch64) BINARCH='arm64' ;; \
        *) echo >&2 "error: unsupported architecture (${APKARCH})"; exit 1 ;; \
    esac; \
    curl -s -J -L -o /tmp/yq.tar.gz \
        "https://github.com/mikefarah/yq/releases/download/v${YQ_VERSION}/yq_linux_${BINARCH}.tar.gz"; \
    tar -xf /tmp/yq.tar.gz -C /usr/bin; \
    mv /usr/bin/yq_linux_${BINARCH} /usr/bin/yq; \
    chmod a+x /usr/bin/yq; \
    rm /tmp/yq.tar.gz; \
    yq --version; \
    \
    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*; \
    chmod +x /etc/cont-init.d/promtail_setup.sh; \
    chmod +x /etc/services.d/promtail/*;

# Install necessary packages
RUN apk add --no-cache curl tar unzip && \
    # Remove nsswitch.conf to avoid conflicts with glibc
    rm -f /etc/nsswitch.conf && \
    # Add the public key for sgerrand glibc packages
    curl -Lo /etc/apk/keys/sgerrand.rsa.pub https://alpine-pkgs.sgerrand.com/sgerrand.rsa.pub && \
    # Download and install glibc
    curl -Lo glibc.apk https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.35-r0/glibc-2.35-r0.apk && \
    apk add glibc.apk && \
    rm glibc.apk

# Download and install Promtail
RUN curl -s -J -L -o promtail-linux-amd64.zip \
        "https://github.com/grafana/loki/releases/download/v${PROMTAIL_VERSION}/promtail-linux-amd64.zip" && \
    unzip promtail-linux-amd64.zip -d /usr/bin && \
    mv /usr/bin/promtail-linux-amd64 /usr/bin/promtail && \
    chmod a+x /usr/bin/promtail && \
    rm promtail-linux-amd64.zip

# Verify Promtail installation
RUN /usr/bin/promtail -version
    
WORKDIR /data/promtail

# Build arguments
ARG BUILD_DATE
ARG BUILD_DESCRIPTION
ARG BUILD_NAME
ARG BUILD_REF
ARG BUILD_REPOSITORY
ARG BUILD_VERSION

# Labels
LABEL \
    io.hass.name="${BUILD_NAME}" \
    io.hass.description="${BUILD_DESCRIPTION}" \
    io.hass.arch="${BUILD_ARCH}" \
    io.hass.type="addon" \
    io.hass.version=${BUILD_VERSION} \
    maintainer="Feres" \
    org.opencontainers.image.title="${BUILD_NAME}" \
    org.opencontainers.image.description="${BUILD_DESCRIPTION}" \
    org.opencontainers.image.vendor="Ferfes's Home Assistant Add-ons" \
    org.opencontainers.image.authors="Feres" \
    org.opencontainers.image.licenses="MIT" \
    org.opencontainers.image.url="https://github.com/mdegat01/hassio-addons" \
    org.opencontainers.image.source="https://github.com/${BUILD_REPOSITORY}" \
    org.opencontainers.image.documentation="https://github.com/${BUILD_REPOSITORY}/blob/main/README.md" \
    org.opencontainers.image.created=${BUILD_DATE} \
    org.opencontainers.image.revision=${BUILD_REF} \
    org.opencontainers.image.version=${BUILD_VERSION}