# syntax=docker/dockerfile:1

ARG BUILD_FROM
FROM $BUILD_FROM

ARG BUILD_DATE
ARG BUILD_REF
ARG BUILD_VERSION
ARG BUILD_ARCH

LABEL \
    io.hass.name="Folding@home" \
    io.hass.description="Run Folding@home to contribute to scientific research" \
    io.hass.arch="${BUILD_ARCH}" \
    io.hass.type="addon" \
    io.hass.version=${BUILD_VERSION} \
    maintainer="retr0"

# Add needed NVIDIA environment variables for container toolkit
ENV NVIDIA_DRIVER_CAPABILITIES="compute,video,utility"

# Prevent interactive prompts during package installation
ENV DEBIAN_FRONTEND="noninteractive"

RUN \
    echo "**** install runtime packages ****" && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        bzip2 \
        curl \
        jq \
        libexpat1 \
        mosquitto-clients \
        netcat-openbsd && \
    echo "**** install foldingathome ****" && \
    if [ "${BUILD_ARCH}" = "aarch64" ]; then \
        FAH_WORKER="debian-stable-arm64"; \
    else \
        FAH_WORKER="debian-10-64bit"; \
    fi && \
    DOWNLOAD_PATH=$(curl -fsSL "https://download.foldingathome.org/releases/public/fah-client/meta.json" | \
        jq -r --arg worker "${FAH_WORKER}" \
        '.[] | select(.worker == $worker and .mode == "release" and (.package | endswith(".tar.bz2"))) | .package' \
        | head -1) && \
    echo "Downloading FAH client: ${DOWNLOAD_PATH}" && \
    curl -fsSL "https://download.foldingathome.org/releases/public/fah-client/${DOWNLOAD_PATH}" \
        -o /tmp/fah.tar.bz2 && \
    mkdir -p /app && \
    tar xf /tmp/fah.tar.bz2 -C /app --strip-components=1 && \
    echo "**** cleanup ****" && \
    apt-get clean && \
    rm -rf \
        /tmp/* \
        /var/lib/apt/lists/* \
        /var/tmp/* \
        /var/log/*

# Add local files
COPY root/ /

EXPOSE 7396
