FROM mundialis/docker-pdal:2.1.0 as pdal
FROM alpine:3.12 as common

# Based on:
# https://github.com/mundialis/docker-grass-gis/blob/master/Dockerfile
LABEL authors="Pietro Zambelli,Markus Neteler"
LABEL maintainer="peter.zamb@gmail.com,neteler@osgeo.org"

# PACKAGES VERSIONS
ARG PYTHON_VERSION=3

# List of packages to be installed
ENV PACKAGES="\
      attr \
      bash \
      bison \
      bzip2 \
      cairo \
      curl \
      fftw \
      flex \
      freetype \
      gdal \
      gdal-tools \
      gettext \
      geos \
      gnutls \
      jsoncpp \
      libbz2 \
      libexecinfo \
      libjpeg-turbo \
      libpng \
      libunwind \
      musl \
      musl-utils \
      ncurses \
      openjpeg \
      openblas \
      py3-numpy \
      py3-pillow \
      py3-six \
      postgresql \
      proj-datumgrid \
      proj-util \
      sqlite \
      sqlite-libs \
      subversion \
      tiff \
      zstd \
      zstd-libs \
    "
# ====================
# INSTALL DEPENDENCIES
# ====================

WORKDIR /src

ENV PYTHONBIN=python$PYTHON_VERSION

RUN echo "Install Python";\
    apk add --no-cache $PYTHONBIN && \
    $PYTHONBIN -m ensurepip && \
    rm -r /usr/lib/python*/ensurepip && \
    pip$PYTHON_VERSION install --upgrade pip setuptools && \
    if [ ! -e /usr/bin/pip ]; then ln -s pip$PYTHON_VERSION /usr/bin/pip ; fi && \
    if [[ ! -e /usr/bin/python ]]; then ln -sf /usr/bin/$PYTHONBIN /usr/bin/python; fi && \
    rm -r /root/.cache

# Add the packages
RUN echo "Install main packages";\
    apk update; \
    apk add --no-cache $PACKAGES

COPY --from=pdal /usr/bin/pdal* /usr/bin/
COPY --from=pdal /usr/lib/libpdal* /usr/lib/
COPY --from=pdal /usr/lib/pkgconfig/pdal.pc /usr/lib/pkgconfig/pdal.pc
COPY --from=pdal /usr/include/pdal /usr/include/pdal
COPY --from=pdal /usr/local/lib/liblaszip* /usr/local/lib/
COPY --from=pdal /usr/local/include/laszip /usr/local/include/laszip


FROM common as build

# ================
# CONFIG VARIABLES
# ================

# set configuration options, without wxGUI
ENV GRASS_CONFIG="\
      --enable-largefile \
      --with-cxx \
      --with-proj --with-proj-share=/usr/share/proj \
      --with-gdal \
      --with-pdal \
      --with-python \
      --with-geos \
      --with-sqlite \
      --with-bzlib \
      --with-zstd \
      --with-cairo --with-cairo-ldflags=-lfontconfig \
      --with-fftw \
      --with-postgres --with-postgres-includes='/usr/include/postgresql' \
      --without-freetype \
      --without-openmp \
      --without-opengl \
      --without-nls \
      --without-mysql \
      --without-odbc \
      --without-openmp \
      --without-ffmpeg \
      "

# Set environmental variables for GRASS GIS compilation, without debug symbols
ENV MYCFLAGS="-O2 -std=gnu99 -m64" \
    MYLDFLAGS="-s -Wl,--no-undefined -lblas" \
    # CXX stuff:
    LD_LIBRARY_PATH="/usr/local/lib" \
    LDFLAGS="$MYLDFLAGS" \
    CFLAGS="$MYCFLAGS" \
    CXXFLAGS="$MYCXXFLAGS" \
    NUMTHREADS=2

# These packages are required to compile GRASS GIS.
ENV GRASS_BUILD_PACKAGES="\
      build-base \
      bzip2-dev \
      cairo-dev \
      fftw-dev \
      freetype-dev \
      g++ \
      gcc \
      gdal-dev \
      geos-dev \
      git \
      gnutls-dev \
      libc6-compat \
      libjpeg-turbo-dev \
      libpng-dev \
      make \
      openjpeg-dev \
      openblas-dev \
      postgresql-dev \
      proj-dev \
      python3-dev \
      py3-numpy-dev \
      sqlite-dev \
      tar \
      tiff-dev \
      unzip \
      vim \
      wget \
      zip \
      zstd-dev \
    "

# Add the packages
RUN echo "Install main packages";\
    # Add packages just for the GRASS build process
    apk add --no-cache --virtual .build-deps $GRASS_BUILD_PACKAGES
    # echo LANG="en_US.UTF-8" > /etc/default/locale;

# Copy and install GRASS GIS
COPY . /src/grass_build/
WORKDIR /src/grass_build/

# Configure compile and install GRASS GIS
RUN echo "  => Configure and compile grass" && \
    /src/grass_build/configure $GRASS_CONFIG && \
    make -j $NUMTHREADS && \
    make install && \
    ldconfig /etc/ld.so.conf.d

# enable simple grass command regardless of version number
# also important for COPY cmd later
RUN ln -s `find /usr/local/bin -name "grass*"` /usr/local/bin/grass

# Reduce the image size - Remove unnecessary grass files
RUN cp /usr/local/grass78/gui/wxpython/xml/module_items.xml module_items.xml; \
    rm -rf /usr/local/grass78/demolocation; \
    rm -rf /usr/local/grass78/fonts; \
    rm -rf /usr/local/grass78/gui; \
    rm -rf /usr/local/grass78/share; \
    mkdir -p /usr/local/grass78/gui/wxpython/xml/; \
    mv module_items.xml /usr/local/grass78/gui/wxpython/xml/module_items.xml;


FROM common as grass

ENV LC_ALL="en_US.UTF-8"

# Copy GRASS GIS from build image
COPY --from=build /usr/local/bin/grass /usr/local/bin/grass
COPY --from=build /usr/local/grass* /usr/local/grass/
# pip 20.0.0 fix
RUN apk add curl && curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py && python3 get-pip.py pip==20.0.2 && rm get-pip.py
RUN pip3 install --upgrade pip six grass-session

RUN ln -s /usr/local/grass /usr/local/grass7
RUN ln -s /usr/local/grass `grass --config path`
RUN grass --tmp-location EPSG:4326 --exec g.version -rge && \
    pdal --version && \
    python3 --version


FROM grass as test

RUN apk add make gcc

## run simple LAZ test
COPY docker/testdata/simple.laz /tmp/simple.laz
COPY docker/testdata/test_grass_session.py /scripts/test_grass_session.py
ENV GRASSBIN=grass
RUN grass --tmp-location EPSG:4326 --exec g.extension extension=r.in.pdal
RUN /usr/bin/python3 /scripts/test_grass_session.py

# Test addon installation
RUN grass --tmp-location EPSG:4326 --exec g.extension extension=r.learn.ml


FROM grass as final

# GRASS GIS specific
# allow work with MAPSETs that are not owned by current user
ENV GRASSBIN="/usr/local/bin/grass" \
    GRASS_SKIP_MAPSET_OWNER_CHECK=1 \
    SHELL="/bin/bash"

# show installed version
RUN grass --tmp-location EPSG:4326 --exec g.version -rge && \
    pdal --version && \
    python3 --version

# Data workdir
WORKDIR /grassdb
VOLUME /grassdb

CMD $GRASSBIN --version
