# Copyright (c) 2026 neoche contributors
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0

# Stage 1: Download Neovim glibc portable build
FROM registry.access.redhat.com/ubi9/ubi-minimal:latest AS nvim-libc-downloader
RUN microdnf install -y tar gzip && microdnf clean all
ARG NVIM_VERSION=v0.10.4
RUN curl -fsSL https://github.com/neovim/neovim/releases/download/${NVIM_VERSION}/nvim-linux-x86_64.tar.gz \
    | tar xz -C /opt && mv /opt/nvim-linux-x86_64 /neovim-linux-libc

# Stage 2: Neovim for musl environments (Alpine package + bundled shared libs)
FROM alpine:3.21 AS nvim-musl-builder
RUN apk add --no-cache neovim
RUN mkdir -p /neovim-linux-musl/bin /neovim-linux-musl/lib /neovim-linux-musl/share \
    && cp /usr/bin/nvim /neovim-linux-musl/bin/ \
    && cp -r /usr/share/nvim /neovim-linux-musl/share/ \
    && ldd /usr/bin/nvim | awk '/=>/{print $3}' | while read lib; do \
         cp "$lib" /neovim-linux-musl/lib/; \
       done

# Stage 3: Download ttyd binaries (glibc and static/musl)
FROM registry.access.redhat.com/ubi9/ubi-minimal:latest AS ttyd-downloader
ARG TTYD_VERSION=1.7.7
RUN mkdir -p /ttyd-bin \
    && curl -fsSL -o /ttyd-bin/ttyd \
       https://github.com/tsl0922/ttyd/releases/download/${TTYD_VERSION}/ttyd.x86_64 \
    && chmod +x /ttyd-bin/*

# Stage 4: Download CLI tools (static binaries)
FROM registry.access.redhat.com/ubi9/ubi-minimal:latest AS tools-downloader
RUN microdnf install -y tar gzip && microdnf clean all
ARG RIPGREP_VERSION=14.1.1
ARG FD_VERSION=10.2.0
ARG FZF_VERSION=0.57.0
ARG LAZYGIT_VERSION=0.44.1
RUN mkdir -p /tools \
    && curl -fsSL https://github.com/BurntSushi/ripgrep/releases/download/${RIPGREP_VERSION}/ripgrep-${RIPGREP_VERSION}-x86_64-unknown-linux-musl.tar.gz \
       | tar xz --strip-components=1 -C /tools ripgrep-${RIPGREP_VERSION}-x86_64-unknown-linux-musl/rg \
    && curl -fsSL https://github.com/sharkdp/fd/releases/download/v${FD_VERSION}/fd-v${FD_VERSION}-x86_64-unknown-linux-musl.tar.gz \
       | tar xz --strip-components=1 -C /tools fd-v${FD_VERSION}-x86_64-unknown-linux-musl/fd \
    && curl -fsSL https://github.com/junegunn/fzf/releases/download/v${FZF_VERSION}/fzf-${FZF_VERSION}-linux_amd64.tar.gz \
       | tar xz -C /tools \
    && curl -fsSL https://github.com/jesseduffield/lazygit/releases/download/v${LAZYGIT_VERSION}/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz \
       | tar xz -C /tools lazygit \
    && chmod +x /tools/*

# Stage 5: machine-exec
FROM quay.io/eclipse/che-machine-exec:7.56.0 AS machine-exec

# Stage 6: Assemble the rootfs (UBI9 glibc 2.34 required by Neovim 0.10.4)
FROM registry.access.redhat.com/ubi9/ubi:9.5 AS rootfs-builder
RUN mkdir -p /mnt/rootfs
RUN dnf install --installroot /mnt/rootfs \
    ncurses-libs coreutils glibc-minimal-langpack bash \
    --releasever 9 --setopt install_weak_deps=false --nodocs -y \
    && dnf --installroot /mnt/rootfs clean all
RUN rm -rf /mnt/rootfs/var/cache/* /mnt/rootfs/var/log/dnf* /mnt/rootfs/var/log/yum.*

WORKDIR /mnt/rootfs

COPY --from=nvim-libc-downloader --chown=0:0 /neovim-linux-libc /mnt/rootfs/neovim-linux-libc
COPY --from=nvim-musl-builder --chown=0:0 /neovim-linux-musl /mnt/rootfs/neovim-linux-musl
COPY --from=ttyd-downloader --chown=0:0 /ttyd-bin /mnt/rootfs/neovim-bin
COPY --from=machine-exec --chown=0:0 /go/bin/che-machine-exec /mnt/rootfs/neovim-bin/machine-exec
COPY --from=tools-downloader --chown=0:0 /tools/ /mnt/rootfs/neovim-bin/

RUN mkdir -p /mnt/rootfs/projects /mnt/rootfs/home/che /mnt/rootfs/bin
RUN cat /mnt/rootfs/etc/passwd | sed s#root:x.*#root:x:\${USER_ID}:\${GROUP_ID}::\${HOME}:/bin/bash#g > /mnt/rootfs/home/che/.passwd.template \
    && cat /mnt/rootfs/etc/group | sed s#root:x:0:#root:x:0:0,\${USER_ID}:#g > /mnt/rootfs/home/che/.group.template
RUN for f in "/mnt/rootfs/neovim-bin/" "/mnt/rootfs/home/che" "/mnt/rootfs/etc/group" "/mnt/rootfs/projects"; do \
      chgrp -R 0 ${f} && chmod -R g+rwX ${f}; \
    done

COPY --chmod=755 build/scripts/*.sh /mnt/rootfs/

# Final image
FROM scratch
COPY --from=rootfs-builder /mnt/rootfs/ /
ENV HOME=/home/che
USER 1001
ENTRYPOINT ["/entrypoint.sh"]
