# Maintainer: Biswapriyo Nath <nathbappai@gmail.com>

_realname=neovim
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.11.4
pkgrel=3
pkgdesc='Fork of Vim aiming to improve user experience, plugins, and GUIs (mingw-w64)'
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://neovim.io/'
msys2_repository_url='https://github.com/neovim/neovim'
msys2_references=(
  'archlinux: neovim'
)
license=('spdx:Apache-2.0')
depends=("${MINGW_PACKAGE_PREFIX}-gettext-runtime"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libluv"
         "${MINGW_PACKAGE_PREFIX}-libtermkey"
         "${MINGW_PACKAGE_PREFIX}-libutf8proc"
         "${MINGW_PACKAGE_PREFIX}-libuv"
         "${MINGW_PACKAGE_PREFIX}-libvterm"
         "${MINGW_PACKAGE_PREFIX}-luajit"
         "${MINGW_PACKAGE_PREFIX}-msgpack-c"
         "${MINGW_PACKAGE_PREFIX}-libtree-sitter"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-c"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-lua"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-markdown"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-query"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-vim"
         "${MINGW_PACKAGE_PREFIX}-tree-sitter-vimdoc"
         "${MINGW_PACKAGE_PREFIX}-unibilium")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-make"
             "${MINGW_PACKAGE_PREFIX}-lua51-mpack"
             "${MINGW_PACKAGE_PREFIX}-gettext-tools"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "0001-cmake-disable-bundling.patch"
        "https://github.com/neovim/neovim/commit/f4fc769c81af6f8d9235d59aec75cfe7c104b3ce.patch")
sha256sums=('83cf9543bedab8bec8c11cd50ccd9a4bf1570420a914b9a28f83ad100ca6d524'
            '356ecd2d571ef9bc30bdcdc55dc27b39a9283019dadaac748b508cb9f71ea0f5'
            '9faf574a78ce35217271b70c20a365b97f4921e04ac4fb984ef8a1e23280c700')


prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/0001-cmake-disable-bundling.patch"
  # uncomment when we update tree-sitter to 0.26.0
  #patch -p1 -i "${srcdir}/f4fc769c81af6f8d9235d59aec75cfe7c104b3ce.patch"

  # Werror is enabled in CI only
  sed "s/-Werror/-Wall/g" -i src/nvim/CMakeLists.txt

  find . -type f -name "*.orig" -exec rm -f {} \;
}

build() {
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-format -Wno-format-extra-args"
  CXXFLAGS+=" -Wno-format -Wno-format-extra-args"

  # Build only lpeg statically
  "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -DUSE_BUNDLED=OFF \
      -DUSE_BUNDLED_LPEG=ON \
      "${extra_config[@]}" \
      -B "${srcdir}/${_realname}-${pkgver}/.deps" \
      "${srcdir}/${_realname}-${pkgver}/cmake.deps"

  "${MINGW_PREFIX}"/bin/cmake.exe --build "${srcdir}/${_realname}-${pkgver}/.deps"

  # MSYS2_ARG_CONV_EXCL is necessary so that -DCMAKE_INSTALL_PREFIX is passed correctly
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DENABLE_TRANSLATIONS=ON \
      ../${_realname}-${pkgver}

  "${MINGW_PREFIX}"/bin/cmake.exe --build "${srcdir}/build-${MSYSTEM}"
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"

  ./bin/nvim.exe --version
  ./bin/nvim.exe --headless -u NONE -i NONE -c ':quit'
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"

  DESTDIR="${pkgdir}" "${MINGW_PREFIX}"/bin/cmake.exe --install .

  install -Dm644 "${srcdir}"/${_realname}-${pkgver}/LICENSE.txt \
    "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.txt
}
