# Build multi-arch : docker buildx build --platform linux/amd64,linux/arm64 .
FROM --platform=$BUILDPLATFORM rust:1.82-alpine AS builder

ARG TARGETPLATFORM
ARG BUILDPLATFORM

RUN apk add --no-cache musl-dev

# Mapping TARGETPLATFORM → cible Rust
RUN case "$TARGETPLATFORM" in \
    "linux/amd64")   echo "x86_64-unknown-linux-musl"    > /rust_target ;; \
    "linux/arm64")   echo "aarch64-unknown-linux-musl"   > /rust_target ;; \
    "linux/arm/v7")  echo "armv7-unknown-linux-musleabihf" > /rust_target ;; \
    *) echo "x86_64-unknown-linux-musl" > /rust_target ;; \
    esac

RUN rustup target add "$(cat /rust_target)"

WORKDIR /build

# Cache des dépendances
COPY Cargo.toml ./
RUN mkdir src && echo 'fn main(){}' > src/main.rs
RUN cargo build --release --target "$(cat /rust_target)" 2>/dev/null || true
RUN rm -rf src

COPY src ./src
RUN cargo build --release --target "$(cat /rust_target)"
RUN cp "target/$(cat /rust_target)/release/agent-scan-network" /agent

# Image finale minimale
FROM alpine:3.21
RUN apk add --no-cache ca-certificates iputils
WORKDIR /app
COPY --from=builder /agent ./agent-scan-network
EXPOSE 9100
VOLUME ["/app/config.yaml"]
ENTRYPOINT ["./agent-scan-network", "/app/config.yaml"]
