# syntax=docker/dockerfile:1.7
#
# Base image + dependency list copied from zelikos/davincibox
# (https://github.com/zelikos/davincibox), the most battle-tested distrobox
# image for DaVinci Resolve on Linux. One deliberate difference from
# upstream: Resolve itself is baked in at build time here (this repo's
# convention: build once, push, no setup step after `distrobox create`)
# instead of being installed into the box afterwards by their
# `setup-davinci` script.
#
# The silent-install invocation and the patchelf fix below are copied
# verbatim from davincibox's `system_files/usr/bin/setup-davinci` script,
# cross-confirmed against fat-tire/resolve's Dockerfile
# (https://github.com/fat-tire/resolve) which drives the same installer with
# the same `-i -a -y` flags at Docker build time — not guessed.
FROM quay.io/fedora/fedora-toolbox:44 AS base

LABEL com.github.containers.toolbox="true" \
      usage="This image is meant to be used with the distrobox command" \
      summary="DaVinci Resolve, baked in, for distrobox"

# davincibox's own dependency list, proven against real Resolve installs
# (Fedora Atomic, Bluefin, Bazzite - see davinci/CLAUDE.md).
COPY davinci-dependencies /davinci-dependencies
RUN dnf -y update && \
    grep -v '^#' /davinci-dependencies | xargs dnf -y install && \
    rm /davinci-dependencies && \
    dnf clean all

# The .run installer as downloaded from Blackmagic's site and unzipped
# (DaVinci_Resolve_<version>_Linux.run, or the _Studio_ variant), placed
# next to this Dockerfile.
ARG RESOLVE_INSTALLER=DaVinci_Resolve_Linux.run
COPY ${RESOLVE_INSTALLER} /tmp/resolve.run

# `-i -a -y`: silent install, accept all, assume yes - same flags used by
# davincibox's setup-davinci and by fat-tire/resolve's Dockerfile.
# QT_QPA_PLATFORM=minimal + SKIP_PACKAGE_CHECK=1: needed with no display
# attached, from davincibox (see zelikos/davincibox#35).
RUN cd /tmp && \
    chmod +x resolve.run && \
    ./resolve.run --appimage-extract >/dev/null && \
    cd squashfs-root && \
    QT_QPA_PLATFORM=minimal SKIP_PACKAGE_CHECK=1 ./AppRun -i -a -y && \
    cd /tmp && rm -rf resolve.run squashfs-root

# Resolve's own binaries dlopen these libs without a DT_NEEDED entry
# (avoids the LD_PRELOAD workaround described at
# https://www.reddit.com/r/voidlinux/comments/12g71x0/comment/l2cwo27/).
# Same fix, same (full-path) argument form, as davincibox's setup-davinci.
RUN find /opt/resolve/bin -executable -type f -exec patchelf \
        --add-needed /usr/lib64/libglib-2.0.so.0 \
        --add-needed /usr/lib64/libgdk_pixbuf-2.0.so.0 \
        --add-needed /usr/lib64/libgio-2.0.so.0 \
        --add-needed /usr/lib64/libgmodule-2.0.so.0 {} \;

COPY davinci-run /usr/local/bin/davinci-run
RUN chmod +x /usr/local/bin/davinci-run

FROM base AS amd
# Covers AMD (ROCm) and Intel (intel-compute-runtime) in one image, like
# davincibox's "-opencl" variant. mesa-libOpenCL (rusticl) is removed first:
# it breaks ROCm outright (zelikos/davincibox#173).
RUN dnf -y remove mesa-libOpenCL && \
    dnf -y install intel-compute-runtime rocm-opencl && \
    dnf clean all

FROM base AS nvidia
# OpenCL/CUDA/NVENC come from the host driver, not baked in.
ENV NVIDIA_VISIBLE_DEVICES=all
ENV NVIDIA_DRIVER_CAPABILITIES=all
