#syntax=docker/dockerfile:1.3
FROM mambaorg/micromamba:0.15.2@sha256:e74f9ba39e549027dd2fb376afbf3f4f5f603747d684487ab2a26445f36ebadd

ENV FACET_HOME /home/facet

# Need to run apt-get as root
#USER root

# Allow docker to cache packages,
# but we will specify cache mounts so they don't get included in the final image
#RUN rm -f /etc/apt/apt.conf.d/docker-clean; echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache

## get unrar lib for debian
## unrar works for *some* of the iccat rar archives
## better to use unar, the CLI version of Mac's unarchiver
#RUN --mount=type=cache,id=apt-cache,target=/var/cache/apt --mount=type=cache,id=apt-lib,target=/var/lib/apt \
#  echo "deb http://deb.debian.org/debian buster buster-updates main contrib non-free" | tee /etc/apt/sources.list \
#  && echo "deb http://security.debian.org/debian-security buster/updates main contrib non-free" | tee -a /etc/apt/sources.list \
#  && apt-get update \
  #&& apt-get install -y libnetcdf-* \
#  && apt-get install -y git
  #&& apt-get install -y libssl-dev \
  #&& apt-get install -y libudunits2-dev \
  #&& apt-get install -y libgdal-dev

# Switching back to micromamba user with a uid of 1000
USER ${USER}

# cache anything in /opt/conda/pkgs so we can build faster locally,
# but do not include them in the final build
RUN --mount=type=cache,id=conda-etag,target=/opt/conda/pkgs,uid=1000,gid=1000 \
  # we only need the mamba environment.yml for install,
  # so we can mount it rather than copy it into the image
  --mount=type=bind,source=./pipelines/etag/environment.yml,target=/tmp/environment.yml \
  micromamba install -y -n base -f /tmp/environment.yml

## Install extra R packages using requirements.R
## Copy requirements.R to container directory /tmp
COPY ./pipelines/etag/R/DockerConfig/requirements.R /home/$USER/requirements.R
# install required R packages on container
RUN Rscript /home/$USER/requirements.R

WORKDIR $FACET_HOME

## custom R functions for this step of pipeline
COPY ./pipelines/etag/R/ ./R/
COPY ./tools/pseudoabs/R/* ./R/
COPY ./R/ ./R/functions/

RUN ls -lR ./R/ \
  && chmod +x ./R/*.r \
  && chmod +x ./R/*.R \
  && chmod +x ./R/functions/*.r \
  && chmod +x ./R/functions/*.R

#COPY ./tools/dagster/facet_shared/ ./facet_shared/

RUN git clone https://github.com/camrinbraun/tags2etuff.git ./R/tags2etuff/


#CMD ["dagster", "api", "grpc", "-h", "0.0.0.0", "-p", "4000", "-f", "/home/facet/py/iccat.py"]
