FROM opensuse/leap:15.1

# Useful packages (we need jpeg and zlib in order to compile libtiff)
RUN zypper --non-interactive install  git cmake gcc gcc-c++ wget tar gzip patch jpeg zlib

# SQLITE3 build
RUN cd && wget https://sqlite.org/2019/sqlite-autoconf-3300000.tar.gz \
  && tar -xvf sqlite-autoconf-3300000.tar.gz \
  && cd sqlite-autoconf-3300000 \
  && ./configure \
  && make \
  && make install

#PROJ 6.2 build
RUN cd && wget https://download.osgeo.org/proj/proj-6.2.0.tar.gz \
  && tar -xvf proj-6.2.0.tar.gz \
  && cd proj-6.2.0 \
  && mkdir build \
  && cd build \
  && cmake -DSQLITE3_INCLUDE_DIR=/usr/local/include -DSQLITE3_LIBRARY=/usr/local/lib/libsqlite3.so .. \
  && make -j12 \
  && make install
 

# TIFF 
RUN cd && wget http://download.osgeo.org/libtiff/tiff-4.0.8.tar.gz \
  && tar -xvf tiff-4.0.8.tar.gz \
  && cd tiff-4.0.8 \
  && mkdir build2 \
  && cd build2 \
  && cmake .. \
  && make -j12 \
  && make install 


# GEOTIFF 
RUN cd && wget http://download.osgeo.org/geotiff/libgeotiff/libgeotiff-1.5.1.tar.gz \
  && tar -xvf libgeotiff-1.5.1.tar.gz \
  && cd libgeotiff-1.5.1/ \
  && mkdir build \
  && cd build \
  && cmake .. \
  && make -j12 \
  && make install 

# GDAL 3.0.1 build
RUN cd && wget http://download.osgeo.org/gdal/3.0.1/gdal-3.0.1.tar.gz \
  && tar -xvf gdal-3.0.1.tar.gz \
  && cd gdal-3.0.1 \
  && ./configure \
  && make -j12 \
  && make install 


ENV LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:/usr/local/lib:/usr/local/lib64

# Superbuild (without python and visualization) note that OSSIM is built with the GEOTIFF lib compiled above.
RUN cd && git clone https://gitlab.orfeo-toolbox.org/orfeotoolbox/otb.git OTB -b release-7.0 \
  && mkdir build && cd build \
  && cmake -DOTB_USE_GLEW=OFF -DOTB_USE_GLFW=OFF -DOTB_USE_GLUT=OFF \ 
    -DOTB_USE_OPENGL=OFF -DOTB_USE_QT=OFF -DOTB_WRAP_PYTHON=OFF \
    -DUSE_SYSTEM_PROJ=ON \
    -DUSE_SYSTEM_TIFF=ON \
    -DUSE_SYSTEM_GEOTIFF=ON \
    -DUSE_SYSTEM_GDAL=ON \
    ../OTB/SuperBuild \
  && make OTB_DEPENDS -j12


# build OTB
RUN cd ~/build && make OTB -j12

# Add the superbuild install dir to env

ENV PATH=$PATH:~/superbuild_install/bin
ENV LD_LIBRARY_PATH=${LD_LIBRARY_PATH}:~/superbuild_install/lib
ENV OTB_APPLICATION_PATH=~/superbuild_install/lib/otb/applications
