FROM alpine AS build
RUN apk add --no-cache alpine-sdk gcc linux-headers libxml2-dev libxslt-dev cmake libusb-dev bash samurai

RUN git clone --depth 1 https://github.com/steve-m/librtlsdr.git && \
    git clone --depth 1 https://github.com/wmbusmeters/wmbusmeters.git && \
    git clone --depth 1 https://github.com/weetmuts/rtl-wmbus.git && \
    git clone --depth 1 https://github.com/merbanan/rtl_433.git && \
    git clone --depth 1 https://github.com/ED6E0F17/rtl_reset.git && \
    git clone --depth 1 https://github.com/libxmq/xmq.git

WORKDIR /librtlsdr
RUN cmake -B build -G Ninja \
        -DCMAKE_BUILD_TYPE=MinSizeRel \
        -DCMAKE_INSTALL_PREFIX:PATH=/usr \
        -DDETACH_KERNEL_DRIVER=ON \
        -Wno-dev && \
    cmake --build build -j `nproc` && \
    cmake --install build
WORKDIR /wmbusmeters
RUN make -j`nproc`
WORKDIR /rtl-wmbus
RUN make -j`nproc` release && chmod 755 build/rtl_wmbus
WORKDIR /rtl_433
RUN cmake -B build -G Ninja \
        -DCMAKE_BUILD_TYPE=MinSizeRel && \
    cmake --build build -j `nproc`
WORKDIR /rtl_reset
RUN make
WORKDIR /xmq
RUN ./configure && make -j`nproc`

FROM alpine AS builder
RUN apk add --no-cache mosquitto-clients libstdc++ curl libusb libxml2
WORKDIR /wmbusmeters
COPY --from=build /librtlsdr/build/src/librtlsdr.so.* /usr/lib/
COPY --from=build /librtlsdr/rtl-sdr.rules /usr/lib/udev/rules.d/rtl-sdr.rules
COPY --from=build /librtlsdr/build/src/rtl_* /usr/bin/
COPY --from=build /wmbusmeters/build/wmbusmeters /wmbusmeters/wmbusmeters
COPY --from=build /rtl-wmbus/build/rtl_wmbus /usr/bin/rtl_wmbus
COPY --from=build /rtl_433/build/src/rtl_433 /usr/bin/rtl_433
COPY --from=build /rtl_reset/rtl_reset /usr/bin/rtl_reset
COPY --from=build /xmq/build/default/release/xmq /usr/bin/xmq
COPY --from=build /wmbusmeters/docker/docker-entrypoint.sh /wmbusmeters/docker-entrypoint.sh
VOLUME /wmbusmeters_data/
CMD ["sh", "/wmbusmeters/docker-entrypoint.sh"]
