##### Toolchain #####

FROM rust:1.94.1-slim-bookworm AS builder

# Install build dependencies and packaging tools
RUN apt-get update && apt-get install -y \
    build-essential libgtk-4-dev libglib2.0-dev \
    ruby ruby-dev rubygems rpm zstd libarchive-tools

# Install fpm
RUN gem install fpm -v 1.18.0

# Install rustup components
RUN rustup component add clippy
RUN rustup component add rustfmt

WORKDIR /workspace

ENV DEBIAN_DEPS="--depends libgtk-4-1 --depends dbus-x11 --depends iproute2 --depends crunch --deb-recommends aircrack-ng"
ENV REDHAT_DEPS="--depends gtk4 --depends dbus-x11 --depends iproute --rpm-tag Recommends:aircrack-ng"
ENV ARCHLINUX_DEPS="--depends gtk4 --depends dbus --depends iproute2 --pacman-optional-depends aircrack-ng"

##### Build and package #####

FROM builder AS build

COPY . .

# Keep the downloaded crates between builds. The target directory is not
# cached on purpose: a cache mount is shared by every checkout, and cargo
# detects source changes by mtime, so it could package stale binaries
RUN --mount=type=cache,target=/usr/local/cargo/registry,sharing=locked \
    cargo build --release --locked && \
    mkdir -p /out && \
    fpm -f -t deb -p /out/airgorah_`arch`.deb -a native $DEBIAN_DEPS && \
    fpm -f -t rpm -p /out/airgorah_`arch`.rpm -a native $REDHAT_DEPS && \
    fpm -f -t pacman -p /out/airgorah_`arch`.pkg.tar.zst -a native $ARCHLINUX_DEPS

##### Export #####

# Only the packages are written to the host by: docker build --output . .
FROM scratch AS packages
COPY --from=build /out/ /
