FROM ubuntu:24.04 AS builder

ARG VPP_BRANCH=v26.02
ENV DEBIAN_FRONTEND=noninteractive

# Install build dependencies
RUN apt-get update && apt-get install -y \
    git \
    build-essential \
    cmake \
    ninja-build \
    pkg-config \
    python3 \
    python3-pip \
    python3-venv \
    python3-ply \
    python3-jsonschema \
    sudo \
    curl \
    wget \
    && rm -rf /var/lib/apt/lists/*

# Clone VPP source
WORKDIR /src
RUN git clone --depth 1 --branch ${VPP_BRANCH} https://gerrit.fd.io/r/vpp.git

WORKDIR /src/vpp

# Install VPP build dependencies
RUN yes | make install-dep 2>&1 | tail -20

# Install external dependencies (DPDK, rdma-core, etc.)
RUN yes | make install-ext-deps 2>&1 | tail -20

# Build release VPP
RUN make build-release 2>&1 | tail -30

# ---- Runtime image ----
FROM ubuntu:24.04

ENV DEBIAN_FRONTEND=noninteractive

# Install runtime dependencies
RUN apt-get update && apt-get install -y \
    libnuma1 \
    libibverbs1 \
    ibverbs-providers \
    rdma-core \
    librdmacm1 \
    libnl-3-200 \
    libnl-route-3-200 \
    libssl3t64 \
    libelf1t64 \
    libmnl0 \
    libunwind8 \
    libapr1t64 \
    iproute2 \
    iputils-ping \
    pciutils \
    && rm -rf /var/lib/apt/lists/*

# Copy the VPP install tree directly from the builder
COPY --from=builder /src/vpp/build-root/install-vpp-native/vpp/ /usr/

# Copy ext-deps shared libraries (rdma-core, DAQ, etc.)
COPY --from=builder /opt/vpp/external/aarch64/lib/ /usr/lib/aarch64-linux-gnu/

# Make sure dynamic linker can find everything
RUN ldconfig

# Verify
RUN vpp --version && echo "=== Plugins ===" && ls /usr/lib/aarch64-linux-gnu/vpp_plugins/ | head -20

# Create runtime directories
RUN mkdir -p /etc/vpp /run/vpp /var/log/vpp /dev/hugepages

COPY configs/startup-common.conf /etc/vpp/startup.conf

ENTRYPOINT ["/usr/bin/vpp", "-c", "/etc/vpp/startup.conf"]
