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-pylint \
    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 yay_builder

# prepare nobody makepkg user
RUN mkdir /home/build && \
    chgrp nobody /home/build && \
    chmod g+ws /home/build && \
    setfacl -m u::rwx,g::rwx /home/build && \
    setfacl -d --set u::rwx,g::rwx,o::- /home/build

RUN sudo pacman -Sy --noconfirm go && rm -rf /var/cache/pacman/pkg/*

# Cannot run makepkg as root
USER nobody

RUN git clone https://aur.archlinux.org/yay.git /home/build/yay
WORKDIR /home/build/yay
RUN env GOCACHE=/home/build makepkg

USER root
RUN pacman --noconfirm -U /home/build/yay/*zst

FROM base as final
WORKDIR /root

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

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

# bretellofier
RUN pip --no-cache install --upgrade pip git+https://git.decapod.one/brethil/bretellofier

# 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 "source $DOTFILES/brethil_dotfile.sh" >> .zshrc
RUN ln -s $DOTFILES/vim/vimrc .vimrc
RUN ln -s $DOTFILES/tmux.conf .tmux.conf
RUN echo -e "[include]\npath = $DOTFILES/gitconfig" >> .gitconfig

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

# Install zsh plugins
RUN zsh -il -c exit

CMD tmux