ARG UBUNTU_VERSION=20.04
ARG LLVM_VERSION=12

FROM mcr.microsoft.com/vscode/devcontainers/base:0-ubuntu-${UBUNTU_VERSION}
# FROM ubuntu:$UBUNTU_VERSION

# https://docs.docker.com/engine/reference/builder/#understand-how-arg-and-from-interact
ARG LLVM_VERSION

ENV DEBIAN_FRONTEND=noninteractive

# install llvm
RUN apt -q update \
    && apt-get install -y --no-install-recommends ca-certificates software-properties-common curl gnupg2 libopenexr-dev sudo\
    && apt-get install -y --no-install-recommends git ssh clang-format autoconf cmake make ninja-build gcc g++ build-essential libtbb-dev libglfw3-dev libzip-dev libembree-dev libtool gfortran llvm-$LLVM_VERSION-dev clang-$LLVM_VERSION libomp-$LLVM_VERSION-dev libeigen3-dev libboost-dev python3-dev python3-pip\
    && apt-get install -y --no-install-recommends xorg-dev libglu1-mesa-dev \
    && python3 -m pip install --upgrade pip setuptools \
    && python3 -m pip install lit \
    && apt-get install -y --no-install-recommends python-is-python3

#install WLLVM
RUN pip3 install wllvm && pip3 install "pybind11[global]" \
    && pip3 install torch==1.10.0+cpu torchvision==0.11.1+cpu torchaudio==0.10.0+cpu -f https://download.pytorch.org/whl/cpu/torch_stable.html\
    && pip3 install OpenEXR scikit-image pytest

RUN ln -s /usr/bin/clang-12 /usr/bin/clang \
    && ln -s /usr/bin/clang++-12 /usr/bin/clang++ \
    && ln -s /usr/bin/llvm-link-12 /usr/bin/llvm-link \
    && ln -s /usr/bin/opt-12 /usr/bin/opt

# build embree
RUN git clone https://github.com/embree/embree.git \
    && cd embree \
    && mkdir -p build \
    && cd build \
    && cmake -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_COMPILER=gcc -DEMBREE_ISPC_SUPPORT=OFF .. \
    && sudo make install -j8

RUN pip install opencv-python==4.3.0.38
RUN pip install gin-config
RUN git clone https://github.com/libigl/libigl-python-bindings.git \
    && cd libigl-python-bindings \
    && python setup.py install

# build Instant Mesh
RUN git clone --recursive https://github.com/wjakob/instant-meshes.git \
    && cd instant-meshes && mkdir build && cd build \
    && cmake .. && make -j \
    && mv 'Instant Meshes' instant_meshes

RUN git clone https://github.com/MarcoAttene/MeshFix-V2.1.git \
    && cd MeshFix-V2.1 && mkdir build && cd build \
    && cmake .. && make -j

# build enzyme
RUN git clone https://github.com/wsmoses/Enzyme.git && cd Enzyme \
    && git checkout v0.0.30 \
    && cd enzyme \
    && mkdir build && cd build \
    && CC=clang-12 CXX=clang++-12 cmake -G Ninja .. -DLLVM_DIR=/usr/lib/llvm-12/lib/cmake/llvm/ \
    && ninja \
    && ninja install


ENV LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib"
ENV PATH="/instant-meshes/build:/MeshFix-V2.1/bin64:$PATH"
ENV PYTHONPATH="/workspaces/psdr-enzyme/:/workspaces/psdr-enzyme/build_enzyme:/libigl-python-bindings/build/lib.linux-x86_64-3.8"