FROM resin/rpi-raspbian:jessie-20170531

# Set the variables
ENV DEBIAN_FRONTEND noninteractive

WORKDIR /root

# Install packages necessary for compiling python
RUN apt-get update && apt-get install -y \
        build-essential \
        libncursesw5-dev \
        libgdbm-dev \
        libc6-dev \
        zlib1g-dev \
        libsqlite3-dev \
        tk-dev \
        libssl-dev \
        openssl \
        curl \
        vim \
        git \
        mc \
        python \
        python-dev \
        python-pip \
        libncurses5-dev \
        python-pillow


RUN sudo echo "Europe/Berlin" > /etc/timezone
RUN sudo dpkg-reconfigure -f noninteractive tzdata

# Update pip and install jupyter
RUN pip install --upgrade pip
RUN pip install readline notebook==5.2.0 ipywidgets path.py requests cython
RUN jupyter nbextension enable --py --sys-prefix widgetsnbextension

# Configure jupyter
# we run this manually after mounting /root/.jupyter
# RUN jupyter notebook --generate-config
# RUN mkdir notebooks
# RUN sed -i "/c.NotebookApp.open_browser/c c.NotebookApp.open_browser = False" /root/.jupyter/jupyter_notebook_config.py \
#        && sed -i "/c.NotebookApp.ip/c c.NotebookApp.ip = '*'" /root/.jupyter/jupyter_notebook_config.py \
#        && sed -i "/c.NotebookApp.notebook_dir/c c.NotebookApp.notebook_dir = '/root/notebooks'" /root/.jupyter/jupyter_notebook_config.py

# change to letscode.00
# sudo vi /opt/fast/jupyter/config_root/jupyter_notebook_config.py


VOLUME /root/notebooks

# Add Tini. Tini operates as a process subreaper for jupyter. This prevents kernel crashes.
ENV TINI_VERSION 0.14.0
ENV CFLAGS="-DPR_SET_CHILD_SUBREAPER=36 -DPR_GET_CHILD_SUBREAPER=37"

ADD https://github.com/krallin/tini/archive/v${TINI_VERSION}.tar.gz /root/v${TINI_VERSION}.tar.gz
RUN apt-get install -y cmake
RUN tar zxvf v${TINI_VERSION}.tar.gz \
        && cd tini-${TINI_VERSION} \
        && cmake . \
        && make \
        && cp tini /usr/bin/. \
        && cd .. \
        && rm -rf "./tini-${TINI_VERSION}" \
        && rm "./v${TINI_VERSION}.tar.gz"

ENTRYPOINT ["/usr/bin/tini", "--"]

EXPOSE 8888

CMD ["jupyter", "notebook", "--allow-root"]

HEALTHCHECK --interval=10s CMD curl -sS --fail http://localhost:8888 || exit 1

# python-pip

# install ipython2 kernel into jupyter
# RUN pip install --upgrade pip
# RUN python2 -m pip install ipykernel
# RUN python2 -m ipykernel install --user

# this version will be used by matrix python bindings
RUN pyversions -d

RUN git clone https://github.com/hzeller/rpi-rgb-led-matrix.git /matrix
WORKDIR /matrix
RUN sed -i "s/=regular/=adafruit-hat-pwm/g" lib/Makefile

# for C++-demos
RUN make -C examples-api-use

# for python interface
RUN cd bindings/python && make build-python install-python

COPY pixelreceiver.py /
CMD python -u /pixelreceiver.py > /pixel/receive.log & jupyter notebook --allow-root
