FROM archlinux:latest

# Disable pacman's alpm sandbox (seccomp-incompatible inside Docker/BuildKit)
RUN echo "DisableSandbox" >> /etc/pacman.conf

# Update and install archiso + OrionOS build dependencies
RUN pacman -Syu --noconfirm && \
    pacman -S --noconfirm \
        archiso \
        syslinux \
        base-devel \
        git \
        libx11 \
        libxft \
        libxfixes \
        libxext \
        libpng \
        imlib2 \
        dbus \
        pam \
        fontconfig \
        freetype2 \
        xorg-server \
        xorg-xinit \
        feh \
        picom \
        alsa-utils \
        ttf-dejavu \
        noto-fonts \
        pkgconf && \
    pacman -Scc --noconfirm

WORKDIR /build

# Copy OrionOS source
COPY . /build/orionos

# Bootstrap boot loader configs from the installed archiso releng profile.
# syslinux/ is required for bios.syslinux.mbr; efiboot/ for systemd-boot UEFI.
# Fall back to a minimal loader.conf if releng ships without efiboot/.
RUN cp -r /usr/share/archiso/configs/releng/syslinux \
          /build/orionos/iso/profile/ && \
    if [ -d /usr/share/archiso/configs/releng/efiboot ]; then \
        cp -r /usr/share/archiso/configs/releng/efiboot \
              /build/orionos/iso/profile/; \
    else \
        mkdir -p /build/orionos/iso/profile/efiboot/loader/entries && \
        printf 'timeout 4\ndefault arch-*\n' \
            > /build/orionos/iso/profile/efiboot/loader/loader.conf; \
    fi

# Build OrionOS components (full output — no tail so errors are visible)
RUN cd /build/orionos && make

# Install binaries into the staging root that archiso will include
RUN cd /build/orionos && \
    install -Dm755 wm/orionwm              /build/orionos/iso/profile/airootfs/usr/local/bin/orionwm && \
    install -Dm755 deskbar/oriondeskbar    /build/orionos/iso/profile/airootfs/usr/local/bin/oriondeskbar && \
    install -Dm755 launcher/orionlauncher  /build/orionos/iso/profile/airootfs/usr/local/bin/orionlauncher && \
    install -Dm755 themes/orionwallpaper   /build/orionos/iso/profile/airootfs/usr/local/bin/orionwallpaper && \
    install -Dm755 orionnotify/orionnotify /build/orionos/iso/profile/airootfs/usr/local/bin/orionnotify && \
    install -Dm755 orionlock/orionlock     /build/orionos/iso/profile/airootfs/usr/local/bin/orionlock && \
    install -Dm755 orionshot/orionshot     /build/orionos/iso/profile/airootfs/usr/local/bin/orionshot && \
    install -Dm755 orionclip/orionclip     /build/orionos/iso/profile/airootfs/usr/local/bin/orionclip && \
    install -Dm755 orionpkg/orionpkg       /build/orionos/iso/profile/airootfs/usr/local/bin/orionpkg && \
    mkdir -p /build/orionos/iso/profile/airootfs/usr/share/themes/ && \
    cp -r themes/OrionOS /build/orionos/iso/profile/airootfs/usr/share/themes/ && \
    install -Dm644 themes/picom.conf       /build/orionos/iso/profile/airootfs/etc/orionos/picom.conf

# Generate default wallpaper
RUN mkdir -p /build/orionos/iso/profile/airootfs/usr/share/orionos/wallpapers && \
    bash /build/orionos/themes/wallpapers/generate-default.sh 2>/dev/null && \
    cp -f /build/orionos/themes/wallpapers/orionos-default.png \
          /build/orionos/iso/profile/airootfs/usr/share/orionos/wallpapers/ 2>/dev/null || true

# Build the ISO at container runtime — mkarchiso needs --privileged (from docker run)
# Output goes to /dist which build-iso.sh mounts from the host.
CMD mkarchiso -v \
    -w /tmp/iso-work \
    -o /dist \
    /build/orionos/iso/profile
