# Basic cross-build environment
FROM rustembedded/cross:aarch64-unknown-linux-musl as cross-build

ARG UID=1000
ARG GID=1000

ENV \
 ARCH=arm64 \
 HOST=aarch64-unknown-linux \
 TOOLCHAIN=stable \
 TARGET=aarch64-unknown-linux-musl \
 BUILD_MODE=release \
 DEBIAN_FRONTEND=noninteractive

RUN \
 sed 's/http:\/\/\(.*\).ubuntu.com\/ubuntu\//[arch-=amd64,i386] http:\/\/ports.ubuntu.com\/ubuntu-ports\//g' /etc/apt/sources.list > /etc/apt/sources.list.d/ports.list && \
 sed -i 's/http:\/\/\(.*\).ubuntu.com\/ubuntu\//[arch=amd64,i386] http:\/\/\1.archive.ubuntu.com\/ubuntu\//g' /etc/apt/sources.list && \
 addgroup --gid $GID build && \
 adduser \
    --disabled-password \
    --gecos "" \
    --ingroup build \
    --uid $UID \
    --home /opt/build \
    build

WORKDIR /opt/build


# Environment for Rust
FROM cross-build as rust

ENV \
 PATH=$PATH:/opt/build/.cargo/bin

ADD --chown=build:build https://sh.rustup.rs /opt/build/rustup.sh

USER build

RUN \
 chmod +x rustup.sh && \
 ./rustup.sh --default-toolchain $TOOLCHAIN --profile minimal -y && \
 rustup target add $TARGET


# Environment for pict-rs
FROM cross-build as pict-rs-builder

ENV \
 PATH=$PATH:/opt/build/.cargo/bin

COPY --from=rust --chown=build:build /opt/build/.cargo /opt/build/.cargo
COPY --from=rust --chown=build:build /opt/build/.rustup /opt/build/.rustup
COPY root/ /

USER build

RUN \
 mkdir -p /opt/build/repo

WORKDIR /opt/build/repo
