FROM scratch as system
COPY system_files /

FROM scratch as eve
COPY eve_executables/eve-lite /usr/bin/eve-lite
COPY eve_executables/eventPlugin /usr/bin/eve-lite-event-plugin
COPY eve_executables/eve-lite-ui.AppImage /Applications/eve-lite.AppImage

FROM registry.fedoraproject.org/fedora:42 AS strfry-builder

RUN dnf5 -y install perl-FindBin perl-File-Find gcc-c++ flatbuffers-devel flatbuffers-compiler openssl-devel lmdb-devel zlib-devel libsecp256k1 libsecp256k1-devel perl-core zstd libzstd-devel git make

RUN git clone https://github.com/hoytech/strfry /tmp/strfry && \
    cd /tmp/strfry && \
    git submodule update --init --recursive && \
    make setup-golpe && \
    make -j$(nproc)

FROM ghcr.io/wayblueorg/hyprland-nvidia-open:latest

COPY --from=strfry-builder /tmp/strfry/strfry /usr/bin/strfry

RUN dnf5 -y install \
  openssl-devel lmdb-devel zlib-devel \
  libsecp256k1 libsecp256k1-devel \
  zstd libzstd-devel \
  gnome-keyring libsecret \
  mpv \
  qt6-qtsvg qt6-qtvirtualkeyboard qt6-qtmultimedia \
  NetworkManager-tui \
  systemd-boot-unsigned

RUN dnf5 -y remove firewalld tuned && rpm -e --noscripts systemd-resolved systemd-resolved

RUN sed -i 's/#AutomaticUpdatePolicy.*/AutomaticUpdatePolicy=stage/' /etc/rpm-ostreed.conf

COPY cosign.pub /etc/pki/containers/eveos.pub

RUN --mount=type=bind,from=system,source=/,target=/system --mount=type=bind,from=eve,source=/,target=/eve \
    rsync -avzp --include ".*" /system/ / && \
    find /system/usr/etc -type f -mindepth 1 -printf '/etc/%P\0' | xargs -0 rm -rf -- && \
    find /system/usr/etc -type f -mindepth 1 -printf '%P\0' | \
        xargs -0 -I {} sh -c 'mkdir -p $(dirname /etc/{}) && ln -sf /usr/etc/{} /etc/{}' && \
    rsync -avzp --include ".*" /system/ / && \
    rsync -avzp --include ".*" /eve/ / && \
    chmod +x /usr/bin/eve-lite && \
    chmod +x /Applications/eve-lite.AppImage && \
    chmod +x /usr/bin/eve-lite-event-plugin && \
    cat /usr/share/hypr/hyprland.conf > /usr/share/hyprland/hyprland.conf

RUN systemctl --global enable eve-lite-relay && \
    systemctl enable bluetooth && \
    systemctl enable nftables && \
    systemctl disable systemd-resolved && \
    systemctl enable cpu-performance && \
    systemctl enable disable-power-features && \
    systemctl disable systemd-binfmt && \
    systemctl mask systemd-binfmt

RUN ldconfig && update-mime-database /usr/share/mime && gtk-update-icon-cache && dconf update

RUN ostree container commit
