# syntax=docker/dockerfile:1.4

# https://askubuntu.com/questions/972516/debian-frontend-environment-variable
ARG DEBIAN_FRONTEND=noninteractive

# Globally set pip break-system-packages option to avoid having to specify it every time
ARG PIP_BREAK_SYSTEM_PACKAGES=1

FROM wheels AS trt-wheels
ARG PIP_BREAK_SYSTEM_PACKAGES

# Install TensorRT wheels
COPY docker/tensorrt/requirements-amd64.txt /requirements-tensorrt.txt
COPY docker/main/requirements-wheels.txt /requirements-wheels.txt

# remove dependencies from the requirements that have type constraints
RUN  sed -i '/\[.*\]/d' /requirements-wheels.txt \
    && pip3 wheel --wheel-dir=/trt-wheels -c /requirements-wheels.txt -r /requirements-tensorrt.txt

FROM deps AS frigate-tensorrt
ARG PIP_BREAK_SYSTEM_PACKAGES

RUN --mount=type=bind,from=trt-wheels,source=/trt-wheels,target=/deps/trt-wheels \
    pip3 uninstall -y onnxruntime tensorflow-cpu \
    && pip3 install -U /deps/trt-wheels/*.whl

COPY --from=rootfs / /
COPY docker/tensorrt/detector/rootfs/etc/ld.so.conf.d /etc/ld.so.conf.d
RUN ldconfig

WORKDIR /opt/frigate/

# Dev Container w/ TRT
FROM devcontainer AS devcontainer-trt

RUN --mount=type=bind,from=trt-wheels,source=/trt-wheels,target=/deps/trt-wheels \
    pip3 install -U /deps/trt-wheels/*.whl
