# -*- Dockerfile -*-

FROM debian:10

# -------------------------------------------------------------------------
# This is going to use P3M on x86_64
COPY tools/build/linux/rig.gpg /etc/apt/trusted.gpg.d/rig.gpg
RUN echo "deb http://rig.r-pkg.org/deb rig main" > \
        /etc/apt/sources.list.d/rig.list && \
    export DEBIAN_FRONTEND=noninteractive && \
    apt-get update -y && \
    apt-get install -y r-rig && \
    rig add release

# -------------------------------------------------------------------------
# Only copy DESCRIPTION, so Docker can cache the dependency install
RUN mkdir /root/bigrquerystorage
COPY DESCRIPTION /root/bigrquerystorage/
WORKDIR /root/bigrquerystorage
# No upgrade, to always install binary packages, in case P3M is behind
RUN R -q -e 'pak::local_install_deps(upgrade = FALSE)'

# -------------------------------------------------------------------------
# FIXME: We don't need this once grpc is in the system requirements db
RUN echo "deb https://deb.debian.org/debian buster-backports main" >> \
    /etc/apt/sources.list.d/backports.list && \
    apt-get update
RUN apt-get update && \
    apt-get install -y 'libgrpc\+\+-dev'/buster-backports \
                       protobuf-compiler-grpc/buster-backports \
                       libprotobuf-dev/buster-backports \
                       protobuf-compiler/buster-backports pkg-config

# -------------------------------------------------------------------------
# Call R CMD build first, so we get rid of Makevars, .o, .so, etc.
# Change file ownership, otherwise git complains
# Clean up thoroughly, in case the cleanup script is not perfect.
RUN apt-get install -y git
COPY . /root/bigrquerystorage
RUN chown root:root -R .
RUN git clean -fdx .

# -------------------------------------------------------------------------
WORKDIR /root
RUN R CMD build bigrquerystorage
RUN R CMD INSTALL bigrquerystorage_*.tar.gz
