#!/bin/bash

## This script takes care of the vim configuration.

if [ -z "$BASE_DIR" ]; then
  echo "BASE_DIR wasn't set."
  exit 1
fi

# Add .vimrc {{{
######
# OK #
######

echo "Adding .vimrc..."
make_symlink ~/.vimrc "$BASE_DIR"/dotfiles/vim/vimrc

# }}}

# Add colors {{{
######
# OK #
######

echo "Adding colors..."
mkdir -p ~/.vim/colors

color_files=$(ls "$BASE_DIR"/dotfiles/vim/colors)
for color_file in $color_files; do
  make_symlink ~/.vim/colors/"$color_file" "$BASE_DIR"/dotfiles/vim/colors/"$color_file"
done

# }}}

# Install or update Vundle {{{
######
# OK #
######

echo "Setting up Vundle..."
if [ ! -e ~/.vim/bundle/Vundle.vim ]; then
  echo "Cloning Vundle from Github..."
  if git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim; then
    echo "${GREEN}Successfully cloned Vundle${NC}"
  else
    echo "${RED}There was a problem while setting up Vundle${NC}"
    exit 1
  fi
else
  echo "Vundle is already installed, updating"
  if cd ~/.vim/bundle/Vundle.vim && git pull; then
    echo "${GREEN}Successfully updated Vundle${NC}"
  else
    echo "${RED}There was a problem while updating Vundle${NC}"
    exit 1
  fi
fi

# }}}

# Update Vundle plugins {{{
######
# OK #
######

echo "Checking Vundle Plugins..."
if vim +PluginInstall +qall; then
  echo "${GREEN}All Vundle Plugins are enabled.${NC}"
else
  echo "${RED}There was a problem while checking Vundle Plugins.${NC}"
  exit 1
fi
if [ "$UPDATE" = 'true' ]; then
  echo "Updating Vundle Plugins..."
  if vim +PluginUpdate +qall; then
    echo "${GREEN}Successfully updated Vundle Plugins${NC}"
  else
    echo "${RED}There was a problem while updating Vundle Plugins${NC}"
    exit 1
  fi
fi

# }}}

# Setup git-submodules for python-mode {{{
######
# OK #
######

echo "Setting up Python-mode folder for git submodules..."
if [ -e ~/.vim/bundle/python-mode ]; then
  cd ~/.vim/bundle/python-mode || exit 1
  if git submodule update --init --recursive; then
    echo "${GREEN}Successfully set up submodules for python-mode${NC}"
  else
    echo "${RED}There was a problem while setting up submodules for python-mode${NC}"
    exit 1
  fi
fi

# }}}

# Install linters {{{
######
# OK #
######

echo "Installing linters..."
if [ "$OS" = 'debian' ]; then
  sudo apt install flake8 pylint3 mypy shellcheck
elif [ "$OS" = 'arch' ]; then
  sudo pacman -S --needed flake8 python-pylint mypy
  if ! sudo pacman -S --needed shellcheck; then
    echo "${ORANGE}Shellcheck was not found with pacman, downloading pre-compiled binary${NC}"
    scversion="stable"
    architechture=$(uname -m)
    if [ -e ~/Downloads/shellcheck-${scversion} ]; then
      rm -r ~/Downloads/shellcheck-${scversion} && echo "${ORANGE}Found and removed pre-existing shellcheck folder${NC}"
    fi
    cd ~/Downloads || exit 1
    echo "Downloading shellcheck ${scversion} binary for ${architechture}..."
    if wget -qO- "https://storage.googleapis.com/shellcheck/shellcheck-${scversion?}.linux.${architechture?}.tar.xz" | tar -xJv; then
      echo "${GREEN}Successfully downloaded and extracted shellcheck.${NC}"
      echo "Copying executable to /usr/bin/ ..."
      sudo cp "shellcheck-${scversion}/shellcheck" /usr/bin/ && shellcheck --version
      if rm -r ~/Downloads/shellcheck-${scversion}; then
        echo "${GREEN}Removed shellcheck installation folder${NC}"
      else
        echo "${RED}Was unable to remove shellcheck installation folder${NC}"
        exit 1
      fi
    else
      echo "${RED}Was unable to download shellcheck binary${NC}"
    fi
  fi
else
  echo "${RED}Was unable to determine which OS you use${NC}"
  exit 1
fi

# }}}

# Install YouCompleteMe {{{
######
# OK #
######

echo "Setting up YouCompleteMe..."
if [ ! -e ~/.vim/ycm_installed ]; then
  if [ "$OS" = 'debian' ]; then
    sudo apt install build-essential cmake python3-dev
  elif [ "$OS" = 'arch' ]; then
    sudo pacman -S --needed base-devel clang cmake
  else
    echo "${RED}Was unable to determine which OS you use${NC}"
    exit 1
  fi
  cd ~/.vim/bundle/YouCompleteMe || exit 1
  if /usr/bin/python3 install.py --clang-completer --rust-completer; then
    echo "${GREEN}Successfully set up YouCompleteMe${NC}"
    cd ~/.vim || exit 1
    touch ycm_installed
  else
    echo "${RED}There was a problem while setting up YouCompleteMe${NC}"
    exit 1
  fi
else
  echo "YouCompleteMe is already installed, skipping..."
fi

# }}}

# Add YouCompleteMe extra conf files {{{
######
# OK #
######

echo "Adding YouCompleteMe extra_conf files..."

make_symlink ~/.ycm_extra_conf.py "$BASE_DIR"/dotfiles/vim/ycm_extra_conf.py
make_symlink ~/.global_extra_conf.py "$BASE_DIR"/dotfiles/vim/global_extra_conf.py

# }}}

exit 0