BootStrap: docker
From: ubuntu:24.04

%environment
    export PATH=/opt/tools/bin:$PATH
    export RETICULATE_MINICONDA_ENABLED=FALSE
    export LANG=en_US.UTF-8
    export LANGUAGE=en_US
    export LC_ALL=en_US.UTF-8
    export LC_CTYPE=C
    export LC_TIME=en_US.UTF-8
    export LC_MONETARY=en_US.UTF-8
    export LC_PAPER=en_US.UTF-8
    export LC_MEASUREMENT=en_US.UTF-8
    export R_LIBS_USER='/usr/local/lib/R/library'

%files
    # A table with package, version and url to download them
    ./info_to_install_2025_09_09.txt /package_version_url.txt
    # R file to install all the packages using a loop
    ./singularity_from_dir.R /install_R_packages.R
    # Directory with all packages archives already downloaded
    ./2025_09_09/* /packages_dir/
    # Copy this definition in the container, for traceability
    ./singularity_from_dir.def /singularity_recipe.def

%post
    ## ------------------------------------------------------------------ ##
    ## ------------------------ Install librairies ---------------------- ##
    ## ------------------------------------------------------------------ ##
    apt-get -y update && apt-get -y upgrade
    apt-get install -y libxml2-dev
    apt-get install -y dirmngr gnupg apt-transport-https ca-certificates software-properties-common

    # install these lib after previous (else : error)
    apt-get -y update && apt-get -y upgrade
    apt-get install -y libcurl4-openssl-dev libssl-dev

    apt-get install -y wget

    ## ------------------------------------------------------------------ ##
    ## ------------- Pre requisites to install R language --------------- ##
    ## ------------------------------------------------------------------ ##
    apt-get -y update && apt-get -y upgrade
    apt-get install -y g++ gcc-13 gfortran zlib1g-dev zlib1g libbz2-dev libreadline-dev
    
    #apt-get install -y  xorg-dev # xserver-xorg-dev # libglx-dev libgl-dev mesa-common-dev
    apt-get install -y libbz2-dev liblzma-dev libpango1.0-dev build-essential
    apt-get install -y libcurl4-gnutls-dev libssl-dev

    ## Java dependencies for some packages
    apt-get -y update && apt-get -y upgrade
    apt-get install -y default-jre default-jdk default-jdk-headless #openjdk-8-jdk openjdk-8-jre
    export JAVA_HOME="/usr/lib/jvm/java-11-openjdk-amd64/jre"
    export PATH=$PATH:$JAVA_HOME/bin

    ## Package specific installation
    apt-get -y update && apt-get -y upgrade

    # Package : nloptr
    apt-get install -y cmake

    # Package : hdf5r
    apt-get install -y libhdf5-dev

    # Package : units
    apt-get install -y libudunits2-dev

    # Package : ragg
    apt-get install -y libtiff-dev

    # Package : jpeg
    apt-get -y update && apt-get -y upgrade
    apt-get install -y libjpeg-dev libjpeg-turbo8-dev

    # Package : rjags
    apt-get -y install pkg-config jags

    # Package : sf
    apt-get -y install libgdal-dev

    # Package : rmarkdown
    wget -O pandoc.deb https://github.com/jgm/pandoc/releases/download/3.1.3/pandoc-3.1.3-1-amd64.deb
    dpkg -i pandoc.deb
    rm /pandoc.deb

    ## ------------------------------------------------------------------ ##
    ## ----------------------- Install R language ----------------------- ##
    ## ------------------------------------------------------------------ ##
    ## As described in https://cran.r-project.org/bin/linux/ubuntu/
    apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9

    export R_VERSION=4.4.1
    cd /usr/local/src
    wget -O R-${R_VERSION}.tar.gz https://cran.r-project.org/src/base/R-4/R-${R_VERSION}.tar.gz
    tar xzvf R-${R_VERSION}.tar.gz
    cd R-${R_VERSION}
    ./configure \
        --prefix=/usr/local \
        --x-includes=/usr/include/X11 \
        --x-libraries=/usr/lib/X11 \
        --enable-R-shlib \
        --enable-prebuilt-html
    make && make install

    rm /usr/local/src/R-${R_VERSION}.tar.gz
    
    ## ------------------------------------------------------------------ ##
    ## --------------------- Install all R packages --------------------- ##
    ## ------------------------------------------------------------------ ##
    apt-get -y update && apt-get -y upgrade
    Rscript /install_R_packages.R /package_version_url.txt

    ## ------------------------------------------------------------------ ##
    ## ---------------------------- Clean ------------------------------- ##
    ## ------------------------------------------------------------------ ##
    rm /install_R_packages.R
    rm /package_version_url.txt
    rm -R /packages_dir

%labels
    Author Audrey ONFROY

%help
    Time to build : 2h15
