FROM rust:1.91.0-slim-bookworm AS builder

RUN apt-get update && apt-get install -y \
    ca-certificates \
    libssl-dev \
    pkg-config \
    g++ \
    libuv1-dev \
    git \
    build-essential \
    cmake \
    curl \
    wget \
    protobuf-compiler \
    zlib1g-dev \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# Update CA certificates
RUN update-ca-certificates

#RUN apt-get update && apt-get install -y \
#      ca-certificates \
#      libssl-dev \
#      libssl3 \
#    && apt-get clean \
#    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

RUN useradd -u 10001 nonroot

WORKDIR /build

COPY . .

RUN cargo build --example chat

ARG ARCHDIR=x86_64-linux-gnu


RUN date > /tmp/x
RUN find /build/target

RUN ls -al /usr/lib/${ARCHDIR}

FROM gcr.io/distroless/cc-debian12

# Set this to aarch64-linux-gnu to docker build on Mac ARM
ARG ARCHDIR=x86_64-linux-gnu


COPY --from=builder /etc/passwd /etc/passwd
COPY --from=builder /etc/group /etc/group
COPY --from=builder --chown=nonroot:nonroot /build/target/debug/examples/chat /app
COPY --from=builder /usr/lib/${ARCHDIR}/libssl.so.3 /usr/lib/libssl.so.3

USER nonroot

CMD ["/app"]
