# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=pygobject
pkgbase=mingw-w64-${_realname}2
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-gobject2" "${MINGW_PACKAGE_PREFIX}-pygobject2-devel")
pkgver=2.28.7
pkgrel=3
pkgdesc="Allows you to load glade interface files in a program at runtime (mingw-w64)"
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="http://www.pygtk.org"
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-gobject-introspection"
             "${MINGW_PACKAGE_PREFIX}-gnome-common")
options=('staticlibs' 'strip')
source=(https://download.gnome.org/sources/pygobject/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        win32-fix.patch
        python-m4.patch)
sha256sums=('bb9d25a3442ca7511385a7c01b057492095c263784ef31231ffe589d83a96a5a'
            '9917129ed8e6271733ff5ebd4149141216262f1df71e9e77bb3c8fc19b4676ab'
            'f409c43d8f4a2c8a486448bb75674f3b2bd1fdf58bb03a72a8427bcf5bda8e36')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i ${srcdir}/win32-fix.patch
  patch -p1 -i ${srcdir}/python-m4.patch
  WANT_AUTOMAKE=latest autoreconf -fi
}

build() {
  rm -rf python2-build-${MINGW_CHOST}
  mkdir python2-build-${MINGW_CHOST} devel
  CFLAGS+=" -Wno-error=format"

  cd python2-build-${MINGW_CHOST}
  PYTHON=${MINGW_PREFIX}/bin/python2 \
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --disable-silent-rules \
    --disable-introspection
  make VERBOSE=1
}

check() {
  cd python2-build-${MINGW_CHOST}
  PYTHON=${MINGW_PREFIX}/bin/python2 \
  make check
}

package_python2-gobject2() {
  pkgdesc="Python 2 bindings for GObject2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2"
           "${MINGW_PACKAGE_PREFIX}-libffi"
           "${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime"
           "${MINGW_PACKAGE_PREFIX}-pygobject2-devel=${pkgver}")

  cd python2-build-${MINGW_CHOST}
  make DESTDIR="${pkgdir}" install

  find "${pkgdir}${MINGW_PREFIX}"/share/pygobject -name '*.py' -exec \
  sed -i '1s|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|' {} +

  rm -r "${pkgdir}${MINGW_PREFIX}"/share/gtk-doc

  # Split devel stuff
  cd ${srcdir}
  mkdir -p devel${MINGW_PREFIX}/{bin,lib,share/pygobject}
  mv "${pkgdir}${MINGW_PREFIX}"/bin/pygobject-codegen-2.0 devel${MINGW_PREFIX}/bin/
  mv {"${pkgdir}",devel}${MINGW_PREFIX}/include
  mv {"${pkgdir}",devel}${MINGW_PREFIX}/lib/pkgconfig
  mv {"${pkgdir}",devel}${MINGW_PREFIX}/share/pygobject/xsl
}

package_pygobject2-devel() {
  pkgdesc="Development files for the pygobject bindings"

  mv ${srcdir}/devel${MINGW_PREFIX} "${pkgdir}/"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;
