# Maintainer: Benedikt Bitterli <benedikt.bitterli@gmail.com>

_realname=rapidjson
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.1.0
pkgrel=2
pkgdesc='A fast JSON parser/generator for C++ with both SAX/DOM style API (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url='https://github.com/miloyip/rapidjson'
license=('MIT')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-doxygen")
source=("${_realname}-${pkgver}.tar.gz::https://github.com/miloyip/rapidjson/archive/v${pkgver}.tar.gz"
        '0001-Use-relative-paths-for-doxygen-outputs.patch'
        '0002-Add-CMake-check-for-MinGW.patch'
        '0003-Exclude-CMakeLists.txt-from-install-targets.patch'
        '0004-CMake-Install-compiled-example-executables.patch')
sha256sums=('bf7ced29704a1e696fbccf2a2b4ea068e7774fa37f6d7dd4039d0787f8bed98e'
            '5a838fe611b367f8939231aae1f604f83fd97108d3b2ac66def00ff1205afcf2'
            '8160d2ef142055cecb520b23cf11e90e0a6e92719427fa33755800cc748cf8be'
            '04ba9821bb09d5f035093bce09690717101f03851308aa12d498311f11024b02'
            '479b8654f0418aa89f245af4345e5df409ac5a91667815a47592da1b6d5d9eee')

prepare() {
  cd "${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0001-Use-relative-paths-for-doxygen-outputs.patch"
  patch -p1 -i "${srcdir}/0002-Add-CMake-check-for-MinGW.patch"
  patch -p1 -i "${srcdir}/0003-Exclude-CMakeLists.txt-from-install-targets.patch"
  patch -p1 -i "${srcdir}/0004-CMake-Install-compiled-example-executables.patch"

  find -name CMakeLists.txt | xargs sed -e 's|-Werror||' -i # Don't use -Werror
}

build() {
  [[ -d "build-${CARCH}" ]] && rm -rf "build-${CARCH}"
  mkdir "${srcdir}/build-${CARCH}" && cd "${srcdir}/build-${CARCH}"

  MSYS2_ARG_CONV_EXCL='-DCMAKE_INSTALL_PREFIX=' \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
    "../${_realname}-${pkgver}"

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

package() {
  cd "${srcdir}/build-${CARCH}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/license.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/license.txt"
}
