Bootstrap: docker
From: debian:bullseye

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

%labels
  Maintainer Luca Delucchi

%setup
  mkdir -p /tmp/grass_build

%files
  * /tmp/grass_build

%post
    # Install useful libraries
    apt-get -y update
    apt-get -y install \
        bison \
        build-essential \
        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 \
        libncurses5-dev \
        libnetcdf-dev \
        libopenblas-base \
        libopenblas-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-pillow \
        python3-pip \
        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-bzlib \
        --with-cairo \
        --with-cairo-ldflags=-lfontconfig \
        --with-cxx \
        --with-fftw \
        --with-freetype \
        --with-freetype-includes="/usr/include/freetype2/" \
        --with-geos=/usr/bin/geos-config \
        --with-netcdf \
        --with-nls \
        --with-pdal \
        --with-postgres \
        --with-postgres-includes="/usr/include/postgresql" \
        --with-proj \
        --with-proj-share=/usr/share/proj \
        --with-readline \
        --with-sqlite \
        --with-zstd \
        --without-ffmpeg \
        --without-mysql \
        --without-odbc \
        --without-opengl \
        --without-openmp
	make -j 2 && make install && ldconfig
    # Create generic GRASS binary name regardless of version number
    ln -sf `find /usr/local/bin -name "grass??" | sort | tail -n 1` /usr/local/bin/grass
    # Create generic GRASS lib name regardless of version number
    ln -sf `grass --config path` /usr/local/grass
    # Remove unused file
    apt-get autoremove -y
    apt-get clean -y
    cd /tmp
    rm -rf /tmp/grass_build
    # Add useful GRASS addons
    grass -c EPSG:4326 --tmp-project --exec g.extension -s ext=i.modis
    grass -c EPSG:4326 --tmp-project --exec g.extension -s ext=i.sentinel
    grass -c EPSG:4326 --tmp-project --exec g.extension -s ext=r.in.pdal
    #grass -c EPSG:4326 --tmp-project --exec g.extension -s ext=i.wi
    #grass -c EPSG:4326 --tmp-project --exec g.extension -s ext=r.bioclim
    #grass -c EPSG:4326 --tmp-project --exec g.extension -s ext=r.series.lwr
    #grass -c EPSG:4326 --tmp-project --exec g.extension -s ext=v.clip
    #grass -c EPSG:4326 --tmp-project --exec g.extension -s ext=v.strds.stats

%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
