ARG ALPINE_TAG=3.23.4
FROM alpine:${ALPINE_TAG}

ARG BUILD_BASE_VERSION=0.5-r3
RUN --mount=type=cache,sharing=locked,target=/var/cache/apk \
    apk add --update build-base=${BUILD_BASE_VERSION}

ARG CAPNPROTO_DEV_VERSION=1.2.0-r0
RUN --mount=type=cache,sharing=locked,target=/var/cache/apk \
    apk add --update capnproto-dev=${CAPNPROTO_DEV_VERSION}

ARG CARGO_VERSION=1.91.1-r1
RUN --mount=type=cache,sharing=locked,target=/var/cache/apk \
    apk add --update cargo=${CARGO_VERSION}

ARG CLANG21_DEV_VERSION=21.1.2-r2
RUN --mount=type=cache,sharing=locked,target=/var/cache/apk \
    apk add --update clang21-dev=${CLANG21_DEV_VERSION}

ARG CLANG21_STATIC_VERSION=21.1.2-r2
RUN --mount=type=cache,sharing=locked,target=/var/cache/apk \
    apk add --update clang21-static=${CLANG21_STATIC_VERSION}

ARG CMAKE_VERSION=4.1.3-r0
RUN --mount=type=cache,sharing=locked,target=/var/cache/apk \
    apk add --update cmake=${CMAKE_VERSION}

ARG LLVM21_DEV_VERSION=21.1.2-r1
RUN --mount=type=cache,sharing=locked,target=/var/cache/apk \
    apk add --update llvm21-dev=${LLVM21_DEV_VERSION}

ARG LLVM21_STATIC_VERSION=21.1.2-r1
RUN --mount=type=cache,sharing=locked,target=/var/cache/apk \
    apk add --update llvm21-static=${LLVM21_STATIC_VERSION}

ARG PROTOC_VERSION=31.1-r1
RUN --mount=type=cache,sharing=locked,target=/var/cache/apk \
    apk add --update protoc=${PROTOC_VERSION}

RUN \
    # This one would be nice if it worked:
    # --mount=type=bind,source=.,target=/root/app \
    # Use this one if there's a hard to track down "read-only filesystem" error:
    --mount=type=bind,source=.,target=/root/app,rw \

    # --mount=type=bind,source=./target,target=/root/app/target,rw \
    --mount=type=cache,sharing=private,target=/root/app/target \
    # --mount=type=cache,sharing=private,target=/root/app/target,uid=1000,gid=1000 \
    --mount=type=cache,target=/usr/local/cargo/git/db \
    --mount=type=cache,target=/usr/local/cargo/registry/ \
    cd /root/app && \
    cargo build --release && \
    cp target/release/fomo-reducer /program

ENTRYPOINT ["/program"]
