# Run the following commands in order:
#
# docker build --tag circuit_training:ci -f tools/docker/ubuntu_ci tools/docker/
#
# Test that everything worked:
# docker run -it --rm -v $(pwd):/workspace --workdir /workspace circuit_training:ci bash
# python3.9 -m circuit_training.environment.environment_test

FROM ubuntu:20.04

LABEL maintainer="tobyboyd@google.com"

ARG python_version="python3.9 python3.10 python3.11"
ARG dreamplace_version="dreamplace"
ARG APT_COMMAND="apt-get -o Acquire::Retries=3 -y"

# Installs basics including add-apt.
RUN ${APT_COMMAND} update && ${APT_COMMAND} install -y --no-install-recommends \
        software-properties-common \
        curl \
        less

# Adds repository to pull versions of python from.
RUN add-apt-repository ppa:deadsnakes/ppa

# Stops tzdata from asking about timezones and blocking install on user input.
ARG DEBIAN_FRONTEND=noninteractive
ENV TZ=America/Los_Angeles

# Installs various versions of python and then cleans up apt.
RUN ${APT_COMMAND} update && ${APT_COMMAND} install -y --no-install-recommends \
        flex \
        libcairo2-dev \
        libboost-all-dev \
        python3.9-dev \
        python3.10-dev \
        python3.11-dev \
        python3.9-distutils \
        python3.10-distutils \
        python3.11-distutils \
        && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Downloads the placement cost utility binary nto /usr/local/bin.
RUN curl https://storage.googleapis.com/rl-infra-public/circuit-training/placement_cost/plc_wrapper_main \
     -o  /usr/local/bin/plc_wrapper_main

RUN chmod 555 /usr/local/bin/plc_wrapper_main

RUN curl -O https://bootstrap.pypa.io/get-pip.py

ARG pip_dependencies=' \
      pytest \
      tox'

# No need to install tf-agents as that should be part of the test setup.
# Doing it for now to test.
RUN for python in ${python_version}; do \
    $python get-pip.py && \
    $python -mpip --no-cache-dir install $pip_dependencies; \
  done
RUN rm get-pip.py

# Installs pre-compiled DREAMPlace for each version of python.
RUN for python in ${python_version}; do \
    mkdir -p /dreamplace_${python} && \
    curl https://storage.googleapis.com/rl-infra-public/circuit-training/dreamplace/${dreamplace_version}_${python}.tar.gz -o /dreamplace_${python}/dreamplace.tar.gz && \
    tar xzf /dreamplace_${python}/dreamplace.tar.gz -C /dreamplace_${python}/; \
  done


CMD ["/bin/bash"]