#!/bin/bash

set -eux
set -o pipefail

./install-package --deb-packages cargo
cargo install tree-sitter-cli
# ./install-package --deb-packages 'xclip wl-clipboard' --brew-packages 'pbcopy pbpaste'
./install-package --deb-packages 'xclip wl-clipboard'
./install-package --deb-packages ripgrep --brew-packages ripgrep
./install-package --deb-packages nodejs --brew-packages nodejs
(cd install-golang && make install)

# Cleanup/create some backup hierarchies
for directory in ~/.config/nvim ~/.local/share/nvim ~/.local/state/nvim ~/cache/nvim
do
    if [ -d "$directory".bak ]
    then 
        rm -rf "$directory".bak
    fi
    if [ -d "$directory" ]
    then
        mv "$directory"{,.bak}
    fi
done

# This didn't work...
# sudo add-apt-repository ppa:lazygit-team/daily
# ...so:
case "$(uname -s)" in
    Linux)
        if ! [ -f /usr/local/bin/lazygit ]
        then
	        set -x
            LAZYGIT_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | grep -Po '"tag_name": "v\K[^"]*')    LAZYGIT_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | grep -Po '"tag_name": "v\K[^"]*')
            # This gets the version on MacOS, but there's little point in downloading and installing the lazygit for Linux on MacOS :)
            # LAZYGIT_VERSION=$(curl -s "https://api.github.com/repos/jesseduffield/lazygit/releases/latest" | \
            #     grep '"tag_name"' | \
            #     sed 's/"//g' | \
            #     awk ' { print $2 }' | \
            #     sed -e 's/^v//' -e 's/,$//'
            # )
            curl -Lo lazygit.tar.gz "https://github.com/jesseduffield/lazygit/releases/latest/download/lazygit_${LAZYGIT_VERSION}_Linux_x86_64.tar.gz"
            tar xf lazygit.tar.gz lazygit
            sudo install lazygit /usr/local/bin
            rm -rf lazygit lazygit.tar.gz
	        set +x
        fi
        ;;
    Darwin)
        brew install jesseduffield/lazygit/lazygit
        ;;
    *)
        echo "$0: unknown OS for lazygit" 1>&2
        exit 1
        ;;
esac

#################################
# Actually install AstroNvim :) #
#################################

############################################################################
# This is how the doc says to do it, but it only gives us the latest.      #
# git clone --depth 1 https://github.com/AstroNvim/template ~/.config/nvim #
# remove template's git connection to set up your own later                #
# rm -rf ~/.config/nvim/.git                                               #
############################################################################

git clone https://github.com/AstroNvim/template ~/.config/nvim
# Use AstroNvim V4, not v5.  I very likely want v5 someday, but not today.
(set -eu && cd ~/.config/nvim && git checkout 9e3be30b92ed4c210c5fa27883ad805cccdef712)


# https://github.com/neovim/nvim-lspconfig/blob/master/doc/server_configurations.md
# sourcery might be interesting as a way of refactoring Python code.
mkdir -p ~/.config/ruff
cp lib/pyproject.toml ~/.config/ruff/pyproject.toml
cp lib/scripts.vim ~/.config/nvim/scripts.vim
cp my_*.lua ~/.config/nvim/lua/plugins/.