ARG BASE_IMAGE=docker.snppla.net/snppla/openvpn:1.0

# Build the WebAPI plugin against the same Python 3 the runtime image uses
FROM ${BASE_IMAGE} AS webapi-build

ARG DEBIAN_FRONTEND=noninteractive
# Current master; v0.2.1 predates the Deluge 2 fixes
ARG WEBAPI_REF=114c39324c1297168ffafd0ee9b2b2726c65408c

RUN apt-get update && \
    apt-get install -y --no-install-recommends \
        ca-certificates \
        git \
        python3-setuptools && \
    rm -rf /var/lib/apt/lists/*

RUN git clone https://github.com/idlesign/deluge-webapi.git /src && \
    git -C /src checkout ${WEBAPI_REF} && \
    cd /src && \
    rm -rf dist && \
    python3 setup.py bdist_egg

FROM ${BASE_IMAGE}

ARG DEBIAN_FRONTEND=noninteractive

EXPOSE 58846

RUN apt-get update && \
    apt-get install -y --no-install-recommends software-properties-common gpg-agent && \
    add-apt-repository ppa:deluge-team/stable -y && \
    apt-get update && \
    apt-get install -y --no-install-recommends deluged deluge-console curl jq && \
    apt-get purge -y --auto-remove software-properties-common gpg-agent && \
    rm -rf /var/lib/apt/lists/*

COPY --from=webapi-build /src/dist/WebAPI-*.egg /config/plugins/

RUN useradd -ms /bin/bash deluge && \
    chown -R deluge:deluge /config

COPY deluged_init.sh pia_port_forward.sh /sbin/

CMD ["/sbin/deluged_init.sh"]
