# =========================
#  Build stage
# =========================
FROM nvcr.io/nvidia/deepstream:7.1-triton-multiarch AS builder

# Remove broken Intel Realsense repo + key
# Install build dependencies (CMake, g++, etc.)
RUN rm -f /etc/apt/sources.list.d/archive_uri-https_librealsense_intel_com_debian_apt-repo-jammy.list && \
    rm -f /etc/apt/sources.list.d/cuda* && \
    rm -f /etc/apt/sources.list.d/nvidia-ml* && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        build-essential \
        cmake \
        git \
        pkg-config \
        libmicrohttpd-dev \
        libgtest-dev \
        nlohmann-json3-dev \
        libglib2.0 \
        libglib2.0-dev \
        libssl-dev \
        libmp3lame0 \
        libmp3lame-dev \
        libflac8 \
        libflac-dev \
        python3-opencv \
        libopencv-dev \
        libchromaprint1 \
        libmpg123-0 \
        mjpegtools \
        libavcodec58 \
        libmjpegutils-2.1-0 \
    && rm -rf /var/lib/apt/lists/*

# =========================
# Install Google Benchmark
# =========================
WORKDIR /tmp
RUN git clone https://github.com/google/benchmark.git && \
    cd benchmark && \
    cmake -E make_directory "build" && \
    cmake -E chdir "build" cmake -DBENCHMARK_DOWNLOAD_DEPENDENCIES=on -DCMAKE_BUILD_TYPE=Release ../ && \
    cmake --build "build" --config Release -- -j$(nproc) && \
    #cmake -E chdir "build" ctest --build-config Release && \
    cmake --build "build" --config Release --target install && \
    cmake --install build && \
    ldconfig && \
    rm -rf /tmp/benchmark

# =========================
# Install Prometheus C++ client
# =========================
WORKDIR /tmp
RUN git clone https://github.com/jupp0r/prometheus-cpp.git && \
    cd prometheus-cpp && \
    git submodule update --init --recursive && \
    mkdir _build && cd _build && \
    cmake .. -DBUILD_SHARED_LIBS=ON \
             -DCMAKE_BUILD_TYPE=Release \
             -DENABLE_PUSH=OFF \
             -DENABLE_PULL=ON \
             -DENABLE_EXPOSER=ON && \
    cmake --build . --parallel $(nproc) && \
    cmake --install . && \
    echo "/usr/local/lib" | tee /etc/ld.so.conf.d/prometheus-cpp.conf && \
    ldconfig && \
    rm -rf /tmp/prometheus-cpp

# =========================
# Install libhiredis
# =========================
WORKDIR /tmp
RUN git clone https://github.com/redis/hiredis.git && \
    cd hiredis && \
    git checkout tags/v1.2.0 && \
    make USE_SSL=1 && \
    make install && \
    cp libhiredis* /opt/nvidia/deepstream/deepstream/lib/ && \
    ln -sf /opt/nvidia/deepstream/deepstream/lib/libhiredis.so /opt/nvidia/deepstream/deepstream/lib/libhiredis.so.1.1.0 && \
    ldconfig && \
    rm -rf /tmp/hiredis

# =========================
# Install redis-server
# =========================
WORKDIR /tmp
RUN wget http://download.redis.io/releases/redis-6.0.8.tar.gz && \
    tar xzf redis-6.0.8.tar.gz && \
    cd redis-6.0.8 && \
    make && \
    make install && \
    rm -rf /tmp/redis-6.0.8 /tmp/redis-6.0.8.tar.gz
#    src/redis-server --protected-mode no
#    src/redis-server &

# expose redis default port
# EXPOSE 9736

# run redis server (no protected mode)
# CMD is the last line → when you run the container, it’ll start Redis automatically.
# CMD ["redis-server", "--protected-mode", "no"]

# Set working dir
WORKDIR /app

# Copy only cmake configs first (for caching)
COPY CMakeLists.txt ./
COPY src ./src
COPY entrypoint.sh ./

# Copy only required configs from host → container
COPY data/addresses.txt                             ./data/
COPY data/configuration.json                        ./data/
COPY data/inferserver                               ./data/inferserver
COPY data/nvmsgboker_configs/msgbroker_config.txt   ./data/nvmsgboker_configs/
COPY data/nvmsgconv_configs/msgconv_config.txt      ./data/nvmsgconv_configs/
COPY data/tracker_configs                           ./data/tracker_configs


# COPY data ./data
# RUN cp /opt/nvidia/deepstream/deepstream/lib/libnvds_redis_proto.so                                         ./data/nvmsgboker_configs/libnvds_redis_proto.so
# RUN cp /opt/nvidia/deepstream/deepstream/lib/libnvds_msgconv.so                                             ./data/nvmsgconv_configs/libnvds_msgconv.so
# RUN cp /opt/nvidia/deepstream/deepstream/lib/gst-plugins/libnvdsgst_tracker.so                              ./data/tracker_configs/libnvdsgst_tracker.so
# RUN cp /opt/nvidia/deepstream/deepstream/lib/libnvds_nvmultiobjecttracker.so                                ./data/tracker_configs/libnvds_nvmultiobjecttracker.so
# RUN cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvSORT.yml           ./data/tracker_configs/config_tracker_NvSORT.yml
# RUN cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_IOU.yml              ./data/tracker_configs/config_tracker_IOU.yml
# RUN cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvDCF_accuracy.yml   ./data/tracker_configs/config_tracker_NvDCF_accuracy.yml
# RUN cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvDCF_max_perf.yml   ./data/tracker_configs/config_tracker_NvDCF_max_perf.yml
# RUN cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvDCF_perf.yml       ./data/tracker_configs/config_tracker_NvDCF_perf.yml
# RUN cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvDeepSORT.yml       ./data/tracker_configs/config_tracker_NvDeepSORT.yml


# Build project
RUN mkdir -p build && cd build && \
    cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_EXPORT_COMPILE_COMMANDS=ON && \
    cmake --build . --config Release --parallel $(nproc)

# =========================
#  Runtime stage
# =========================
FROM nvcr.io/nvidia/deepstream:7.1-triton-multiarch

# =========================
# Install Python dependencies
# =========================
RUN pip3 install \
    numpy==1.26.4 \
    opencv-python-headless==4.10.0.84 \
    tritonclient \
    gevent \
    geventhttpclient

WORKDIR /app

# Copy configs from build stage: Copy runtime data/configs
COPY --from=builder /app/data ./data

WORKDIR /app/build

# Copy compiled binaries from builder
COPY --from=builder /app/build/bin/BodyPipeline /app/build/bin/BodyPipeline
COPY --from=builder /app/entrypoint.sh /app/entrypoint.sh


# Copy DeepStream runtime libs/configs (container → image → host at runtime)
# These files will be available inside the image under /app/data
RUN mkdir -p /app/data/nvmsgboker_configs && \
    mkdir -p /app/data/nvmsgconv_configs && \
    mkdir -p /app/data/tracker_configs && \
    cp /opt/nvidia/deepstream/deepstream/lib/libnvds_redis_proto.so                                          /app/data/nvmsgboker_configs/ && \
    cp /opt/nvidia/deepstream/deepstream/lib/libnvds_msgconv.so                                              /app/data/nvmsgconv_configs/ && \
    cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvSORT.yml            /app/data/tracker_configs/ && \
    cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_IOU.yml               /app/data/tracker_configs/ && \
    cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvDCF_accuracy.yml    /app/data/tracker_configs/ && \
    cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvDCF_max_perf.yml    /app/data/tracker_configs/ && \
    cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvDCF_perf.yml        /app/data/tracker_configs/ && \
    cp /opt/nvidia/deepstream/deepstream/samples/configs/deepstream-app/config_tracker_NvDeepSORT.yml        /app/data/tracker_configs/ && \
    cp /opt/nvidia/deepstream/deepstream/lib/libnvds_nvmultiobjecttracker.so                                 /app/data/tracker_configs/ && \
    cp /opt/nvidia/deepstream/deepstream/lib/gst-plugins/libnvdsgst_tracker.so                               /app/data/tracker_configs/

RUN chmod +x /app/entrypoint.sh