# Maintainer: Ilya Rakhlin <i.rakhlin@gmail.com>

_realname=pkgconf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.7.4
pkgrel=2
pkgdesc='pkg-config compatible utility which does not depend on glib'
url='https://github.com/pkgconf/pkgconf'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('ISC')
options=('!debug' 'strip')
provides=("${MINGW_PACKAGE_PREFIX}-pkg-config")
conflicts=("${MINGW_PACKAGE_PREFIX}-pkg-config")
replaces=("${MINGW_PACKAGE_PREFIX}-pkg-config")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja")
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
source=("https://github.com/pkgconf/pkgconf/archive/refs/tags/$_realname-$pkgver.tar.gz"
        0001-fix-prefix-dirsep.patch
        0002-size-t-format.patch
        0003-printf-format.patch
        0004-default-pure-static.patch)
sha256sums=('2828dcdef88098748c306281d64a630b4ccd0703b1d92b532c31411e531d3088'
            'fd446cc3f35528d6d3f67b5c46fca6e4fbfee3a35a8e0bae3d14e5ad020b7c8c'
            '1db5975d6b1db946ba1d71cc3cf390dec567899a89d7ef53f56f24f0058cdbe3'
            'a7efeef61b4af1c0813d9d8a211cde669a3e3fa2c97ed21e55487667769c567a'
            '93a4aa1475122d16b6ac8a84f771e004e1a53e1df982d3dd069b262c9815ebb4')

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

  # https://github.com/pkgconf/pkgconf/pull/217
  patch -p1 -i ${srcdir}/0001-fix-prefix-dirsep.patch

  # https://github.com/msys2/MINGW-packages/issues/8473
  patch -R -p1 -i ${srcdir}/0004-default-pure-static.patch

  patch -p1 -i ${srcdir}/0002-size-t-format.patch
  patch -p1 -i ${srcdir}/0003-printf-format.patch
}

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

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Dtests=false \
    ../${_realname}-${pkgver}

  meson compile
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}

  DESTDIR="${pkgdir}" meson install

  cp "${pkgdir}${MINGW_PREFIX}"/bin/pkgconf "${pkgdir}${MINGW_PREFIX}"/bin/pkg-config
  cp "${pkgdir}${MINGW_PREFIX}"/bin/pkgconf "${pkgdir}${MINGW_PREFIX}"/bin/${MINGW_CHOST}-pkgconf
  cp "${pkgdir}${MINGW_PREFIX}"/bin/pkg-config "${pkgdir}${MINGW_PREFIX}"/bin/${MINGW_CHOST}-pkg-config
}
