FROM ubuntu:20.04
# See https://askubuntu.com/questions/909277/avoiding-user-interaction-with-tzdata-when-installing-certbot-in-a-docker-contai
ENV DEBIAN_FRONTEND=noninteractive
ENV LANG=C.UTF-8 LC_ALL=C.UTF-8
ENV PATH /opt/conda/bin:$PATH

RUN apt-get update --fix-missing && apt-get install -y wget bzip2 ca-certificates \
    libglib2.0-0 libxext6 libsm6 libxrender1 \
    git mercurial subversion

RUN wget --quiet https://repo.anaconda.com/miniconda/Miniconda3-py39_4.10.3-Linux-x86_64.sh -O ~/anaconda.sh && \
    /bin/bash ~/anaconda.sh -b -p /opt/conda && \
    rm ~/anaconda.sh && \
    ln -s /opt/conda/etc/profile.d/conda.sh /etc/profile.d/conda.sh && \
    echo ". /opt/conda/etc/profile.d/conda.sh" >> ~/.bashrc && \
    echo "conda activate base" >> ~/.bashrc

RUN apt-get install -y curl grep sed zip unzip dpkg netcat iproute2 dnsutils vim build-essential cmake && \
    TINI_VERSION=0.19.0 && \
    curl -L "https://github.com/krallin/tini/releases/download/v${TINI_VERSION}/tini_${TINI_VERSION}.deb" > tini.deb && \
    dpkg -i tini.deb && \
    rm tini.deb && \
    apt-get clean

SHELL ["/bin/bash", "-c"]

WORKDIR /relnet
ADD ./environment.yml /relnet/environment.yml

RUN conda env update --file environment.yml

RUN source activate relnet-cenv && conda install pytorch==1.11.0 cpuonly -c pytorch
RUN source activate relnet-cenv && conda install pyg -c pyg
RUN apt-get update && apt-get install -y --no-install-recommends libpcap-dev libglpk-dev cmake g++ make libpcap0.8 libglpk40 && apt-get clean

WORKDIR /relnet
ADD ./packaged_deps /relnet/packaged_deps

RUN cd /relnet/packaged_deps/ncode-common/external && git clone --recursive https://github.com/google/googletest.git && cd googletest && git reset --hard d62d6c6556d96dda924382547c54a4b3afedb22c
RUN cd /relnet/packaged_deps/ncode-common && mkdir -p build && cd build && cmake -DNCODE_DISABLE_TESTS=YES -DNCODE_DISABLE_BENCHMARKS=YES .. && make && make install

RUN cd /relnet/packaged_deps/tm-gen/external && git clone --recursive https://github.com/gflags/gflags.git && cd gflags && git reset --hard 660603a3df1c400437260b51c55490a046a12e8a && \
    cd /relnet/packaged_deps/tm-gen/external && git clone --recursive https://github.com/google/googletest && cd googletest && git reset --hard e82d320567a45db1a999f9109f2b9a733bc59bb1

#RUN cd /relnet/packaged_deps/tm-gen && mkdir -p build && cd build && cmake -DTM_GEN_DISABLE_TESTS=YES -DTM_GEN_DISABLE_BENCHMARKS=YES .. && make && make install
RUN cd /relnet/packaged_deps/tm-gen && mkdir -p build && cd build && cmake -DTM_GEN_BASE_DEBUG=YES -DTM_GEN_DISABLE_TESTS=YES -DTM_GEN_DISABLE_BENCHMARKS=YES .. && make && make install

ENV PATH /opt/conda/envs/relnet-cenv/bin:$PATH
ENV LD_LIBRARY_PATH /usr/local/lib/:$LD_LIBRARY_PATH
ENV RELNET_DEVICE_PLACEMENT CPU

ENTRYPOINT [ "/usr/bin/tini", "--" ]