# Rx3-Docker/Deluge Dockerfile
#-------------------------------------------------------------------------------

ARG IMG_FROM_URL="rx3-docker/server:latest"

FROM ${IMG_FROM_URL}

ARG IMG_NAME
ARG IMG_NAME_FULL
ARG IMG_VERSION
ARG IMG_MAINTAINER
ARG IMG_URL

LABEL org.rx3.${IMG_NAME}.name=${IMG_NAME_FULL}
LABEL org.rx3.${IMG_NAME}.version=${IMG_VERSION}
LABEL org.rx3.${IMG_NAME}.maintainer=${IMG_MAINTAINER}
LABEL maintainer=${IMG_MAINTAINER}

ARG MODULE_PATH
ARG DELUGE_LIB
ARG DELUGE_SPOOL
ARG DELUGE_PORT_DAEMON
ARG DELUGE_PORT_WEB

ARG DELUGE_HOME=${DELUGE_LIB}
ARG DELUGE_CONFIG=${DELUGE_HOME}
ARG DELUGE_CONFIG_TEMPLATE=${DELUGE_HOME}-template



#-----------------------------------------------------------------------------------------------------------------------
ARG DELUGED='#!/bin/bash                                                                                             \n\
                                                                                                                     \n\
usermod  -u ${DELUGE_UID} deluge                                                                                     \n\
groupmod -g ${DELUGE_GID} deluge                                                                                     \n\
                                                                                                                     \n\
if [[ ! -f '${DELUGE_CONFIG}'/auth ]]                                                                                \n\
then                                                                                                                 \n\
    cp -R '${DELUGE_CONFIG_TEMPLATE}'/* '${DELUGE_CONFIG}'                                                           \n\
                                                                                                                     \n\
    hash="$(head -c 256 /dev/urandom | base64 | tr -dc '0-9a-f' | cut -c1-41)"                                       \n\
    echo "localclient:${hash}:10"                                                    >> '${DELUGE_CONFIG}'/auth      \n\
                                                                                                                     \n\
    sed -e "s/DELUGE_PORT_BT_LOW/${DELUGE_PORT_BT_LOW}/"                             -i '${DELUGE_CONFIG}'/core.conf \n\
    sed -e "s/DELUGE_PORT_BT_HIGH/${DELUGE_PORT_BT_HIGH}/"                           -i '${DELUGE_CONFIG}'/core.conf \n\
fi                                                                                                                   \n\
                                                                                                                     \n\
for dir in torrent-auto torrent tmp completed                                                                        \n\
do                                                                                                                   \n\
    if [[ ! -d '${DELUGE_SPOOL}'/${dir} ]]                                                                           \n\
    then                                                                                                             \n\
        mkdir '${DELUGE_SPOOL}'/${dir}                                                                               \n\
    fi                                                                                                               \n\
done                                                                                                                 \n\
                                                                                                                     \n\
chown -R deluge:deluge '${DELUGE_LIB}' '${DELUGE_SPOOL}
#-----------------------------------------------------------------------------------------------------------------------



#-----------------------------------------------------------------------------------------------------------------------
ARG DELUGED_INI='[program:deluged]                                                                                   \n\
directory='${DELUGE_HOME}'                                                                                           \n\
command=deluged --config '${DELUGE_CONFIG}' --do-not-daemonize                                                       \n\
user=deluge                                                                                                          \n\
environment=HOME='${DELUGE_HOME}
#-----------------------------------------------------------------------------------------------------------------------



#-----------------------------------------------------------------------------------------------------------------------
ARG DELUGE_WEB_INI='[program:deluge_web]                                                                             \n\
directory='${DELUGE_HOME}'                                                                                           \n\
command=/bin/bash -c `deluge-web --config '${DELUGE_CONFIG}' --base ${DELUGE_WEB_PREFIX} --do-not-daemonize`         \n\
user=deluge                                                                                                          \n\
environment=HOME='${DELUGE_HOME}
#-----------------------------------------------------------------------------------------------------------------------



RUN    . /etc/profile.d/rx3.sh && isl --add ${IMG_URL}                                                                 \
    && useradd --home-dir ${DELUGE_HOME} --password "" deluge                                                          \
    && urpmi.update -a                                                                                                 \
    && urpmi --force deluge --no-recommends                                                                            \
    && mkdir        ${DELUGE_SPOOL}                                                                                    \
    && echo  -e     "${DELUGED}"        | sed -e "s/\`/'/g" -e 's/ *$//'           > /etc/init.d/deluged               \
    && chmod a+x    /etc/init.d/deluged                                                                                \
    && ln    -s     /etc/init.d/deluged /etc/rcD.d/S30deluged                                                          \
    && echo  -e     "${DELUGED_INI}"    | sed -e "s/\`/'/g" -e 's/ *$//'           > /etc/supervisord.d/deluged.ini    \
    && echo  -e     "${DELUGE_WEB_INI}" | sed -e "s/\`/'/g" -e 's/ *$//'           > /etc/supervisord.d/deluge_web.ini
    
ADD    ${MODULE_PATH} ${DELUGE_CONFIG_TEMPLATE}



EXPOSE ${DELUGE_PORT_DAEMON}
EXPOSE ${DELUGE_PORT_WEB}
