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

_realname=tree-sitter-lua
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.4.0
pkgrel=2
pkgdesc='Lua grammar for tree-sitter (mingw-w64)'
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/tree-sitter-grammars/tree-sitter-lua'
msys2_repository_url='https://github.com/tree-sitter-grammars/tree-sitter-lua'
msys2_references=(
  'archlinux: tree-sitter-lua'
)
license=('spdx:MIT')
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-tree-sitter"
)
source=("${_realname}-${pkgver}.tar.gz::${msys2_repository_url}/releases/download/v${pkgver}/${_realname}.tar.gz")
sha256sums=('e01fa3a1531f62be02d0c4c22b5b917eaeec7eb3dc15b562015bc1c60deeb8f2')
noextract=("${_realname}-${pkgver}.tar.gz")

prepare() {
  mkdir -p "${_realname}-${pkgver}"
  tar -xf "${_realname}-${pkgver}.tar.gz" -C "${_realname}-${pkgver}" || true
}

build() {
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX" \
    cmake \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      -Wno-dev \
      -DCMAKE_BUILD_TYPE=None \
      -DCMAKE_SHARED_LIBRARY_SUFFIX_C=.so \
      -DCMAKE_INSTALL_BINDIR=lib \
      -S "${_realname}-${pkgver}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"

  # 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 "${_realname}-${pkgver}"/LICENSE.md "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE.md"
}
