# Build paivana-httpd from source against Taler libs (deb.taler.net)
FROM docker.io/library/debian:trixie AS build
ENV DEBIAN_FRONTEND=noninteractive
RUN mkdir -p /etc/apt/keyrings
COPY taler-systems.gpg /etc/apt/keyrings/taler-systems.gpg
RUN apt-get update -yqq \
 && apt-get install -yqq --no-install-recommends \
      ca-certificates curl gnupg git \
      meson ninja-build pkg-config gcc g++ make \
      zlib1g-dev libmicrohttpd-dev libcurl4-gnutls-dev \
      libjansson-dev libgcrypt20-dev libpq-dev \
      python3 python3-jinja2 \
 && printf "%s\n" "deb [signed-by=/etc/apt/keyrings/taler-systems.gpg] https://deb.taler.net/apt/debian trixie main" \
      > /etc/apt/sources.list.d/taler.list \
 && apt-get update -yqq \
 && apt-get install -yqq --no-install-recommends \
      libgnunet-dev libtalerexchange-dev libtalermerchant-dev \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /src
RUN git clone --depth 1 https://git.taler.net/paivana.git . \
 && if [ -x ./bootstrap ]; then ./bootstrap; fi \
 && if [ -x ./configure ]; then \
      ./configure --prefix=/usr/local && make -j"$(nproc)" && make install; \
    else \
      meson setup build --prefix=/usr/local && meson compile -C build && meson install -C build; \
    fi

FROM docker.io/library/debian:trixie-slim
ENV DEBIAN_FRONTEND=noninteractive
RUN mkdir -p /etc/apt/keyrings
COPY taler-systems.gpg /etc/apt/keyrings/taler-systems.gpg
RUN apt-get update -yqq \
 && apt-get install -yqq --no-install-recommends ca-certificates curl \
 && printf "%s\n" "deb [signed-by=/etc/apt/keyrings/taler-systems.gpg] https://deb.taler.net/apt/debian trixie main" \
      > /etc/apt/sources.list.d/taler.list \
 && apt-get update -yqq \
 && apt-get install -yqq --no-install-recommends \
      libtalerexchange libtalermerchant libgnunet \
      libmicrohttpd12t64 libcurl3t64-gnutls libjansson4 libgcrypt20 zlib1g libpq5 \
 && rm -rf /var/lib/apt/lists/* \
 && useradd --system --home /var/lib/paivana --shell /usr/sbin/nologin paivana-httpd \
 && mkdir -p /etc/paivana /var/lib/paivana \
 && chown -R paivana-httpd:paivana-httpd /var/lib/paivana /etc/paivana

COPY --from=build /usr/local/ /usr/local/
RUN ldconfig || true
COPY entrypoint.sh /usr/local/bin/paivana-entrypoint
COPY conf/paivana.conf.template /etc/paivana/paivana.conf.template
RUN chmod 755 /usr/local/bin/paivana-entrypoint \
 && chown paivana-httpd:paivana-httpd /etc/paivana/paivana.conf.template

USER paivana-httpd
EXPOSE 9967
ENTRYPOINT ["/usr/local/bin/paivana-entrypoint"]
