#####################
# Build Application #
#####################
FROM reg.office.nic.cz/cpp/build:latest AS builder

ARG BUILD_JOBS=1
ENV CMAKE_BUILD_PARALLEL_LEVEL=$BUILD_JOBS

WORKDIR /app

COPY . /app

RUN echo "Using $CMAKE_BUILD_PARALLEL_LEVEL job(s) for build" && \
    cmake -B build -S . -DFRED_DNSHOSTING_SERVICES_BUILD_TESTS=ON && cmake --build build

###########
# Runtime #
###########
FROM reg.office.nic.cz/cpp/base:latest AS runtime

ARG USER=fred

RUN apt-get -y update                       \
        && apt-get -y install --no-install-recommends \
            postgresql-client               \
            libpq5                          \
            libboost-test1.74.0             \
            libboost-program-options1.74.0  \
            libgrpc++1.51                   \
            libgetdns10                     \
        && apt-get -y clean                 \
        && rm -rf /var/lib/apt/lists/*

USER ${USER}:${USER}

EXPOSE 2247

COPY --from=config --chown=${USER}:${USER} ./docker-entrypoint.sh /

ENTRYPOINT ["/docker-entrypoint.sh"]

########################
# Final image - client #
########################
FROM runtime AS dnshosting-client

COPY --from=builder --chown=${USER}:${USER} /app/build/bin/fred-dnshosting-client /usr/local/bin/fred-dnshosting-client
COPY --from=config --chown=${USER}:${USER} ./fred-dnshosting-client.conf.template /etc/fred/fred-dnshosting.conf.template

CMD ["fred-dnshosting-client", "--config", "/etc/fred/fred-dnshosting.conf"]

#########################
# Final image - service #
#########################
FROM runtime AS dnshosting

COPY --from=builder --chown=${USER}:${USER} /app/build/bin/fred-dnshosting-services /usr/local/bin/fred-dnshosting-services
COPY --from=config --chown=${USER}:${USER} ./fred-dnshosting-services.conf.template /etc/fred/fred-dnshosting.conf.template
COPY --chown=${USER}:${USER} sql/ /app/sql/

COPY --from=builder --chown=${USER}:${USER} /app/build/test/bin/test-dnshosting-services /app/test/test-dnshosting-services
COPY --from=builder --chown=${USER}:${USER} /app/docker/devel/test.conf /app/test/test.conf
COPY --chown=${USER}:${USER} test/testdata/02_insert_data.sql /app/sql/999_test_data.sql

CMD ["fred-dnshosting-services", "--config", "/etc/fred/fred-dnshosting.conf"]
