# Maintainer: Andrew Chadwick <a.t.chadwick@gmail.com>
# Contributor: Andrew Sun <adsun701@gmail.com>

_realname=json-c
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.18
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64')
pkgdesc="A JSON implementation in C (mingw-w64)"
url='https://github.com/json-c/json-c'
msys2_references=(
  "cpe: cpe:/a:json-c_project:json-c"
)
depends=("${MINGW_PACKAGE_PREFIX}-cc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
license=('spdx:MIT')
source=(https://github.com/json-c/json-c/archive/${_realname}-${pkgver//_/-}.tar.gz
        001-install-private-header.patch)
sha256sums=('8069b5e6100bb8b9908dcfdd368f0e99bdeea2cb139cba558e536715c8c8ac8e'
            'ac8c5c306bae8fc098c1420c78eab9c9d026f874614f24466987af5f30c70b20')
noextract=(${_realname}-${pkgver//_/-}.tar.gz)

prepare() {
  tar -xzf ${_realname}-${pkgver}.tar.gz -C "${srcdir}" || true
  cd "${srcdir}/${_realname}-${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}"/001-install-private-header.patch
}

build() {
  mkdir -p build-${MSYSTEM} && cd 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 \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_DLL_NAME_WITH_SOVERSION=ON \
    ${_extra_config[@]} \
    -DBUILD_TESTING=OFF \
    -DDISABLE_WERROR=ON \
    ../${_realname}-${_realname}-${pkgver}

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

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --install ./
  install -vDm644 "${srcdir}"/${_realname}-${_realname}-${pkgver}/COPYING "${pkgdir}"${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
