ARG MESHROOM_VERSION
ARG AV_VERSION
ARG CUDA_VERSION
ARG ROCKY_VERSION
FROM alicevision/meshroom-deps:${MESHROOM_VERSION}-av${AV_VERSION}-rocky${ROCKY_VERSION}-cuda${CUDA_VERSION}
LABEL maintainer="AliceVision Team alicevision-team@googlegroups.com"

# Execute with nvidia docker (https://github.com/nvidia/nvidia-docker/wiki/Installation-(version-2.0))
# docker run -it --runtime nvidia -p 2222:22 --name meshroom -v</path/to/your/data>:/data alicevision/meshroom:develop-av2.2.8.develop-ubuntu20.04-cuda11.0
# ssh -p 2222 -X root@<docker host> /opt/Meshroom_bundle/Meshroom # Password is 'meshroom'

RUN dnf install -y patchelf

ENV MESHROOM_DEV=/opt/Meshroom \
    MESHROOM_BUILD=/tmp/Meshroom_build \
    MESHROOM_BUNDLE=/opt/Meshroom_bundle \
    AV_INSTALL=/opt/AliceVision_install \
    QT_DIR=/opt/Qt/6.8.3/gcc_64 \
    PATH="${PATH}:${MESHROOM_BUNDLE}"

COPY *.txt *.md *.py ${MESHROOM_DEV}/
COPY ./docs ${MESHROOM_DEV}/docs
COPY ./meshroom ${MESHROOM_DEV}/meshroom
COPY ./tests ${MESHROOM_DEV}/tests
COPY ./bin ${MESHROOM_DEV}/bin

WORKDIR ${MESHROOM_DEV}

# Generate the exe for Meshroom and clean-up the bundle folder
RUN python setup.py install_exe -d "${MESHROOM_BUNDLE}" && \
        find ${MESHROOM_BUNDLE} -name "*Qt6Web*" -delete && \
        find ${MESHROOM_BUNDLE} -name "*Qt6Designer*" -delete && \
        rm -rf ${MESHROOM_BUNDLE}/lib/PySide6/typesystems/ \
                ${MESHROOM_BUNDLE}/lib/PySide6/examples/ \
                ${MESHROOM_BUNDLE}/lib/PySide6/include/ \
                ${MESHROOM_BUNDLE}/lib/PySide6/Qt/translations/ \
                ${MESHROOM_BUNDLE}/lib/PySide6/Qt/resources/ \
                ${MESHROOM_BUNDLE}/lib/PySide6/QtWeb* \
                ${MESHROOM_BUNDLE}/lib/PySide6/rcc \
                ${MESHROOM_BUNDLE}/lib/PySide6/designer

WORKDIR ${MESHROOM_BUILD}

# Move the bundled installation of AliceVision into Meshroom's bundle
RUN mkdir ${MESHROOM_BUNDLE}/aliceVision && \
    mv /opt/AliceVision_bundle/* ${MESHROOM_BUNDLE}/aliceVision

# Build Meshroom plugins
RUN cmake "${MESHROOM_DEV}" -DALICEVISION_ROOT="${AV_INSTALL}" -DCMAKE_INSTALL_PREFIX="${MESHROOM_BUNDLE}/qtPlugins"
RUN make "-j$(nproc)" QtAliceVision
RUN make "-j$(nproc)" && \
	rm -rf "${MESHROOM_BUILD}" "${MESHROOM_DEV}" \
		${MESHROOM_BUNDLE}/aliceVision/share/doc \
		${MESHROOM_BUNDLE}/aliceVision/share/eigen3 \
		${MESHROOM_BUNDLE}/aliceVision/share/fonts \
		${MESHROOM_BUNDLE}/aliceVision/share/lemon \
		${MESHROOM_BUNDLE}/aliceVision/share/libraw \
		${MESHROOM_BUNDLE}/aliceVision/share/man/ \
		aliceVision/share/pkgconfig

# PySide6: copy missing libQt63DQuickScene3D.so along with its dependencies to avoid runtime issues
RUN cp ${QT_DIR}/lib/libQt63DQuickScene3D.so.6.8.3 ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/QtQuick/Dialogs && \
    mv ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/QtQuick/Dialogs/libQt63DQuickScene3D.so.6.8.3 ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/QtQuick/Dialogs/libQt63DQuickScene3D.so.6 && \
    cp ${MESHROOM_BUNDLE}/lib/PySide6/Qt/lib/libQt6Concurrent.so.6 ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/QtQuick/Dialogs && \
    cp ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/Qt3D/Animation/libQt63DAnimation.so.6 ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/QtQuick/Dialogs && \
    cp ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/Qt3D/Core/libQt63DCore.so.6 ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/QtQuick/Dialogs && \
    cp ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/Qt3D/Input/libQt63DInput.so.6 ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/QtQuick/Dialogs && \
    cp ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/Qt3D/Logic/libQt63DLogic.so.6 ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/QtQuick/Dialogs && \
    cp ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/Qt3D/Render/libQt63DRender.so.6 ${MESHROOM_BUNDLE}/lib/PySide6/Qt/qml/QtQuick/Dialogs

# Copy libOpenGL in the bundle: needed by QtAliceVision as a side effect of a Qt6 bug
RUN cp /usr/lib64/libOpenGL.so.0.0.0 ${MESHROOM_BUNDLE}/lib
RUN mv ${MESHROOM_BUNDLE}/lib/libOpenGL.so.0.0.0 ${MESHROOM_BUNDLE}/lib/libOpenGL.so.0

# Enable SSH X11 forwarding, needed when the Docker image
# is run on a remote machine
RUN dnf install -y openssh openssh-clients openssh-server xorg-x11-xauth
RUN systemctl enable sshd && \
    mkdir -p /run/sshd && \
    ssh-keygen -A

RUN sed -i "s/^.*X11Forwarding.*$/X11Forwarding yes/; s/^.*X11UseLocalhost.*$/X11UseLocalhost no/; s/^.*PermitRootLogin prohibit-password/PermitRootLogin yes/; s/^.*X11UseLocalhost.*/X11UseLocalhost no/;" /etc/ssh/sshd_config
RUN echo "root:meshroom" | chpasswd

WORKDIR /root

EXPOSE 22
CMD ["/usr/sbin/sshd", "-D"]

