FROM ghcr.io/mamba-org/micromamba:1.4-jammy

ARG DEBIAN_FRONTEND=noninteractive

ENV LC_ALL=C.UTF-8
ENV LANG=C.UTF-8

# Set the build arg to ENV so they are available at runtime.
ENV APP_FOLDER="/apps/openfractal"
ENV ENV_NAME="openfractal"
ENV ENV_FILE="env.yml"

# Get the root for admin tasks
USER root

# Make a ready to use apps folder
RUN mkdir -p /apps && chown $MAMBA_USER:$MAMBA_USER /apps

# Custom aliases
RUN echo "alias m=\"/usr/bin/micromamba\"" >> /etc/bash.bashrc && \
    echo "alias ma=\"/usr/bin/micromamba activate\"" >> /etc/bash.bashrc && \
    echo "alias ccc=\"clear\"" >> /etc/bash.bashrc && \
    echo "alias c=\"cd ..\"" >> /etc/bash.bashrc && \
    echo "alias p=\"pwd\"" >> /etc/bash.bashrc

# Install system-wide packages with `apt`
RUN apt update --fix-missing && \
    apt install --no-install-recommends -y \
    wget bzip2 ca-certificates libglib2.0-0 libxext6 \
    htop libsm6 libxrender1 git gettext-base tzdata \
    libxml2 libaio-dev nano && \
    apt clean

# Switch back to regular user
USER $MAMBA_USER

RUN mkdir -p $MAMBA_ROOT_PREFIX

# Configure base micromamba
RUN micromamba config append channels conda-forge && \
    micromamba config set show_banner false && \
    micromamba config set auto_activate_base true && \
    micromamba config set channel_priority strict && \
    micromamba config set repodata_use_zst true

# Copy env.yml file
COPY --chown=$MAMBA_USER:$MAMBA_USER ./$ENV_FILE /tmp/$ENV_FILE

# Install openfractal deps
RUN micromamba create --yes --name $ENV_NAME -f /tmp/$ENV_FILE && micromamba clean --all --yes

# Activate base during the image build
ARG MAMBA_DOCKERFILE_ACTIVATE=1

# Copy the entire app git repo to the container
COPY --chown=$MAMBA_USER:$MAMBA_USER . $APP_FOLDER

# Install the project application with `pip`
RUN pip install -e $APP_FOLDER

# Set the working directory
WORKDIR $APP_FOLDER

# From upstream image
SHELL ["/usr/local/bin/_dockerfile_shell.sh"]
ENTRYPOINT ["/usr/local/bin/_entrypoint.sh"]

CMD ["bash"]
