# Install the distro-packaged rTorrent/libtorrent pair; export its glibc runtime.
FROM opensuse/tumbleweed:latest AS runtime
RUN zypper --non-interactive dup --no-recommends \
    && zypper --non-interactive install --no-recommends \
       rtorrent ca-certificates ca-certificates-mozilla timezone \
       gcc glibc-devel libcurl-devel binutils python3 \
    && rpm -q rtorrent libtorrent55 \
    && test "$(rpm -q --qf '%{VERSION}' rtorrent)" = "$(rpm -q --qf '%{VERSION}' libtorrent55)" \
    && test "$(printf '%s\n' 0.16.27 "$(rpm -q --qf '%{VERSION}' rtorrent)" | sort -V | head -n1)" = 0.16.27 \
    && zypper clean --all
COPY install/rtorrent-entrypoint.c /build/rtorrent-entrypoint.c
RUN cc -Os -Wall -Wextra -Werror -o /usr/local/bin/rtorrent-entrypoint /build/rtorrent-entrypoint.c \
    && strip /usr/local/bin/rtorrent-entrypoint
COPY install/rtorrent-public-ip.c /build/rtorrent-public-ip.c
RUN cc -Os -Wall -Wextra -Werror -o /usr/local/bin/rtorrent-public-ip /build/rtorrent-public-ip.c -lcurl \
    && strip /usr/local/bin/rtorrent-public-ip
COPY build/runtime-root.py /build/runtime-root.py
RUN python3 /build/runtime-root.py /runtime /usr/bin/rtorrent /usr/local/bin/rtorrent-entrypoint /usr/local/bin/rtorrent-public-ip
COPY tests/rtorrent-smoke.c /build/rtorrent-smoke.c
RUN cc -Os -Wall -Wextra -Werror -o /build/rtorrent-smoke /build/rtorrent-smoke.c

FROM scratch
COPY --from=runtime /runtime/ /
ENV HOME=/root PATH=/usr/local/bin:/usr/bin:/bin TERM=xterm
RUN --mount=type=bind,from=runtime,source=/build/rtorrent-smoke,target=/ci-smoke ["/ci-smoke"]
VOLUME ["/home/nfs_download"]
EXPOSE 5001/tcp
HEALTHCHECK --interval=10s --timeout=10s --start-period=90s --retries=5 \
    CMD ["/usr/local/bin/rtorrent-entrypoint", "--healthcheck"]
ENTRYPOINT ["/usr/local/bin/rtorrent-entrypoint"]
