# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=pixman
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=0.40.0
pkgrel=2
pkgdesc="The pixel-manipulation library for X and cairo (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://xorg.freedesktop.org/"
license=(MIT)
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-libpng"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-ninja")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
options=('staticlibs' 'strip')
source=("https://xorg.freedesktop.org/releases/individual/lib/pixman-${pkgver}.tar.xz")
sha256sums=('da8ed9fe2d1c5ef8ce5d1207992db959226bd4e37e3f88acf908fd9a71e2704e')

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

build() {
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix="${MINGW_PREFIX}" \
    --buildtype plain \
    -Ddefault_library=both \
    -Dgtk=disabled \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/ninja
}

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

  ${MINGW_PREFIX}/bin/ninja test
}

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

  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/ninja install

  for pcfile in  "${pkgdir}${MINGW_PREFIX}"/lib/pkgconfig/*.pc; do
    # https://github.com/mesonbuild/meson/issues/3934
    sed -s "s|-lpixman-mmx||g" -i "${pcfile}"
    sed -s "s|-lpixman-sse2||g" -i "${pcfile}"
    sed -s "s|-lpixman-ssse3||g" -i "${pcfile}"
  done

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
