#!/bin/bash

vim_dir="$WORKSPACE/vim"

# Get the build dependencies
if [ "$PACKAGE_MANAGER" == 'apt-get' ]; then
  sudo apt-get build-dep vim vim-gtk
fi
# TODO: Add pacman support

# Build latest vim
if [ ! -d "$vim_dir" ]; then
    log "Cloning vim"
    git clone https://github.com/vim/vim.git "$vim_dir"
else
    (cd "$vim_dir" && git fetch)
fi

function conf_build_and_install() {
    # Get latest tagged source code
    latest_tag=$(git describe --tags)
    git checkout "$latest_tag"

    # Configure vim with ruby, python and GTK
    ./configure --with-features=huge \
        --enable-pythoninterp \
        --enable-rubyinterp \
        --enable-gui=gtk2

    # Compile
    make
    # Install newly compiled vim
    sudo make install
    # Link vi to vim out of convenience
    vim_path=$(command -v vim)
    sudo ln -s "$vim_path" "${vim_path:0:(-1)}"
}

(cd "$vim_dir" && conf_build_and_install)