# docker build --rm -t neuriplo-infer:openvino -f Dockerfiles/Dockerfile.openvino .
#
# NOTE: OPENVINO_VERSION must be declared as a hardcoded ARG here (before FROM)
# because Docker evaluates FROM instructions before any RUN commands can execute.
# This means versions.env cannot be sourced to populate it at build time.
# Keep this version in sync with OPENVINO_VERSION in neuriplo/versions.env.
ARG OPENVINO_VERSION=2025.2.0

# Stage 1: Build
FROM openvino/ubuntu24_runtime:${OPENVINO_VERSION} AS builder

ENV DEBIAN_FRONTEND=noninteractive
ENV HOME=/root
ENV OpenVINO_DIR=${INTEL_OPENVINO_DIR}/runtime/cmake
ENV IE_PLUGINS_PATH=${INTEL_OPENVINO_DIR}/runtime/lib/intel64
ENV LD_LIBRARY_PATH=/opt/intel/opencl:${INTEL_OPENVINO_DIR}/runtime/3rdparty/tbb/lib:${IE_PLUGINS_PATH}:${LD_LIBRARY_PATH}
USER root

# Install common dependencies
RUN apt-get update \
    && apt-get -y install --no-install-recommends \
    build-essential cppcheck valgrind clang lldb llvm gdb libopencv-dev wget \
    ca-certificates \
    cmake \
    g++ \
    gcc \
    ninja-build \
    git \
    libgoogle-glog-dev \
    && apt-get autoremove -y && apt-get clean -y && rm -rf /var/lib/apt/lists/*

COPY VERSION /tmp/neuriplo-infer.VERSION

RUN dependency_ref=develop && \
    if grep -Eq '^[0-9]+\.[0-9]+\.[0-9]+$' /tmp/neuriplo-infer.VERSION; then dependency_ref=master; fi && \
    wget -q -O /tmp/versions.env "https://raw.githubusercontent.com/olibartfast/neuriplo/${dependency_ref}/versions.env"

RUN . /tmp/versions.env && \
    mkdir -p "$HOME/dependencies" && \
    ln -s "${INTEL_OPENVINO_DIR}" "$HOME/dependencies/openvino_${OPENVINO_VERSION}"

ARG BACKEND=OPENVINO

WORKDIR /app

COPY . .

# Build the project using CMake
RUN cmake -Bbuild -H. \
    -DDEFAULT_BACKEND=OPENVINO \
    -DBUILD_INFERENCE_ENGINE_TESTS=OFF -DBUILD_TESTS=OFF && \
    cmake --build build --config Release -j$(nproc)

# Stage 2: Final image
ARG OPENVINO_VERSION=2025.2.0
FROM openvino/ubuntu24_runtime:${OPENVINO_VERSION} AS final

ENV DEBIAN_FRONTEND=noninteractive
ENV HOME=/root
ENV OpenVINO_DIR=${INTEL_OPENVINO_DIR}/runtime/cmake
ENV IE_PLUGINS_PATH=${INTEL_OPENVINO_DIR}/runtime/lib/intel64
ENV LD_LIBRARY_PATH=/opt/intel/opencl:${INTEL_OPENVINO_DIR}/runtime/3rdparty/tbb/lib:${IE_PLUGINS_PATH}:${LD_LIBRARY_PATH}

WORKDIR /app

# Copy the executable
COPY --from=builder /app/build/app/neuriplo-infer /app/neuriplo-infer

# Copy the shared libraries
COPY --from=builder /app/build/_deps/neuriplo-build/libneuriplo.so /app/
COPY --from=builder /app/build/_deps/videocapture-build/libVideoCapture.so /app/

# Set the LD_LIBRARY_PATH to include the directory where the `.so` files are located
ENV LD_LIBRARY_PATH=/app:${LD_LIBRARY_PATH}

# Set the entry point to the compiled executable
ENTRYPOINT ["./neuriplo-infer"]
