# dockerfiles/Dockerfile.monero-p2pool
# syntax=docker/dockerfile:1.6
ARG DEBIAN_IMAGE=debian:bookworm-slim
ARG P2POOL_VERSION=v4.9

FROM ${DEBIAN_IMAGE} AS fetch
ARG TARGETARCH
ARG P2POOL_VERSION
RUN set -eux; \
    apt-get update; \
    apt-get install -y --no-install-recommends ca-certificates curl jq xz-utils tar; \
    update-ca-certificates; rm -rf /var/lib/apt/lists/*
RUN set -eux; \
    case "${TARGETARCH:-arm64}" in \
      amd64) ARCH_RE='(x86_64|amd64)';; \
      arm64) ARCH_RE='(arm64|aarch64|armv8)';; \
      arm)   ARCH_RE='(arm|armv7|armhf)';; \
      *) echo "Unsupported TARGETARCH=${TARGETARCH}"; exit 1;; \
    esac; \
    API="https://api.github.com/repos/SChernykh/p2pool/releases/tags/${P2POOL_VERSION}"; \
    URL="$(curl -fsSL "${API}" | jq -r --arg re "${ARCH_RE}" '.assets[].browser_download_url | select(test("linux") and test($re))' | head -n1)"; \
    test -n "${URL}"; echo "Downloading ${URL}"; \
    curl -fsSL "${URL}" -o /tmp/p2pool.tgz; \
    mkdir -p /out; tar -xzf /tmp/p2pool.tgz -C /out; \
    BIN="$(find /out -maxdepth 1 -type f -name 'p2pool*' | head -n1)"; \
    test -n "${BIN}"; install -m0755 "${BIN}" /out/p2pool

FROM ${DEBIAN_IMAGE}
RUN set -eux; \
    apt-get update; \
    apt-get install -y --no-install-recommends ca-certificates; \
    update-ca-certificates; rm -rf /var/lib/apt/lists/*; \
    groupadd --system p2pool; \
    useradd --system --uid 65532 --gid p2pool --home-dir /nonexistent --shell /usr/sbin/nologin p2pool
COPY --from=fetch /out/p2pool /usr/local/bin/p2pool

RUN /usr/local/bin/p2pool --version || true
EXPOSE 3333
USER p2pool
ENTRYPOINT ["/usr/local/bin/p2pool"]
