# Rx3-Docker/Orthanc 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 ORTHANC_LIB_DIR=/var/lib/orthanc
ARG ORTHANC_HOME_DIR=${ORTHANC_LIB_DIR}
ARG ORTHANC_CONFIG_DIR=/etc/orthanc
ARG ORTHANC_CONFIG_FILE=${ORTHANC_CONFIG_DIR}/orthanc.json
ARG ORTHANC_LOG_DIR=/var/log/orthanc
ARG ORTHANC_HTTP_PORT=8042
ARG ORTHANC_DICOM_PORT=4242



#---------------------------------------------------------------------------------------------------
ARG ORTHANCD='#!/bin/bash                                                                        \n\
                                                                                                 \n\
if [[ ! -f '${ORTHANC_CONFIG_FILE}' ]]                                                           \n\
then                                                                                             \n\
    cp -Rf '${ORTHANC_CONFIG_DIR}.ref/*'                                '${ORTHANC_CONFIG_DIR}'  \n\
                                                                                                 \n\
    sed -i -e "s/ORTHANC_USER/${ORTHANC_USER}/"                         '${ORTHANC_CONFIG_FILE}' \n\
    sed -i -e "s/ORTHANC_PASSWORD/${ORTHANC_PASSWORD}/"                 '${ORTHANC_CONFIG_FILE}' \n\
fi                                                                                               \n\
                                                                                                 \n\
usermod  -u ${ORTHANC_UID} orthanc                                                               \n\
groupmod -g ${ORTHANC_GID} orthanc                                                               \n\
                                                                                                 \n\
chown -R orthanc:orthanc '${ORTHANC_LIB_DIR}' '${ORTHANC_CONFIG_DIR}' '${ORTHANC_LOG_DIR}
#---------------------------------------------------------------------------------------------------



#---------------------------------------------------------------------------------------------------
ARG ORTHANCD_INI='[program:orthancd]                                                             \n\
directory='${ORTHANC_HOME_DIR}'                                                                  \n\
command=orthanc '${ORTHANC_CONFIG_FILE}'                                                         \n\
user=orthanc                                                                                     \n\
environment=USER="orthanc", HOME="'${ORTHANC_HOME_DIR}'"'
#---------------------------------------------------------------------------------------------------



RUN    . /etc/profile.d/rx3.sh && isl --add ${IMG_URL}                                                                                          \
    && urpmi.update -a                                                                                                                          \
    && urpmi --force orthanc dcmtk                                                                                                              \
    && sed -i -e  's/"RemoteAccessAllowed" : false,/"RemoteAccessAllowed" : true,/'                            ${ORTHANC_CONFIG_FILE}           \
    && sed -i -e  's/\(Basic .*\)/\1\n\n  "RegisteredUsers" : \{ "ORTHANC_USER" : "ORTHANC_PASSWORD" \},\n/'   ${ORTHANC_CONFIG_FILE}           \
    && mv         ${ORTHANC_CONFIG_DIR}                                                                        ${ORTHANC_CONFIG_DIR}.ref        \
    && echo  -e   "${ORTHANCD}" | sed -e "s/\`/'/g" -e 's/ *$//'                                             > /etc/init.d/orthancd             \
    && chmod  a+x /etc/init.d/orthancd                                                                                                          \
    && ln    -s   /etc/init.d/orthancd /etc/rcD.d/S40orthancd                                                                                   \
    && echo  -e   "${ORTHANCD_INI}" | sed -e "s/\`/'/g" -e 's/ *$//'                                         > /etc/supervisord.d/orthancd.ini 



VOLUME ${ORTHANC_LOG_DIR}
VOLUME ${ORTHANC_LIB_DIR}

EXPOSE ${ORTHANC_HTTP_PORT}
EXPOSE ${ORTHANC_DICOM_PORT}

#    && useradd --home-dir ${ORTHANC_HOME_DIR} --password "" orthanc                                \
