# OrionOS ISO builder
# Uses official Arch Linux as the base — provides pacman, pacstrap, archiso.
# Build context: the repo root (OS/)
#
# Build:  docker build -t orionos-builder -f iso/docker/Dockerfile .
# Run:    docker run --privileged -v $(pwd)/dist:/dist orionos-builder
# Output: dist/orionos.iso

FROM archlinux:latest

# ── disable pacman seccomp sandbox (breaks under QEMU on Apple Silicon) ──
RUN echo "DisableSandbox" >> /etc/pacman.conf

# ── install build tools ────────────────────────────────────────────────
RUN pacman -Syu --noconfirm && \
    pacman -S --noconfirm \
        archiso \
        base-devel \
        libx11 \
        libxft \
        fontconfig \
        freetype2 \
        imagemagick \
        fakeroot \
        git \
        sudo \
    && pacman -Scc --noconfirm

# ── build OnlyOffice from AUR (as a non-root build user) ─────────────
RUN useradd -m builder && \
    echo "builder ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/builder
RUN su builder -c " \
    git clone --depth=1 https://aur.archlinux.org/onlyoffice-bin.git /tmp/onlyoffice && \
    cd /tmp/onlyoffice && \
    makepkg -s --noconfirm 2>&1 | tail -5 \
    " && \
    find /tmp/onlyoffice -name '*.pkg.tar.zst' -exec cp {} /onlyoffice.pkg.tar.zst \; || \
    echo "OnlyOffice AUR build failed — will be skipped in ISO"

# ── show which mkinitcpio hooks archiso installed (helps debugging) ───
RUN echo "=== archiso initcpio hooks ===" && \
    ls /usr/lib/initcpio/hooks/ 2>/dev/null || echo "(none found)"

# ── copy the entire OrionOS source tree ──────────────────────────────
WORKDIR /orionos
COPY . .

# ── build the OrionOS binaries inside the container ───────────────────
# pkg-config finds X11/Xft headers automatically on Arch — no overrides needed
RUN make

# ── generate the default wallpaper ───────────────────────────────────
RUN bash themes/wallpapers/generate-default.sh || true

# ── run the ISO build script ──────────────────────────────────────────
# --privileged is required for loop device mounts inside the container
ENTRYPOINT ["/orionos/iso/docker/entrypoint.sh"]
