FROM debian:bookworm-slim AS builder

ARG BIRD_VERSION=3.2.1

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    bison \
    flex \
    m4 \
    perl \
    ca-certificates \
    wget \
    xz-utils \
    libreadline-dev \
    libncurses-dev \
    libssh-dev \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /build

RUN wget -O bird.tar.gz "https://bird.nic.cz/download/bird-${BIRD_VERSION}.tar.gz" \
 && tar -xzf bird.tar.gz \
 && mv "bird-${BIRD_VERSION}" bird

WORKDIR /build/bird

RUN ./configure \
      --prefix=/usr \
      --sysconfdir=/etc/bird \
      --localstatedir=/run \
 && make -j"$(nproc)" \
 && make install

FROM debian:bookworm-slim

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    gettext-base \
    tzdata \
    netcat-openbsd \
    libreadline8 \
    libncurses6 \
    libtinfo6 \
    libssh-4 \
 && rm -rf /var/lib/apt/lists/*

COPY --from=builder /usr/sbin/bird /usr/sbin/bird
COPY --from=builder /usr/sbin/birdc /usr/sbin/birdc
COPY --from=builder /etc/bird /etc/bird
ENV TZ=Asia/Shanghai

COPY entrypoint.sh /entrypoint.sh
RUN chmod +x /entrypoint.sh \
 && mkdir -p /run/bird

ENTRYPOINT ["/entrypoint.sh"]
