FROM archlinux:latest as base

ENV TERM=xterm-256color
ENV DOTFILES=/root/.dotfiles

RUN pacman --noconfirm -Syu && pacman --noconfirm -Sy \
    vim \
    tmux \
    git \
    fzf \
    base-devel \
    ipython \
    python-pip \
    htop \
    python-ruff \
    python-virtualenv \
    bash-language-server \
    && rm -rf /var/cache/pacman/pkg/*

# Enable colors in pacman
RUN sed -i 's/^#Color/Color/' /etc/pacman.conf

FROM base as builder

# Cannot run makepkg as root: add build user
RUN useradd build && echo "build ALL=(ALL:ALL) NOPASSWD: ALL" >> /etc/sudoers

USER build

# yay (pre-compiled) and bretellofier
WORKDIR /home/build/yay
RUN git clone https://aur.archlinux.org/yay-bin.git /home/build/yay  && \
    git clone https://aur.archlinux.org/bretellofier.git /home/build/bretellofier && \
    cd /home/build/yay && env GOCACHE=/home/build makepkg --syncdeps --noconfirm && \
    cd /home/build/bretellofier && makepkg --syncdeps --noconfirm


FROM base as final
WORKDIR /root

RUN pacman -Sy --noconfirm zsh && chsh -s /bin/zsh

COPY --from=builder /home/build/yay/*zst /tmp/
COPY --from=builder /home/build/bretellofier/*zst /tmp/
RUN pacman --noconfirm -U /tmp/*zst && rm -rf /tmp/*zst

# antibody
RUN curl -sL https://git.io/antibody | sh -s - -b /usr/local/bin

# vim-plug
RUN curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim

RUN git clone https://git.decapod.one/brethil/dotfiles ${DOTFILES}
COPY . ${DOTFILES}

WORKDIR /root/

RUN echo -e "export DOTFILES=/root/.dotfiles\nsource \$DOTFILES/brethil_dotfile.sh" >> .zshrc && \
    ln -s $DOTFILES/vim/vimrc .vimrc && \
    ln -s $DOTFILES/tmux.conf .tmux.conf && \
    echo -e "[include]\npath = $DOTFILES/gitconfig" >> .gitconfig

# Install vim plugins
RUN vim -c 'PlugInstall|qa!'

# Install zsh plugins
RUN zsh -c exit

CMD ["tmux"]