# Maintainer: Jaroslav Lichtblau <svetlemodry@archlinux.org>

_realname=sdcv
pkgbase="mingw-w64-${_realname}"
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.5.5
pkgrel=1
pkgdesc="StarDict Console Version (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://dushistov.github.io/sdcv/"
msys2_repository_url="https://github.com/Dushistov/sdcv"
license=('spdx:GPL-2.0-or-later')
depends=(
  "${MINGW_PACKAGE_PREFIX}-glib2"
  "${MINGW_PACKAGE_PREFIX}-readline"
  "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-ninja")
source=(https://github.com/Dushistov/${_realname}/archive/v$pkgver/${_realname}-$pkgver.tar.gz
        "001-sdcv-0.5.3-fix-mingw-build.patch")
sha256sums=('4d2519e8f8479b9301dc91e9cda3e1eefef19970ece0e8c05f0c7b7ade5dc94b'
            '7e629fe4a2337d7e700da4ae15b97b756a0ce2c0ccfefda58ddea308c99f2cdd')

prepare() {
  cd ${_realname}-${pkgver}
  patch -p1 -i "${srcdir}/001-sdcv-0.5.3-fix-mingw-build.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 \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      ${extra_config[@]} \
      -DBUILD_TESTS=True \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
  ${MINGW_PREFIX}/bin/cmake --build . --target lang
}

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

  ${MINGW_PREFIX}/bin/ctest
}

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

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