#!/bin/sh
#
# Install and setup a Vim or Neovim for running tests.
# This should work on both GitHub Actions and people's desktop computers, and
# be 100% independent from any system installed Vim.
#
# It will echo the full path to a Vim binary, e.g.:
#   /some/path/src/vim

set -euC

vimgodir=$(cd -P "$(dirname "$0")/.." > /dev/null && pwd)
cd "$vimgodir"

vim=${1:-}

case "$vim" in
  "vim-8.0")
    # This follows the version in Ubuntu LTS. Vim's master branch isn't always
    # stable, and we don't want to have the build fail because Vim introduced a
    # bug.
    tag="v8.0.1453"
    giturl="https://github.com/vim/vim"
    ;;

  "vim-8.2")
    # This is the version that's installed by homebrew currently. It doesn't
    # have to stay up to date with homebrew, and is only chosen here because
    # that's what homebrew was using at the the time and we need a version to
    # vimlint with.
    tag="v8.2.0200"
    giturl="https://github.com/vim/vim"
    ;;

  "nvim")
    # Use latest stable version.
    tag="v0.4.0"
    giturl="https://github.com/neovim/neovim"
    ;;

  *)
    echo "unknown version: '${1:-}'"
    echo "First argument must be 'vim-8.0', vim-8.2, or 'nvim'."
    exit 1
    ;;
esac

srcdir="/tmp/vim-go-test/$1-src"
installdir="/tmp/vim-go-test/$1-install"

# Use cached installdir.
if [ -d "$installdir" ]; then
  echo "$installdir exists; skipping build."

  # The ./scripts/test script relies on this.
  echo "installed to: $installdir"
  exit 0
fi

mkdir -p "$srcdir"
cd "$srcdir"

# Neovim build requires more deps than Vim and is annoying, so we use the
# binary.
# 0.2.0 doesn't have a binary build for Linux, so we use 0.2.1-dev for now.
if [ "$1" = "nvim" ]; then

  # TODO: Use macOS binaries on macOS
  curl -Ls https://github.com/neovim/neovim/releases/download/$tag/nvim-linux64.tar.gz |
    tar xzf - -C /tmp/vim-go-test/
  mv /tmp/vim-go-test/nvim-linux64 /tmp/vim-go-test/nvim-install
  mkdir -p "$installdir/share/nvim/runtime/pack/vim-go/start"
  ln -s "$vimgodir" "$installdir/share/nvim/runtime/pack/vim-go/start/vim-go"

  # Consistent paths makes calling things easier.
  mv "$installdir/bin/nvim" "$installdir/bin/vim"
  mkdir -p "$installdir/share/vim/vimgo/pack"
  ln -s "$installdir/share/nvim/runtime/pack/vim-go" "$installdir/share/vim/vimgo/pack/vim-go"

# Build Vim from source.
else
  if [ -d "$srcdir/.git" ]; then
    echo "Skipping clone as $srcdir/.git exists"
  else
    echo "Cloning $tag from $giturl"
    git clone --branch "$tag" --depth 1 "$giturl" "$srcdir"
  fi

  ./configure --prefix="$installdir" --with-features=huge --disable-gui
  make install
  mkdir -p "$installdir/share/vim/vimgo/pack/vim-go/start"
  ln -s "$vimgodir" "$installdir/share/vim/vimgo/pack/vim-go/start/vim-go"
fi

# Don't really need source after successful install.
rm -rf "$srcdir"

echo "installed to: $installdir"

# vim:ts=2:sts=2:sw=2:et
