FROM aisiuk/inspect-computer-tool:latest

ENV DEBIAN_FRONTEND=noninteractive
ENV DEBIAN_PRIORITY=high

USER root
RUN apt-get update && \
    apt-get install -y  \
      curl \                    
      python3-tk \
      python3-dev \
      python3-pyatspi \
      at-spi2-core \
      wmctrl \
      socat \
      unzip \
      # needed for null sound device
      alsa-utils \
      # Some requirements.txt entries require git 
      git && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# Configure ALSA to use the null device
RUN echo 'pcm.!default { type null } ctl.!default { type null }' > /etc/asound.conf

# install the apps
RUN apt-get update && \
    apt-get install -y \
      nano \
      libreoffice \
      thunderbird \
      vlc \
      gimp && \
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# install required fonts
RUN mkdir -p /usr/share/fonts/truetype/custom && \
    curl -sSL -o /tmp/fonts.tar.gz "https://raw.githubusercontent.com/epatey/fonts/main/fonts.tar.gz" && \
    tar -xzf /tmp/fonts.tar.gz -C /usr/share/fonts/truetype/custom/ --strip-components=1 && \
    rm -f /tmp/fonts.tar.gz

# Prepare the /tmp/osworld directory
# it will be populated with two sources:
# 1. a sparse clone of OSWorld.git that includes `desktop_env/evaluators` and `requirements.txt`
# 2. inspect_eval specific code (essentially a cli) that will be added to `desktop_env`
#
# /tmp/osworld
# ├── requirements.txt <from osworld repo>
# └── desktop_env
#     ├── evaluators <from osworld repo>
#     │   ├── getters
#     │   │   └── custom getter modules called dynamically by the controller
#     │   └── metrics
#     │       └── custom metrics modules called dynamically by the controller
#     ├── cli.py <from inspect_eval>
#     └── private support files (e.g. _controller.py, _env.py, _example.py) <from inspect_eval>

#1 - the sparse clone
# the explicit install of pymupdf is required since 1.25.3 no longer builds on arm64 machines.
# https://github.com/pymupdf/PyMuPDF/issues/4278
# Lol. A more robust approach (e.g. pip-tools) is called for.
RUN git clone --depth 1 --filter=blob:none --sparse https://github.com/xlang-ai/OSWorld.git /tmp/osworld && \
  cd /tmp/osworld && \
  git sparse-checkout init && \
  git sparse-checkout set desktop_env/evaluators && \
  git sparse-checkout add /requirements.txt && \
  rm -rf /tmp/osworld/.git/ && \
  pip install pymupdf==1.25.2 && \
  pip install -r requirements.txt

#2 - the inspect_eval code
COPY code/*.py /tmp/osworld/desktop_env/
RUN chmod -R 777 /tmp/osworld
ENV PYTHONPATH="/tmp/osworld"

ADD --chown=$USERNAME:$USERNAME home_dir/ $HOME/

# There may be a better way, but in order to hit the ENTRYPOINT properly, we need to
# go back to its expected state
USER user
WORKDIR $HOME
    
# disable profile conversion prompting
RUN mkdir -p $HOME/.config/GIMP/2.10 && echo '(color-profile-policy convert)' >> $HOME/.config/GIMP/2.10/gimprc
