# syntax=docker/dockerfile:1.7
#
# Base + install command follow SideFX's own docker/Ubuntu/Dockerfile
# (shipped inside the Houdini tarball). Ubuntu 22.04 matters here, not just
# as a default: gccXX.X-tagged tarballs need a libstdc++ new enough to match,
# and Ubuntu's base image keeps libstdc++6 updated while RHEL/Rocky pin their
# system toolchain for the OS lifetime.
FROM docker.io/library/ubuntu:22.04 AS base

ARG HOUDINI_TARBALL=houdini-linux.tar.gz
# Plain YYYY-MM-DD, no "SideFX-" prefix (that prefix is for the newer,
# unrelated houdini_installer launcher tool). Current EULA date: 2021-10-13,
# see https://www.sidefx.com/legal/license-agreement/
ARG HOUDINI_EULA_DATE=2021-10-13
# Github release tag at https://github.com/sideeffects/SideFXLabs/releases —
# pick the newest tag whose target branch is "Development" and whose bundled
# SideFXLabs.json "enable" range covers this image's Houdini version.
ARG SIDEFXLABS_VERSION=22.0.440

RUN apt-get update && apt-get install -y --no-install-recommends \
        sudo procps ca-certificates \
        bc fontconfig wget libasound2 libgl1 libglu1 libglu1-mesa libglx0 \
        libegl1 libatomic1 libxkbfile1 libxcb-cursor0 \
        libice6 libnss3 libopengl0 libpci3 libsm6 libx11-6 libx11-xcb1 \
        libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 \
        libxcb-render-util0 libxcb-render0 libxcb-shape0 libxcb-shm0 \
        libxcb-sync1 libxcb-util1 libxcb-xfixes0 libxcb-xinerama0 \
        libxcb-xkb1 libxcb1 libxcomposite1 libxcursor1 libxdamage1 \
        libxext6 libxi6 libxkbcommon-x11-0 libxkbcommon0 libxrandr2 \
        libxrender1 libxss1 libxt6 libxtst6 && \
    rm -rf /var/lib/apt/lists/*

WORKDIR /tmp/houdini
COPY ${HOUDINI_TARBALL} ./houdini.tar.gz

# Flags match SideFX's own Dockerfile verbatim (space before the date, no
# '='). The installer creates /opt/hfs<major.minor> (e.g. /opt/hfs22.0), not
# a generic /opt/hfs — add our own stable symlink so houdini-run and the
# profile.d snippet don't need to know the version.
RUN tar xzf houdini.tar.gz --strip-components=1 && \
    ./houdini.install --auto-install --accept-EULA ${HOUDINI_EULA_DATE} && \
    ln -s "$(find /opt -maxdepth 1 -type d -name 'hfs[0-9]*' | sort -V | tail -1)" /opt/hfs && \
    rm -rf /tmp/houdini

COPY houdini-run /usr/local/bin/houdini-run
RUN chmod +x /usr/local/bin/houdini-run && \
    printf 'if [ -d /opt/hfs ]; then _p="$PWD"; cd /opt/hfs && . ./houdini_setup >/dev/null 2>&1; cd "$_p"; fi\n' \
        > /etc/profile.d/houdini.sh

# SideFX Labs, baked in rather than installed through Houdini's own in-app
# package installer. That installer writes into $HOUDINI_USER_PREF_DIR
# (under $HOME), which distrobox bind-mounts from the host over whatever the
# image ships there — so anything baked in under $HOME would just be
# shadowed at runtime, and doing it live means every container needs its own
# git-clone/download at first launch (this is also almost certainly the
# actual source of the "no such file or directory": the installer shells out
# to `git`, which this image never installed).
# $HFS/packages is a package-search path Houdini scans unconditionally,
# independent of $HOME (see `houdini/ref/plugins.html` in SideFX's docs), so
# drop the official package descriptor + the Labs tree there untouched: the
# descriptor's default `$HOUDINI_PACKAGE_PATH/SideFXLabs22.0` already
# resolves correctly since HOUDINI_PACKAGE_PATH is just "wherever this json
# file lives".
RUN mkdir -p /opt/hfs/packages/SideFXLabs22.0 && \
    wget -qO /tmp/sidefxlabs.tar.gz \
        "https://github.com/sideeffects/SideFXLabs/archive/refs/tags/${SIDEFXLABS_VERSION}.tar.gz" && \
    tar xzf /tmp/sidefxlabs.tar.gz --strip-components=1 -C /opt/hfs/packages/SideFXLabs22.0 && \
    cp /opt/hfs/packages/SideFXLabs22.0/SideFXLabs.json /opt/hfs/packages/SideFXLabs22.0.json && \
    rm /tmp/sidefxlabs.tar.gz

FROM base AS amd
RUN apt-get update && apt-get install -y --no-install-recommends \
        libgl1-mesa-dri libglx-mesa0 mesa-vulkan-drivers && \
    rm -rf /var/lib/apt/lists/*

FROM base AS nvidia
# GL/Vulkan/CUDA come from the host driver via `distrobox --nvidia`.
