# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=gobject-introspection
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-runtime")
pkgver=1.58.2
pkgrel=1
arch=('any')
url="https://live.gnome.org/GObjectIntrospection"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-python3-mako"
             "${MINGW_PACKAGE_PREFIX}-python3-markdown"
             "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
             "autoconf-archive")
source=(https://download.gnome.org/sources/gobject-introspection/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        0021-tests-no-undefined.patch
        0022-change-pkg-config-invocations.mingw.patch
        0024-Support-passing-arguments-to-g-ir-scanner-through-a-.all.patch
        0026-giscanner-assertions-and-waits.patch
        0027-wait-for-xml-parse-too.patch
        pyscript2exe.py)

sha256sums=('2a5e0f69459af7ca4c41b8f4e19d9c80ddf877834bb0e7fdd420e2495ae2eda8'
            '79ec6c7f845d65eacafdcb8380441b85ab17afd5ef6cf7d374f2370431135fd5'
            'bb5f48047613216a6c3c652e0ef65c099475ffdddbea4a835b5a51279d7755ac'
            '46106032ea0cd6798082bd7baf1c11bc92eecbd3dd36b8fea17754ef646357fe'
            '19ca830262339c4ac9f21bfb8d669ee8e126a949a4237d55656e00c5ae81c451'
            '3f38bdd0dd43d9cf626cbca7c2abd22a7ed0213e6756252c6d467d470d9c5948'
            'f68b24932b3365c4098c04eeaeaf87275ceec29694b3f0597c431bbcf4f913a3')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0021-tests-no-undefined.patch
  patch -p1 -i "${srcdir}"/0022-change-pkg-config-invocations.mingw.patch
  # Source for this patch is:
  # https://bazaar.launchpad.net/~lrn1986/stupidbuild/trunk/view/head:/src/mingw/gobject-introspection-1.0-1.44.0-2/patches/0024-Support-passing-arguments-to-g-ir-scanner-through-a-.all.patch
  patch -p1 -i "${srcdir}"/0024-Support-passing-arguments-to-g-ir-scanner-through-a-.all.patch
  patch -p1 -i "${srcdir}"/0026-giscanner-assertions-and-waits.patch
  patch -p1 -i "${srcdir}"/0027-wait-for-xml-parse-too.patch

  autoreconf -fi
}

build() {
  export PYTHON=${MINGW_PREFIX}/bin/python3
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-silent-rules \
    --enable-doctool

  make
}

package_gobject-introspection() {
  pkgdesc="Introspection system for GObject-based libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-pkg-config"
           "${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-mako")
  options=('!emptydirs')

  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
  make -j1 DESTDIR="${pkgdir}" uninstall-libLTLIBRARIES uninstall-typelibsDATA

  for name in g-ir-scanner g-ir-doc-tool g-ir-annotation-tool; do
    ${MINGW_PREFIX}/bin/python3 \
      "${srcdir}/pyscript2exe.py" "${pkgdir}${MINGW_PREFIX}/bin/${name}"
  done
}

package_gobject-introspection-runtime() {
  pkgdesc="Introspection system for GObject-based libraries - runtime files (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2")

  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install-libLTLIBRARIES install-typelibsDATA
}

package_mingw-w64-i686-gobject-introspection()
{
  package_gobject-introspection
}

package_mingw-w64-i686-gobject-introspection-runtime()
{
  package_gobject-introspection-runtime
}

package_mingw-w64-x86_64-gobject-introspection()
{
  package_gobject-introspection
}

package_mingw-w64-x86_64-gobject-introspection-runtime()
{
  package_gobject-introspection-runtime
}
