# Maintainer: Philippe Renon <philippe_renon@yahoo.fr>
# Contributor: Lubosz Sarnecki <lubosz@gmail.com>

_realname=gst-python
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-gst-python-git" "${MINGW_PACKAGE_PREFIX}-gst-python2-git")
provides=("${MINGW_PACKAGE_PREFIX}-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=r1383.31c3304
pkgrel=1
pkgdesc="GStreamer GObject Introspection overrides for Python 3 (mingw-w64)"
arch=('any')
license=('LGPL')
makedepends=("${MINGW_PACKAGE_PREFIX}-gstreamer"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3"
             "${MINGW_PACKAGE_PREFIX}-pygobject-devel"
             "${MINGW_PACKAGE_PREFIX}-python2-gobject"
             "${MINGW_PACKAGE_PREFIX}-python3-gobject")
makedepends=('git')
url='https://gstreamer.freedesktop.org/modules/gst-python.html'
options=(!libtool strip staticlibs)
source=("${_realname}::git+https://anongit.freedesktop.org/git/gstreamer/gst-python.git"
        '0001-msys2-python3-config-returns-1-so-use-which.patch'
        '0002-msys2-fix-linking-errors.patch')
sha256sums=('SKIP'
            '836cf2e9e713705e5fd0f09d4e38d3de839a3c663eb6fa85f0a3f9a5f0b224ee'
            '9b4400e0e468888cded57c7269e2deb8f8c6dae9491282469a8c631fc33f7183')

pkgver() {
  cd "$srcdir/$_realname"
  printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short HEAD)"
}

prepare() {
  cd ${_realname}
  patch -p1 -i ${srcdir}/0001-msys2-python3-config-returns-1-so-use-which.patch
  patch -p1 -i ${srcdir}/0002-msys2-fix-linking-errors.patch

  NOCONFIGURE=1 ./autogen.sh
}

build() {
  rm -rf python{2,3}-build devel || true
  mkdir python{2,3}-build devel

  for builddir in python{2,3}-build; do
    pushd ${builddir} > /dev/null
      PYTHON=${MINGW_PREFIX}/bin/${builddir%-build} \
      ../${_realname}/configure \
        --prefix=${MINGW_PREFIX} \
        --build=${MINGW_CHOST} \
        --host=${MINGW_CHOST} \
        --target=${MINGW_CHOST} \
        --disable-silent-rules
      make
    popd > /dev/null
  done
}

package_gst-python() {
  pkgdesc="GStreamer GObject Introspection overrides for Python 3 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gstreamer"
           "${MINGW_PACKAGE_PREFIX}-python3-gobject")

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

  mv "${pkgdir}${MINGW_PREFIX}"/lib/bin/*.dll "${pkgdir}${MINGW_PREFIX}"/lib/gstreamer-1.0/
  rm -rf "${pkgdir}${MINGW_PREFIX}/lib/bin"

  # The python plugin loader makes problems because it links against libpython
  # which might be in conflict one already used loading the plugin.
  # The overrides don't need it so just remove it for now.
  # https://github.com/Alexpux/MINGW-packages/issues/2034
  rm -r "${pkgdir}${MINGW_PREFIX}/lib/gstreamer-1.0"
}

package_gst-python2() {
  pkgdesc="GStreamer GObject Introspection overrides for Python 2 (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gstreamer"
           "${MINGW_PACKAGE_PREFIX}-python2-gobject")

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

  # See other package()
  rm -r "${pkgdir}${MINGW_PREFIX}/lib/gstreamer-1.0"
}

package_mingw-w64-i686-gst-python2-git() {
  package_gst-python2
}

package_mingw-w64-i686-gst-python-git() {
  package_gst-python
}

package_mingw-w64-x86_64-gst-python2-git() {
  package_gst-python2
}

package_mingw-w64-x86_64-gst-python-git() {
  package_gst-python
}
