#####################
# 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 . && 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 \
            libpq5                          \
            libmpdec3                       \
            libssl3                         \
            libboost-program-options1.74.0  \
            libboost-thread1.74.0           \
            libboost-regex1.74.0            \
            libboost-filesystem1.74.0       \
            libboost-random1.74.0           \
            libboost-test1.74.0             \
            libidn12                        \
            libgrpc++1.51                   \
            protobuf-compiler               \
        && apt-get -y clean                 \
        && rm -rf /var/lib/apt/lists/*

USER ${USER}:${USER}

EXPOSE 2240

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

ENTRYPOINT ["/docker-entrypoint.sh"]

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

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

COPY --from=builder --chown=${USER}:${USER} /app/build/bin/test-fred-registry-services /app/test/test-fred-registry-services
COPY --chown=${USER}:${USER} ./docker/devel/test.conf /app/test/test.conf

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