FROM debian:13 AS build

ARG NGTCP2_BRANCH=main

RUN <<EOF
set -e

apt-get update
DEBIAN_FRONTEND=noninteractive apt-get install -y --no-install-recommends \
    git clang-19 make binutils autoconf automake autotools-dev libtool \
    pkg-config libev-dev libjemalloc-dev cmake cmake-data \
    ca-certificates media-types

git clone --depth 1 -b v1.71.0 https://github.com/aws/aws-lc
cd aws-lc
cmake -Bbuild -DDISABLE_GO=ON \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_C_COMPILER=clang-19 \
    -DCMAKE_CXX_COMPILER=clang++-19
make -j$(nproc) -C build
cmake --install build
cd ..
rm -rf aws-lc

git clone --recursive --shallow-submodules --depth 1 \
    https://github.com/ngtcp2/nghttp3
cd nghttp3
autoreconf -i
./configure --disable-dependency-tracking --enable-lib-only \
    CC=clang-19 CXX=clang++-19
make -j$(nproc)
make install-strip
cd ..
rm -rf nghttp3

git clone --recursive --shallow-submodules --depth 1 -b $NGTCP2_BRANCH \
    https://github.com/ngtcp2/ngtcp2
cd ngtcp2
autoreconf -i
./configure \
    CC=clang-19 \
    CXX=clang++-19 \
    LDFLAGS="-static-libgcc -static-libstdc++" \
    LIBTOOL_LDFLAGS="-static-libtool-libs" \
    LIBEV_LIBS="-l:libev.a" \
    BORINGSSL_LIBS="-lssl -lcrypto" \
    BORINGSSL_CFLAGS="-I/usr/local/include" \
    JEMALLOC_LIBS="-l:libjemalloc.a -lm" \
    --disable-dependency-tracking \
    --with-boringssl
make -j$(nproc)
strip examples/bsslclient examples/bsslserver
cp examples/bsslclient examples/bsslserver /usr/local/bin
cd ..
rm -rf ngtcp2

apt-get -y purge \
    git clang-19 make binutils autoconf automake autotools-dev libtool \
    pkg-config libev-dev libjemalloc-dev \
    ca-certificates
apt-get -y autoremove --purge
rm -rf /var/log/*
EOF

FROM gcr.io/distroless/base-nossl-debian13

COPY --from=build --link /usr/local/bin/bsslclient /usr/local/bin/bsslserver \
    /usr/local/bin/
COPY --from=build --link /etc/mime.types /etc/

CMD ["/usr/local/bin/bsslclient"]
