FROM debian:bookworm-slim AS build

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && apt-get install -y --no-install-recommends \
    build-essential \
    autoconf \
    automake \
    libtool \
    pkg-config \
    ca-certificates \
    libssl-dev \
    && rm -rf /var/lib/apt/lists/*

WORKDIR /src
COPY . .

RUN ./bootstrap.sh && \
    cd build && \
    ./configure -C --prefix=/opt/darkbot --localstatedir=/data && \
    make -j"$(nproc)" && \
    make install DESTDIR=/stage

RUN mkdir -p /stage/opt/darkbot/share/darkbot/defaults && \
    cp -a dat/. /stage/opt/darkbot/share/darkbot/defaults/

FROM debian:bookworm-slim

ENV DEBIAN_FRONTEND=noninteractive \
    DARKBOT_DATA_DIR=/data/darkbot

RUN apt-get update && apt-get install -y --no-install-recommends \
    ca-certificates \
    libssl3 \
    libcrypt1 \
    && rm -rf /var/lib/apt/lists/*

COPY --from=build /stage/ /
COPY docker-entrypoint.sh /opt/darkbot/bin/docker-entrypoint.sh

RUN chmod +x /opt/darkbot/bin/docker-entrypoint.sh

VOLUME ["/data"]
WORKDIR /data/darkbot
ENTRYPOINT ["/opt/darkbot/bin/docker-entrypoint.sh"]
CMD ["/opt/darkbot/bin/darkbot"]
