# 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.15
pkgrel=1
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
pkgdesc="A JSON implementation in C (mingw-w64)"
url='https://github.com/json-c/json-c'
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config")
options=('strip' '!debug' 'staticlibs')
license=('MIT')
source=(https://github.com/json-c/json-c/archive/${_realname}-${pkgver//_/-}.tar.gz
        001-install-private-header.patch
        002-library-version.patch)
sha256sums=('74985882e39467b34722e584ab836ed2abd47061888f318125fd4b167002afd5'
            '197e33a56fc3d655e058d6936e92698c8aa65b7297cb218345b8ad51350c4ecf'
            '2288c0ad1c0f98f2b43a8e4bdd09a6cf91710c7c8b1e7bf7a2b516ed21e81705')
noextract=(${_realname}-${pkgver//_/-}.tar.gz)

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

build() {
  [[ -d build-${MINGW_CHOST} ]] && rm -rf build-${MINGW_CHOST}
  mkdir -p build-${MINGW_CHOST} && cd build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    ../${_realname}-${_realname}-${pkgver}
  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR="${pkgdir}" install
  install -D ${srcdir}/${_realname}-${_realname}-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
