ARG OS_VERSION
FROM devxygmbh/alpine-jags:${OS_VERSION} AS jags

ARG OS_VERSION
FROM alpine:${OS_VERSION} AS final

ARG R_VERSION=4.4.3
ENV PATH=/opt/R/${R_VERSION}/bin:$PATH
ENV TZ="Europe/Berlin"
ENV R_LIBS_USER=/mnt/cache/R-pkgs
ENV R_PKG_CACHE_DIR=/mnt/cache/pkgcache

ENV NCPUS=2

ARG GITHUB_PAT

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

# Background: JAGS needs lapack & lapack-dev but these conflict with openblas-dev which is required by R-dev
# Solution: first compile JAGS, then remove lapack & lapack-dev again
# # xvfb: for graphical pkgs requiring tcl, like gWidgets2tcltk
RUN apk add -q --no-cache curl tar make blas blas-dev gcc gfortran g++ htop git cmake tk-dev xvfb xvfb-run xorg-server xauth xinit xz libdeflate libdeflate-dev

### CUSTOM DEPENDENCIES

# linux-headers: ps
# libxml2-dev: xml2
# libgit2-dev: gert
# libpq-dev: RPostgres
# libfftw3-dev: ravetools -> actually auto-detect but somehow doesn't work in container?
RUN apk add -q --no-cache R-dev curl ca-certificates bash g++ tzdata openjdk17 tar sed patch openblas-dev pkgconfig linux-headers ccache libxml2-dev libgit2-dev libpq-dev pkgconfig fftw-dev automake autoconf libtool elfutils-dev
# - Chromium should be auto-installed once pkgdepends merges the latest system-requirements rule changes
RUN apk add -q --no-cache chromium

# https://github.com/RcppCore/Rcpp/issues/448#issuecomment-220148774
# LDFLAGS+=-fPIC -> arrow (https://github.com/r-hub/r-minimal/blob/main/examples/arrow/Dockerfile)
# dbarts: CFLAGS += -flax-vector-conversions (https://github.com/vdorie/dbarts/issues/66)
# CXXFLAGS: -> Rcpp
# -DDMLC_USE_FOPEN64=0 -> xgboost (https://github.com/dmlc/xgboost/issues/8595)
# 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\nCXXFLAGS=-D__MUSL__ -DDMLC_USE_FOPEN64=0\nCPPFLAGS=-D__MUSL__ -DDMLC_USE_FOPEN64=0\nCFLAGS += -flax-vector-conversions\nLDFLAGS+=-fPIC\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(crayon.enabled = TRUE, Ncpus = ${NCPUS}, future.globals.onReference = NULL, repos = structure(c(CRAN = \"https://cloud.r-project.org\", INLA = \"https://inla.r-inla-download.org/R/stable\")))" > /root/.Rprofile'
RUN bash -c 'echo -e "PKG_SYSREQS=TRUE\nPKG_SYSREQS_VERBOSE=TRUE" > ~/.Renviron'

### INSTALL R

ARG OS_VERSION
RUN export OS_VERSION_TRIM=$(echo $OS_VERSION | tr -d '.') && \
  curl -v -O "https://devxy-r-builds.fsn1.your-objectstorage.com/alpine${OS_VERSION_TRIM}/r-${R_VERSION}_1_$(arch).apk"
RUN apk add --allow-untrusted "r-${R_VERSION}_1_$(arch).apk" && rm "r-${R_VERSION}_1_$(arch).apk"

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))'

# 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

# encoding -> https://github.com/r-hub/r-minimal/blob/fa9c117313e05860a0d20b113e5f794c51be8b8c/Dockerfile#L84
RUN sed -i 's/,//g' /opt/R/${R_VERSION}/lib/R/library/utils/iconvlist

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

CMD ["/bin/bash"]
