FROM binhex/arch-base:latest AS build

COPY ./aur-packages/srcpkgs/cage-vi/ /tmp/cage/

RUN pacman -Syu --noconfirm base-devel \
 && chown -R nobody: /tmp/cage/ \
 && source /tmp/cage/PKGBUILD && pacman -S --noconfirm ${depends[@]} ${makedepends[@]} \
 && runuser -u nobody -- sh -c "cd /tmp/cage/ && makepkg -c --noconfirm" 

FROM binhex/arch-base:latest

COPY --from=build /tmp/cage/*.pkg.tar.zst /tmp/cage/

COPY build/root/*.sh /root/
COPY config/ /config/
COPY supervisor/*.conf /etc/supervisor/conf.d/

RUN chmod +x /root/*.sh && \
    /bin/bash /root/install.sh

# TODO, move to install.sh
ENV PUID=10000 \
    PGID=10000 \
    UMASK=022 \
    WAYLAND_DISPLAY=wayland-0 \
    XDG_RUNTIME_DIR=/run/user/${PUID} \
    WLR_BACKENDS=headless \
    WLR_NO_HARDWARE_CURSORS=1 \
    WLR_DRM_DEVICES=/dev/dri/card0

RUN usermod -a -G seat,video,render nobody \
 && mkdir -p ${XDG_RUNTIME_DIR} \
 && chown ${PUID}:${PGID} ${XDG_RUNTIME_DIR} \
 && chmod 7700 ${XDG_RUNTIME_DIR}

EXPOSE 5900

CMD ["/bin/bash", "/usr/local/bin/init.sh"]