# luochen570-cli - Alpine-based CLI toolkit
FROM alpine:latest

LABEL org.opencontainers.image.title="luochen570-cli"
LABEL org.opencontainers.image.description="Alpine-based CLI toolkit: zsh+zinit plugins, NvChad, and common CLI tools"
LABEL org.opencontainers.image.source="https://git.luochen570.cn/luochen570/luochen570-cli"

# ---- Enable community repo ----
RUN echo "https://dl-cdn.alpinelinux.org/alpine/v3.24/community" >> /etc/apk/repositories

# ---- Install Alpine packages ----
RUN apk add --no-cache \
    # Core
    zsh git curl ca-certificates openssl shadow \
    # CLI tools (some from community repo)
    eza fzf ncdu yazi bat tree \
    # neovim (for NvChad)
    neovim \
    # runtime deps
    tmux

# ---- Install duf from GitHub (not in Alpine repos) ----
ARG TARGETARCH
ARG DUF_VERSION=0.9.1
RUN DUF_ARCH="${TARGETARCH}" \
    && [ "${DUF_ARCH}" = "amd64" ] && DUF_ARCH="x86_64" \
    && curl -fsSL "https://github.com/muesli/duf/releases/download/v${DUF_VERSION}/duf_${DUF_VERSION}_linux_${DUF_ARCH}.tar.gz" \
      | tar xz -C /usr/local/bin duf

# ---- Set up zinit (zsh plugin manager) ----
ENV ZINIT_HOME=/root/.local/share/zinit/zinit.git
RUN mkdir -p "$(dirname "$ZINIT_HOME")" \
    && git clone --depth 1 https://github.com/zdharma-continuum/zinit.git "$ZINIT_HOME"

# ---- Copy zsh configs ----
COPY configs/.zshrc /root/.zshrc
COPY configs/.p10k.zsh /root/.p10k.zsh

# ---- Pre-install zinit plugins (including deferred/wait plugins) ----
RUN zsh -c 'source /root/.zshrc 2>/dev/null; \
    zpcdreplay 2>/dev/null; \
    zinit update --all 2>/dev/null; \
    # Force-load deferred plugins to ensure they are cloned
    for plugin in \
      zdharma-continuum/fast-syntax-highlighting \
      zsh-users/zsh-autosuggestions; do \
      zinit light "$plugin" 2>/dev/null; \
    done; \
    # Force-load deferred OMZ snippets
    for snippet in sudo z git extract kubectl colored-man-pages; do \
      zinit snippet "OMZP::${snippet}" 2>/dev/null; \
    done; \
    zinit update --all 2>/dev/null; \
    exit 0'

# ---- Set up NvChad (neovim IDE config) ----
RUN git clone --depth 1 https://github.com/NvChad/starter /root/.config/nvim \
    && rm -rf /root/.config/nvim/.git

# ---- Set default shell ----
SHELL ["/bin/zsh", "-c"]
ENV SHELL=/bin/zsh
CMD ["/bin/zsh"]
