# Maintainer: Maksim Bondarenkov <maksapple2306@gmail.com>

_realname=tree-sitter-vim
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.7.0
pkgrel=1
pkgdesc='Vimscript grammar for tree-sitter (mingw-w64)'
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/tree-sitter-grammars/tree-sitter-vim'
msys2_references=(
  'archlinux: tree-sitter-vim'
)
license=('spdx:MIT')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-tree-sitter"
)
source=("https://github.com/tree-sitter-grammars/${_realname}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz")
sha256sums=('44eabc31127c4feacda19f2a05a5788272128ff561ce01093a8b7a53aadcc7b2')

prepare() {
  cd "${_realname}-${pkgver}"
  tree-sitter generate src/grammar.json
}

build() {
  cd "${_realname}-${pkgver}"
  # To get around Windows not being supported
  OS='unknown' make CC='cc' LINKSHARED='-shared' PREFIX="${MINGW_PREFIX}" PARSER_URL=$url
}

package() {
  install -d "${pkgdir}${MINGW_PREFIX}/lib/tree_sitter"
  cd "${_realname}-${pkgver}"
  OS='unknown' make DESTDIR="${pkgdir}" PREFIX="${MINGW_PREFIX}" install

  # To add library to neovim runtime path
  mkdir -p "${pkgdir}${MINGW_PREFIX}/lib/nvim/parser"
  cp "${pkgdir}${MINGW_PREFIX}/lib/lib${_realname}.so" "${pkgdir}${MINGW_PREFIX}/lib/nvim/parser/${_realname#tree-sitter-}.so"

  install -Dm644 LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
