Bootstrap: docker
From: debian:bullseye

%help
Singularity container for GRASS GIS to be run into GRASS main directory

%labels
  Maintainer Luca Delucchi

%setup
  mkdir /tmp/grass_build

%files
  . /tmp/grass_build

%post
    # Install useful libraries
    apt-get -y update
    apt-get -y install \
        build-essential \
        bison \
        bzip2 \
        cmake \
        curl \
        flex \
        g++ \
        gcc \
        gdal-bin \
        gettext \
        git \
        libbz2-dev \
        libcairo2 \
        libcairo2-dev \
        libcurl4-gnutls-dev \
        libfftw3-bin \
        libfftw3-dev \
        libfreetype6-dev \
        libgdal-dev \
        libgeos-dev \
        libgsl-dev \
        libjpeg-dev \
        libjsoncpp-dev \
        libopenblas-base \
        libopenblas-dev \
        libnetcdf-dev \
        libncurses5-dev \
        libopenjp2-7 \
        libopenjp2-7-dev \
        libpdal-dev \
        libpnglite-dev \
        libpq-dev \
        libpython3-all-dev \
        libreadline-dev \
        libsqlite3-dev \
        libtiff-dev \
        libzstd-dev \
        make \
        mesa-common-dev \
        moreutils \
        ncurses-bin \
        netcdf-bin \
        pdal \
        python3 \
        python3-dateutil \
        python3-dev \
        python3-gdal \
        python3-magic \
        python3-numpy \
        python3-pdal \
        python3-pil \
        python3-pip \
        python3-ply \
        python3-requests \
        python3-setuptools \
        python3-venv \
        software-properties-common \
        sqlite3 \
        subversion \
        unzip \
        vim \
        wget \
        zip \
        zlib1g-dev

    # Set Python 3 as default Python version
    update-alternatives --install /usr/bin/python python /usr/bin/python3 1
    echo LANG="en_US.UTF-8" > /etc/default/locale
    # compile GRASS
    cd /tmp/grass_build
    # -O2 -march=native -std=gnu99 -m64
    LDFLAGS="-s"
    CFLAGS="-O2 -std=gnu99 -m64"
    CXXFLAGS="-O2"
    GRASS_PYTHON=/usr/bin/python3
    ./configure \
        --enable-largefile \
        --with-cxx \
        --with-nls \
        --with-readline \
        --with-sqlite \
        --with-bzlib \
        --with-zstd \
        --with-cairo \
        --with-cairo-ldflags=-lfontconfig \
        --with-freetype \
        --with-freetype-includes="/usr/include/freetype2/" \
        --with-fftw \
        --with-netcdf \
        --with-pdal \
        --with-proj \
        --with-proj-share=/usr/share/proj \
        --with-geos=/usr/bin/geos-config \
        --with-postgres \
        --with-postgres-includes="/usr/include/postgresql" \
        --without-mysql \
        --without-odbc \
        --without-openmp \
        --without-ffmpeg \
        --without-opengl
	make -j 2 && make install && ldconfig
    # Create generic GRASS GIS binary name regardless of version number
    ln -sf `find /usr/local/bin -name "grass??" | sort | tail -n 1` /usr/local/bin/grass
    ln -sf `find /usr/local -type d -name "grass??" | sort | tail -n 1` /usr/local/grass
    # Remove unused file
    apt-get autoremove -y
    apt-get clean -y
    cd /tmp
    rm -rf /src/grass_build
    # Add useful GRASS addons
    grass -c EPSG:4326 --tmp-location --exec g.extension -s ext=i.modis
    grass -c EPSG:4326 --tmp-location --exec g.extension -s ext=i.sentinel
    grass -c EPSG:4326 --tmp-location --exec g.extension -s ext=r.in.pdal
    #grass -c EPSG:4326 --tmp-location --exec g.extension -s ext=i.wi
    #grass -c EPSG:4326 --tmp-location --exec g.extension -s ext=r.bioclim
    #grass -c EPSG:4326 --tmp-location --exec g.extension -s ext=r.series.lwr
    #grass -c EPSG:4326 --tmp-location --exec g.extension -s ext=v.clip
    #grass -c EPSG:4326 --tmp-location --exec g.extension -s ext=v.strds.stats

    # Add Python GRASS session
    pip3 install grass-session

%environment
    export LANG=C.UTF-8
    export LC_ALL=C.UTF-8
    export SHELL=/bin/bash
    export LD_LIBRARY_PATH="/usr/local/lib"

%runscript
    grass --version
