FROM ubuntu:jammy AS jags

RUN apt update && apt install -y --no-install-recommends curl make ca-certificates gcc g++ gfortran liblapack-dev
# These usually are not packaged in OS repositories and need to be installed from source
# - JAGS (needed for runjags which itself is again a dependency of many packages)
RUN curl -q -L -O https://sourceforge.net/projects/mcmc-jags/files/JAGS/4.x/Source/JAGS-4.3.2.tar.gz && tar -xzf JAGS-4.3.2.tar.gz && cd JAGS-4.3.2 && ./configure --enable-silent-rules && make 2>&1

FROM ubuntu:jammy AS final

ARG R_VERSION=4.4.3
ENV PATH=/opt/R/${R_VERSION}/bin:$PATH
ENV TZ="Europe/Berlin"
ENV DEBIAN_FRONTEND=noninteractive

ARG ARCH=arm64
ENV NCPUS=2
ENV R_LIBS_USER=/mnt/cache/R-pkgs
ENV R_PKG_CACHE_DIR=/mnt/cache/pkgcache

ARG GITHUB_PAT

COPY --link --from=jags /JAGS-4.3.2 /JAGS-4.3.2

# xvfb: for graphical pkgs requiring tcl, like gWidgets2tcltk
# tcl tk tk-dev tk-table: must be available before running xvfb-run (i.e. starting the graphical session)
# libmecab-dev: RcppMeCab -> no autodetection yet
# libfftw3-dev: ravetools -> actually auto-detect but somehow doesn't work in container?
RUN apt update && apt install -y --no-install-recommends gdebi-core g++ gfortran libbz2-dev libblas-dev libicu-dev liblapack-dev liblzma-dev libpaper-utils libpcre2-dev libtcl8.6 libtk8.6 libxt6 unzip zip zlib1g-dev libpcre3-dev libopenblas-dev curl ca-certificates make ccache htop libgit2-dev git libpq-dev libxml2-dev libgit2-dev cmake xvfb libcurl4-openssl-dev xz-utils tcl tk tk-dev tk-table pkg-config libmecab-dev libfftw3-dev automake

### CUSTOM DEPENDENCIES
# - Chromium should be auto-installed once pkgdepends merges the latest system-requirements rule changes
RUN apt-get install -y software-properties-common && \
  add-apt-repository -y ppa:xtradeb/apps && \
  apt update && \
  apt install -y chromium

### INSTALL R
RUN curl -O "https://devxy-r-builds.fsn1.your-objectstorage.com/2204/r-${R_VERSION}_1_$(dpkg --print-architecture).deb"
RUN gdebi -n -qq "r-${R_VERSION}_1_$(dpkg --print-architecture).deb" && rm "r-${R_VERSION}_1_$(dpkg --print-architecture).deb"
RUN R -q -e 'install.packages("pak", repos = sprintf("https://r-lib.github.io/p/pak/devel/%s/%s/%s", .Platform$pkgType, R.Version()$os, R.Version()$arch))'

### Makevars
# dbarts: CFLAGS += -flax-vector-conversions (https://github.com/vdorie/dbarts/issues/66)
# can't set ccache for gfortran because of https://gitlab.com/roigrp/solver/highs/-/issues/4
RUN mkdir -p /root/.R && bash -c 'echo -e "CXX_STD = CXX14\nCFLAGS += -flax-vector-conversions\nCC=ccache gcc\nCPP=gcc -E\nCXXCPP=gcc -e\nCXX=ccache g++\nCXX11=ccache g++\nCXX14=ccache g++\nCXX17=ccache g++\nF77=gfortran\nFC=gfortran" >> /root/.R/Makevars'

# R repos
RUN bash -c 'echo -e "options(pkg.sysreqs_verbose = TRUE, crayon.enabled = TRUE, Ncpus = ${NCPUS}, future.globals.onReference = NULL, repos = structure(c(CRAN = \"https://cran.devxy.io/$ARCH/jammy/latest\", INLA = \"https://inla.r-inla-download.org/R/stable\")))" > ~/.Rprofile'

# these dirs will later be used by the CI with existing mounts
RUN mkdir -p /mnt/cache/packages /mnt/cache/pkgcache /mnt/cache/R-pkgs

RUN apt install -y locales && echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen \
  && locale-gen en_US.utf8 \
  && /usr/sbin/update-locale LANG=en_US.UTF-8

RUN R -q -e 'pak::pak("git::https://codefloe.com/rpkgs/bincraftr.git", dependencies = TRUE)'

CMD ["/bin/bash"]
