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

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

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

  cd "../tree-sitter-markdown-inline"
  tree-sitter generate src/grammar.json
}

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"
  cp "${pkgdir}${MINGW_PREFIX}/lib/lib${_realname}-inline.so" "${pkgdir}${MINGW_PREFIX}/lib/nvim/parser/${_realname#tree-sitter-}_inline.so"

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