FROM archlinux:latest AS base

COPY mirrorlist /etc/pacman.d/mirrolist

RUN pacman-key --init
RUN pacman-key --refresh-keys

RUN curl --retry 3 -OJ https://gitea.woggioni.net/api/packages/woggioni/arch/repository.key

RUN pacman-key --add repository.key
RUN pacman-key --lsign-key 0D28BF66FDB45D18D8EBEE5D4C91DADCD00B3F77

RUN --mount=type=bind,source=pacman-gitea-repository.conf,target=pacman-gitea-repository.conf cat pacman-gitea-repository.conf  >> /etc/pacman.conf

RUN rm repository.key
RUN --mount=type=cache,target=/var/cache/pacman pacman -Syu --noconfirm
RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed \
    curl \
    aarch64-unknown-linux-musl-gcc x86_64-unknown-linux-musl-gcc \
    arm-unknown-linux-musleabihf-gcc \
    arm-unknown-linux-musleabi-gcc \
    x86_64-unknown-linux-musl-gcc \
    x86_64-w64-mingw32-gcc \
    aarch64-unknown-linux-musl-gcc x86_64-unknown-linux-musl-zlib \
    arm-unknown-linux-musleabihf-zlib \
    arm-unknown-linux-musleabi-zlib \
    x86_64-unknown-linux-musl-zlib \
    x86_64-w64-mingw32-zlib


RUN mkdir downloads
RUN ln -s /opt/x-tools/aarch64-unknown-linux-musl/bin/aarch64-unknown-linux-musl-gcc /usr/local/bin/aarch64-linux-musl-gcc 
RUN ln -s /opt/x-tools/x86_64-unknown-linux-musl/bin/x86_64-unknown-linux-musl-gcc /usr/local/bin/x86_64-linux-musl-gcc
RUN ln -s /opt/x-tools/arm-unknown-linux-musleabihf/bin/arm-unknown-linux-musleabihf-gcc /usr/local/bin/arm-linux-musleabihf-gcc
RUN ln -s /opt/x-tools/arm-unknown-linux-musleabihf/bin/arm-unknown-linux-musleabi-gcc /usr/local/bin/arm-linux-musleabi-gcc
RUN ln -s /opt/x-tools/x86_64-w64-mingw32/bin/x86_64-w64-mingw32-gcc /usr/local/bin/x86_64-pc-windows-gnu-gcc

COPY makepkg/* /etc/

RUN useradd luser -G users -u 1000 -s /bin/bash -m -d /home/luser

RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed \
    git \
    cmake \
    base-devel \
    meson \
    tar \
    xz \
    ccache \
    lzop \
    lz4 \
    bzip2 \
    gzip \
    upx

FROM base AS kernel
RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed \
    jre21-openjdk \
    bc \
    cpio \
    gettext \
    libelf \
    pahole \
    perl \
    python \
    python-pip \
    python-build \
    python-wheel \
    graphviz \
    imagemagick \
    python-sphinx \
    python-yaml \
    openssh \
    rust \
    rust-src \
    rust-bindgen \
    texlive-latexextra
USER luser
WORKDIR /home/luser

FROM base AS openwrt-builder
RUN mkdir -p /etc/cargo
COPY --chown=1000:1000 config.toml /home/luser/.cargo/config.toml
RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed rustup go rsync wget unzip swig python-setuptools python-pyelftools

FROM base AS rust
RUN mkdir -p /etc/cargo
COPY --chown=1000:1000 config.toml /home/luser/.cargo/config.toml
RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed \
    rustup \
    trunk \
    llvm \
    clang
    
FROM rust AS rust-full
RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed \
    hidapi \
    protobuf     
USER luser
WORKDIR /home/luser
RUN rustup update
RUN rustup toolchain install stable-x86_64-unknown-linux-gnu --profile minimal
RUN rustup component add rustfmt
RUN rustup target add \
    x86_64-unknown-linux-musl \
    x86_64-unknown-linux-gnu \
    armv7-unknown-linux-musleabihf \
    arm-unknown-linux-musleabi \
    aarch64-unknown-linux-musl \
    aarch64-unknown-linux-gnu \
    wasm32-unknown-unknown \
    wasm32-wasip2
    
FROM base AS rust-sccache
RUN mkdir -p /etc/cargo
COPY --chown=1000:1000 config-sccache.toml /home/luser/.cargo/config.toml
COPY --chown=1000:1000 sccache_config.toml /home/luser/.config/sccache/config
RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed \
    rustup \
    trunk \
    sccache \
    llvm \
    clang

FROM rust-sccache AS rust-full-sccache
RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed \
    hidapi \
    protobuf     
USER luser
WORKDIR /home/luser
RUN rustup update
RUN rustup toolchain install stable-x86_64-unknown-linux-gnu --profile minimal
RUN rustup component add rustfmt
RUN rustup target add \
    x86_64-unknown-linux-musl \
    x86_64-unknown-linux-gnu \
    armv7-unknown-linux-musleabihf \
    arm-unknown-linux-musleabi \
    aarch64-unknown-linux-musl \
    aarch64-unknown-linux-gnu \
    wasm32-unknown-unknown \
    wasm32-wasip2

FROM base AS gitea_act_runner
RUN --mount=type=bind,source=woggioni.net.ca.pem,target=/root.pem trust anchor root.pem
RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed nodejs npm docker docker-buildx btrfs-progs hidapi protobuf rustup llvm clang trunk emscripten sccache
RUN --mount=type=cache,target=/var/cache/pacman pacman -S --noconfirm --needed gitea-act-runner jdk21-graalvm-bin jdk25-graalvm-bin jdk21-graalvm-ce-bin jdk25-graalvm-ce-bin go
RUN usermod -a luser -G docker
USER luser
WORKDIR /home/luser
COPY --chown=1000:1000 config.toml /home/luser/.cargo/config.toml
COPY --chown=1000:1000 sccache_config.toml /home/luser/.config/sccache/config
RUN rustup update
RUN rustup toolchain install stable-x86_64-unknown-linux-gnu --profile minimal
RUN rustup component add rustfmt
RUN rustup component add clippy
RUN rustup target add \
    x86_64-unknown-linux-musl \
    x86_64-unknown-linux-gnu \
    armv7-unknown-linux-musleabihf \
    arm-unknown-linux-musleabi \
    aarch64-unknown-linux-musl \
    aarch64-unknown-linux-gnu \
    wasm32-unknown-unknown \
    wasm32-wasip2
RUN cargo install worker-build
RUN cargo install wasm-bindgen-cli
RUN npm install -D wrangler
ENV PATH="/home/luser/.local/bin:/home/luser/.cargo/bin:${PATH}"
CMD ["/usr/lib/gitea/act_runner/act_runner", "daemon", "--config", "/var/lib/gitea/act_runner.yaml"]
