FROM ubuntu:16.04
MAINTAINER Caspar
WORKDIR /opt

ENV QT_VERSION_MINOR 5.7
ENV QT_VERSION_PATCH 5.7.1
ENV MITK_HASH a0dedffb74dbcb57726366a56716f489ccf18337

RUN apt-get -y update && apt-get -y install \
  bison \
  build-essential \
  cmake \
  cmake-curses-gui \
  docbook-xsl-ns \
  flex \
  git \
  gperf \
  gyp \
  libasound2-dev \
  libbz2-dev \
  libcap-dev \
  libcups2-dev \
  libdrm-dev \
  libegl1-mesa-dev \
  libfontconfig1-dev \
  libgcrypt11-dev \
  libglu1-mesa-dev \
  libnss3-dev \
  libpci-dev \
  libpulse-dev \
  libssl-dev \
  libtiff5-dev \
  libudev-dev \
  libwrap0-dev \
  libxcb-xinerama0-dev \
  libxcomposite-dev \
  libxcursor-dev \
  libxdamage-dev \
  libxrandr-dev \
  libxss-dev \
  libxt-dev \
  libxtst-dev \
  ninja-build \
  patchelf \
  python \
  ruby \
  wget \
  xz-utils

RUN mkdir /opt/mbi-src \
  && mkdir /opt/mbi-bin \
  && mkdir /opt/mitk-bin \
  && mkdir /opt/bin
RUN git clone -n https://github.com/MITK/MITK.git mitk-src && cd mitk-src && git checkout $MITK_HASH \
  && cd /opt/mitk-bin \
  && cmake \
  -GNinja \
  -DCMAKE_BUILD_TYPE:STRING=Release \
  -DMITK_USE_Qt5:BOOL=OFF \
  -DMITK_USE_Qt5_WebEngine:BOOL=OFF \
  -DMITK_USE_BLUEBERRY:BOOL=OFF \
  -DMITK_USE_CTK:BOOL=OFF \
  -DMITK_USE_Vigra:BOOL=ON \
  -DMITK_USE_HDF5:BOOL=ON \
  -DCMAKE_INSTALL_PREFIX=/opt/install \
  ../mitk-src \
  && ninja MITK-Configure \
  && cd MITK-build \
  && cmake -DBUILD_DiffusionMiniApps:Bool=TRUE . \
  && ninja
  
COPY shapemodels/ /opt/modeldir/

ADD mitk-mbi /opt/mbi-src

RUN cd /opt/mbi-bin \
  && cmake -GNinja -DCMAKE_BUILD_TYPE:STRING=Release -DMBI_SUPERBUILD:BOOL=OFF -DMITK_DIR:STRING=/opt/mitk-bin/MITK-build -DBUILD_TESTING:BOOL=OFF -DMBI_BUILD_APP_MBIApp:BOOL=OFF /opt/mbi-src/mbi/ \
  && ninja \
  && ldd /opt/mbi-bin/bin/ShapeModelMiniApps | awk 'BEGIN{ORS=" "}$1~/^\//{print $1}$3~/^\//{print $3}' | xargs -i -n 1 -d ' ' cp {} /opt/bin \
  && cp /opt/mbi-bin/bin/ShapeModelMiniApps /opt/bin \
  && cd /opt/bin/ \
  && patchelf --set-rpath /opt/bin ShapeModelMiniApps \
  && cd /opt/ \
  && rm -rf /opt/mitk-* \
  && rm -rf /opt/mbi-*

ENV LD_LIBRARY_PATH /opt/bin/