# Use Ubuntu 20.04 LTS as a base image
FROM ubuntu:20.04

# Avoid prompts from apt
ENV DEBIAN_FRONTEND=noninteractive

# Install necessary dependencies for adding repos and keys
RUN apt-get update && apt-get install -y --no-install-recommends \
    software-properties-common \
    dirmngr \
    gpg-agent \
    wget \
    curl

# Add the R repository with public key
RUN wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | gpg --dearmor -o /usr/share/keyrings/cloud-r-project.gpg && \
    echo "deb [signed-by=/usr/share/keyrings/cloud-r-project.gpg] https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/" > /etc/apt/sources.list.d/cran40.list

# Update and install R and other necessary packages
RUN apt-get update && apt-get install -y \
    r-base \
    libcurl4-openssl-dev \
    libssl-dev \
    libxml2-dev \
    openjdk-11-jdk

# Clean up to reduce image size
RUN apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Set JAVA_HOME environment
ENV JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64
ENV PATH=$JAVA_HOME/bin:$PATH

# Install the H2O package from within R
RUN R -e "install.packages('h2o', dependencies=TRUE, repos='http://cran.rstudio.com/')"

# Copy your scripts and data into the Docker image
COPY . /usr/local/src/scripts
WORKDIR /usr/local/src/scripts

# Set up default command to run when starting the container
CMD ["R"]
