FROM rust:1.96-slim-trixie AS builder
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates pkg-config perl make cmake clang mold \
    && rm -rf /var/lib/apt/lists/*
ENV RUSTFLAGS="-C linker=clang -C link-arg=-fuse-ld=mold"
RUN rustup toolchain install 1.96.0 \
      --profile minimal \
      --component rustfmt \
      --component clippy \
      --target wasm32-unknown-unknown \
      --no-self-update
ENV CC=clang
ENV CXX=clang++
ARG BOBBIN_PROFILE=release
WORKDIR /src
COPY Cargo.toml Cargo.lock rust-toolchain.toml ./
COPY lexicons ./lexicons
COPY crates/trusted-proxies ./crates/trusted-proxies
COPY bobbin ./bobbin
# keep image lighter by not copying in shittle, knot2
# does however need some cargo toml patchings
RUN sed -i \
      -e 's/, "shuttle"//' \
      -e 's/, "knot2\/crates\/\*"//' \
      -e '/^knot-[a-z0-9-]* = { path = "knot2\/crates\//d' \
      -e '/^gix-pack = { path = "knot2\/third_party\/gix-pack" }/d' \
      Cargo.toml
RUN cargo build --profile ${BOBBIN_PROFILE} --bin bobbin --package bobbin
RUN if [ "${BOBBIN_PROFILE}" = "release" ]; then strip target/${BOBBIN_PROFILE}/bobbin; fi

FROM debian:trixie-slim
ARG BOBBIN_PROFILE=release
RUN apt-get update && apt-get install -y --no-install-recommends \
      ca-certificates \
    && rm -rf /var/lib/apt/lists/*
COPY --from=builder /src/target/${BOBBIN_PROFILE}/bobbin /usr/local/bin/bobbin
ENV BOBBIN_BIND=0.0.0.0:8090
EXPOSE 8090
ENTRYPOINT ["/usr/local/bin/bobbin"]
