FROM ubuntu:22.04

ARG DEBIAN_FRONTEND=noninteractive \
    OSVER=ubuntu2204 \
    LOCAL_USER_ID=1000 \
    LOCAL_GROUP_ID=1000 \
    FFMPEG_DLL_REPO=https://github.com/rigaya/build_scripts.git \
    FFMPEG_DLL_REF=master \
    BUILD_SCRIPTS_DIR=/opt/build_scripts \
    FFMPEG_PREFIX=/opt/build_scripts/ffmpeg_dll/build_ffmpeg_dll/x64/build \
    CUDA_VER_MAJOR=11 \
    CUDA_VER_MINOR=7 \
    CUDA_DEB_URL=https://developer.download.nvidia.com/compute/cuda/11.7.0/local_installers/cuda-repo-ubuntu2204-11-7-local_11.7.0-515.43.04-1_amd64.deb  \
    CUDA_DEB_NAME=cuda-repo-ubuntu2204-11-7-local_11.7.0-515.43.04-1_amd64.deb \
    CUDA_GPG_PATH=/var/cuda-repo-ubuntu2204-11-7-local

RUN apt update \
    && apt-get install -y \
      wget \
      curl \
      git \
      ca-certificates \
      autogen \
      autoconf \
      automake \
      libtool \
      autopoint \
      gettext \
      make \
      cmake \
      pkg-config \
      gperf \
      nasm \
      yasm \
      patch \
      xz-utils \
      p7zip-full \
      unzip \
      bzip2 \
      python3 \
      python3-pip \
      build-essential \
      libx11-dev \
      openssl \
      libssl-dev \
    && wget -q https://developer.download.nvidia.com/compute/cuda/repos/${OSVER}/x86_64/cuda-${OSVER}.pin \
    && mv cuda-${OSVER}.pin /etc/apt/preferences.d/cuda-repository-pin-600 \
    && wget -q ${CUDA_DEB_URL} \
    && dpkg -i ${CUDA_DEB_NAME} \
    && cp ${CUDA_GPG_PATH}/cuda-*-keyring.gpg /usr/share/keyrings/ \
    && apt-get update \
    && apt-get -y install cuda-compiler-${CUDA_VER_MAJOR}-${CUDA_VER_MINOR} cuda-cudart-dev-${CUDA_VER_MAJOR}-${CUDA_VER_MINOR} cuda-driver-dev-${CUDA_VER_MAJOR}-${CUDA_VER_MINOR} cuda-nvrtc-dev-${CUDA_VER_MAJOR}-${CUDA_VER_MINOR} libcurand-dev-${CUDA_VER_MAJOR}-${CUDA_VER_MINOR} libnpp-dev-${CUDA_VER_MAJOR}-${CUDA_VER_MINOR} cuda-nvml-dev-${CUDA_VER_MAJOR}-${CUDA_VER_MINOR} \
    && (test -e /usr/local/cuda || ln -s /usr/local/cuda-${CUDA_VER_MAJOR}.${CUDA_VER_MINOR} /usr/local/cuda) \
    && python3 -m pip install --upgrade pip \
    && python3 -m pip install --upgrade meson ninja \
    && meson --version && which meson \
    && rm -f ${CUDA_DEB_NAME} \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/*

ENV CUDA_PATH=/usr/local/cuda
ENV RUSTUP_HOME=/usr/local/rustup
ENV CARGO_HOME=/usr/local/cargo
ENV PATH=/usr/local/cuda/bin:/usr/local/cargo/bin:$PATH

RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs \
      | sh -s -- -y --profile minimal \
    && /usr/local/cargo/bin/rustup show \
    && /usr/local/cargo/bin/cargo --version \
    && /usr/local/cargo/bin/cargo install cargo-c --locked \
    && /usr/local/cargo/bin/cargo cbuild --version

RUN git clone --depth 1 --branch ${FFMPEG_DLL_REF} ${FFMPEG_DLL_REPO} ${BUILD_SCRIPTS_DIR} \
    && cd ${BUILD_SCRIPTS_DIR}/ffmpeg_dll \
    && sed -i 's/\r$//' ./build_ffmpeg_dll.sh \
    && export NUMBER_OF_PROCESSORS="$(nproc)" \
    && bash ./build_ffmpeg_dll.sh --skip-src-archive \
    && test -f "${FFMPEG_PREFIX}/lib/pkgconfig/libavutil.pc" \
    && find "${BUILD_SCRIPTS_DIR}" -mindepth 1 -maxdepth 1 ! -path "${BUILD_SCRIPTS_DIR}/ffmpeg_dll" -exec rm -rf {} + \
    && find "${BUILD_SCRIPTS_DIR}/ffmpeg_dll" -mindepth 1 -maxdepth 1 ! -path "${BUILD_SCRIPTS_DIR}/ffmpeg_dll/build_ffmpeg_dll" -exec rm -rf {} + \
    && find "${BUILD_SCRIPTS_DIR}/ffmpeg_dll/build_ffmpeg_dll" -mindepth 1 -maxdepth 1 ! -path "${BUILD_SCRIPTS_DIR}/ffmpeg_dll/build_ffmpeg_dll/x64" -exec rm -rf {} + \
    && find "${BUILD_SCRIPTS_DIR}/ffmpeg_dll/build_ffmpeg_dll/x64" -mindepth 1 -maxdepth 1 ! -path "${BUILD_SCRIPTS_DIR}/ffmpeg_dll/build_ffmpeg_dll/x64/build" -exec rm -rf {} +

ENV FFMPEG_PREFIX=${FFMPEG_PREFIX}
WORKDIR /work
