FROM node:10
LABEL maintainer="Benjamin Bouvier <public@benj.me>"

# Try to keep the number of RUN statements low to avoid creating too many
# layers, and try to ensure that each layer would be useful to cache.

# Install Weboob OS-level dependencies.
RUN apt-get update \
 && apt-get install -y locales git python3 python3-dev python3-pip libffi-dev \
    libxml2-dev libxslt-dev libyaml-dev libtiff-dev libjpeg-dev libopenjp2-7-dev zlib1g-dev \
    libfreetype6-dev libwebp-dev build-essential gcc g++ wget unzip mupdf-tools \
 && rm -rf /var/lib/apt/lists/;

COPY ./config.example.ini /opt/config.ini
COPY ./entrypoint.sh /entrypoint.sh

# Mundane tasks, all in one to reduce the number of layers:
# - Make sure the UTF-8 locale exists and is used by default.
# - Make sure python3 is used as default python version and link pip to pip3.
# - Then setup Kresus layout.
# - Tweak executable rights.
RUN locale-gen C.UTF-8 && \
    update-locale C.UTF-8 && \
    update-alternatives --install /usr/bin/python python $(which python3) 1 && \
    ln -s $(which pip3) /usr/bin/pip && \
    useradd -d /home/user -m -s /bin/bash -U user && \
    mkdir -p /home/user/data && \
    mkdir -p /weboob && \
    chmod -x /opt/config.ini && \
    chmod +x /entrypoint.sh;

# Install Python dependencies.
RUN pip install --upgrade setuptools && \
    pip install simplejson BeautifulSoup4 PyExecJS pdfminer.six;

# Install Kresus.
RUN npm install --prefix /home/user/app --production kresus;

# Run server.
ENV LC_ALL C.UTF-8
ENV LANG C.UTF-8
ENV HOST 0.0.0.0
ENV KRESUS_DIR /home/user/data
ENV KRESUS_WEBOOB_DIR /weboob
ENV NODE_ENV production
ENV KRESUS_PYTHON_EXEC python3

VOLUME /home/user/data
VOLUME /weboob
EXPOSE 9876

ENTRYPOINT ["/entrypoint.sh"]
CMD ["/home/user/app/node_modules/kresus/bin/kresus.js --config /opt/config.ini"]
