# syntax=docker/dockerfile:1
FROM scratch as pdfium
ADD https://artifactory.developers.mega.co.nz/artifactory/debian-dev/private/Debian_12/amd64/pdfium-mega_5247.0-4.1_amd64.deb /tmp/pdfium/pdfium-mega.deb

FROM debian:12-slim as base

RUN rm -f /etc/apt/apt.conf.d/docker-clean; \
    echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    --mount=type=bind,from=pdfium,source=/tmp/pdfium/pdfium-mega.deb,target=/tmp/pdfium/pdfium-mega.deb \
    apt-get update \
    && apt-get install -y --no-install-recommends \
    googletest \
    libasan8 \
    libtsan2 \
    libavcodec59 \
    libavformat59 \
    libavutil57 \
    libc6 \
    libpcrecpp0v5 \
    libc-ares2 \
    libcrypto++8 \
    libfreeimage3 \
    libgcc-s1 \
    libicu72 \
    libmediainfo0v5 \
    libpcre3 \
    libpcre32-3 \
    libreadline8 \
    libsodium23 \
    libsqlite3-0 \
    libssl3 \
    libstdc++6 \
    libswscale6 \
    libtinyxml2-9 \
    libtinyxml2.6.2v5 \
    libubsan1 \
    libuv1 \
    libzen0v5 \
    zlib1g \
    gpg \
    /tmp/pdfium/pdfium-mega.deb

FROM base as build-deps

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt-get update \
    && apt-get install -y --no-install-recommends \
    build-essential \
    ccache \
    debhelper dh-autoreconf \
    libavcodec-dev \
    libavformat-dev \
    libavutil-dev \
    libc-ares-dev \
    libcrypto++-dev \
    libfreeimage-dev \
    libgmock-dev \
    libgtest-dev \
    libicu-dev \
    libmediainfo-dev \
    libpcre3-dev \
    libreadline-dev \
    libsodium-dev \
    libsqlite3-dev \
    libssl-dev \
    libswscale-dev \
    libtinyxml2-dev \
    libtool-bin \
    libuv1-dev \
    libzen-dev \
    pkg-config \
    zip \
    zlib1g-dev

FROM build-deps as build-deps-cmake

RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
    --mount=type=cache,target=/var/lib/apt,sharing=locked \
    apt-get update \
    && apt-get install -y --no-install-recommends cmake

FROM scratch as src

WORKDIR /usr/src/megacmd
COPY Makefile.am autogen.sh clean.sh configure.ac ./
COPY sdk ./sdk
COPY src ./src
COPY build ./build
COPY contrib ./contrib
COPY tests/common ./tests/common
COPY tests/integration ./tests/integration
COPY tests/unit ./tests/unit

FROM build-deps-cmake as build

WORKDIR /usr/src/megacmd

ENV CC "ccache gcc-12"
ENV CXX "ccache g++-12"
ENV CCACHE_DIR /tmp/ccache
ARG ENABLE_ASAN=ON
ARG ENABLE_UBSAN=ON
ARG ENABLE_TSAN=OFF
ARG ENABLE_MEGACMD_TESTS=ON

COPY --from=src /usr/src/megacmd /usr/src/megacmd

RUN --mount=type=cache,target=/tmp/ccache \
    --mount=type=tmpfs,target=/tmp/build \
    cmake -S build/cmake -B /tmp/build \
    -D ENABLE_ASAN=${ENABLE_ASAN} -D \
    ENABLE_UBSAN=${ENABLE_UBSAN} -D \
    ENABLE_TSAN=${ENABLE_TSAN} -D \
    ENABLE_MEGACMD_TESTS=${ENABLE_MEGACMD_TESTS} \
    && make -C /tmp/build -j$(nproc) mega-cmd mega-cmd-server mega-exec \
    mega-cmd-updater mega-cmd-tests-integration mega-cmd-tests-unit \
    && cd /tmp/build \
    && install mega-cmd mega-cmd-server \
    mega-cmd-tests-integration mega-cmd-tests-unit \
    mega-cmd-updater mega-exec \
    /usr/local/bin/

FROM base as final

COPY --from=build /usr/local/ /usr/local/
