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

_realname=gdk-pixbuf2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.42.6
pkgrel=1
pkgdesc="An image loading library (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://gitlab.gnome.org/GNOME/gdk-pixbuf"
license=(LGPL2.1)
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-meson"
             "${MINGW_PACKAGE_PREFIX}-gi-docgen")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-glib2>=2.37.2"
         "${MINGW_PACKAGE_PREFIX}-libjpeg-turbo"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libtiff")
options=('emptydirs')
install=${_realname}-${MSYSTEM}.install
source=("https://download.gnome.org/sources/gdk-pixbuf/${pkgver%.*}/gdk-pixbuf-${pkgver}.tar.xz"
        0003-fix-dllmain.patch
        0004-build-all-loaders-plus-gdi.patch
        fix-missing-meson-dep.patch
        gdk-pixbuf-query-loaders.hook.in)
noextract=("gdk-pixbuf-${pkgver}.tar.xz")
sha256sums=('c4a6b75b7ed8f58ca48da830b9fa00ed96d668d3ab4b1f723dcf902f78bde77f'
            '21bd9b2ba1447267c84f1b445cbcf50c62299254856c1c227cc7ba4babc9f27e'
            '7f0732b26fbb76a1b33f54fac5e9fbd7258025aa1c9d086e97691724ea6ff545'
            '3d3350dbb437a675e1bdfbc45ad7701634516cd95508411dfb33005b28c01044'
            '6277c30e763c7889a3446e2ce8c7b8dbe7212678497b2905582de8159831e3fb')

prepare() {
  cd ${srcdir}/
  tar --exclude tests/test-images/reftests/tga -xf "gdk-pixbuf-${pkgver}.tar.xz"
  cd ${srcdir}/gdk-pixbuf-${pkgver}

  msg2 "0003-fix-dllmain.patch"
  patch -p1 -i "${srcdir}"/0003-fix-dllmain.patch
  patch -p1 -i "${srcdir}"/fix-missing-meson-dep.patch

  # build all loaders + all gdi loaders static and just don't register
  # the gdi loaders we don't need so the external ones get used
  # (everything except wmf/emf atm)
  patch -p1 -i "${srcdir}"/0004-build-all-loaders-plus-gdi.patch
}

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

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  meson \
    --prefix=${MINGW_PREFIX} \
    --wrap-mode=nodownload \
    --auto-features=enabled \
    --buildtype=plain \
    -Dinstalled_tests=false \
    -Drelocatable=true \
    -Dgtk_doc=true \
    -Dnative_windows_loaders=true \
    -Dbuiltin_loaders=windows \
    "../gdk-pixbuf-${pkgver}"

  ninja
}

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

  DESTDIR="${pkgdir}" ninja install

  for hook in gdk-pixbuf-query-loaders; do
    local hook_path="${srcdir}/${MINGW_PACKAGE_PREFIX}-${hook}.hook";
    cp "${srcdir}/${hook}.hook.in" "${hook_path}"
    sed -s "s|@MINGW_HOOK_TARGET_PREFIX@|${MINGW_PREFIX:1}|g" -i "${hook_path}"
    sed -s "s|@MINGW_PREFIX@|${MINGW_PREFIX}|g" -i "${hook_path}"
    install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 "${hook_path}"
  done

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