# Contributor: Mehdi Chinoune <mehdi.chinoune@hotmail.com>

_realname=neovim-qt
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.2.19
pkgrel=1
pkgdesc="Neovim client library and GUI, in Qt6. (mingw-w64)"
arch=('any')
mingw_arch=('ucrt64' 'clang64' 'clangarm64')
url='https://github.com/equalsraf/neovim-qt'
msys2_references=(
  'archlinux: neovim-qt'
)
license=('spdx:ISC')
depends=("${MINGW_PACKAGE_PREFIX}-qt6-svg"
         "${MINGW_PACKAGE_PREFIX}-neovim"
         "${MINGW_PACKAGE_PREFIX}-msgpack-c")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-qt6-tools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-gtest")
source=("https://github.com/equalsraf/neovim-qt/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        "001-disable-deployment.patch")
sha256sums=('2c5a5de6813566aeec9449be61e1a8cd8ef85979a9e234d420f2882efcfde382'
            '31ae40b26be048367afc1a5609bfb8dcfdd0681a584d6cb060126d385d2308e1')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}

  patch -Np1 -i "${srcdir}"/001-disable-deployment.patch
}

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

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    "${MINGW_PREFIX}"/bin/cmake.exe \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${extra_config[@]}" \
      -DWITH_QT=Qt6 \
      -DUSE_SYSTEM_MSGPACK=ON \
      -DENABLE_TESTS=OFF \
      ../${_realname}-${pkgver}

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

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

  "${MINGW_PREFIX}"/bin/cmake.exe -DENABLE_TESTS=ON ../${_realname}-${pkgver}
  "${MINGW_PREFIX}"/bin/cmake.exe --build .
  "${MINGW_PREFIX}"/bin/ctest.exe --output-on-failure || "Tests failed"
}

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

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

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