# Contributor: Runar Tenfjord < runar dot tenfjord at gmail dot com >
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=cython
pkgbase=mingw-w64-${_realname}-git
pkgname=("${MINGW_PACKAGE_PREFIX}-cython-git" "${MINGW_PACKAGE_PREFIX}-cython2-git")
pkgver=0.23.beta1.7.g702f1f4
pkgrel=1
pkgdesc="C-Extensions for Python (mingw-w64)"
arch=('any')
url="http://www.cython.org"
license=('APACHE')
makedepends=(
            "${MINGW_PACKAGE_PREFIX}-python3-setuptools"
            "${MINGW_PACKAGE_PREFIX}-python2-setuptools"
            "${MINGW_PACKAGE_PREFIX}-gcc"
            )
source=("git+https://github.com/cython/cython.git")
sha256sums=('SKIP')

pkgver() {
  cd "${srcdir}"/${_realname}
  git describe --tags | sed 's|-|.|g'
}

prepare() {
  cp -a ${_realname} ${_realname}-py2-${pkgver}-${CARCH}
  mv ${_realname} ${_realname}-${pkgver}-${CARCH}
}

package_cython() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
  cd ${_realname}-${pkgver}-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  DISTUTILS_DEBUG=1 \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
                                --root="${pkgdir}" --optimize=1

  # Our setuptools {gui,cli}{,-32,-64}.exe have special-case handling for /usr/bin/env such that
  # they look in PATH.
  sed -i 's|#!.*|#!/usr/bin/env python3|' ${pkgdir}/${MINGW_PREFIX}/bin/cy{gdb,thon}-script.py
}

package_cython2() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools")
  cd ${_realname}-py2-${pkgver}-${CARCH}
  DISTUTILS_DEBUG=1 \
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
                                --root="${pkgdir}" --optimize=1

  # Our setuptools {gui,cli}{,-32,-64}.exe have special-case handling for /usr/bin/env such that
  # they look in PATH.
  sed -i 's|#!.*|#!/usr/bin/env python2|' ${pkgdir}/${MINGW_PREFIX}/bin/cy{gdb,thon}-script.py

  mv ${pkgdir}${MINGW_PREFIX}/bin/cygdb{,2}.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/cython{,2}.exe
  mv ${pkgdir}${MINGW_PREFIX}/bin/cythonize{,2}.exe
  if [ -f ${pkgdir}${MINGW_PREFIX}/bin/cygdb.exe.manifest ]; then
    mv ${pkgdir}${MINGW_PREFIX}/bin/cygdb{,2}.exe.manifest
    mv ${pkgdir}${MINGW_PREFIX}/bin/cython{,2}.exe.manifest
    mv ${pkgdir}${MINGW_PREFIX}/bin/cythonize{,2}.exe.manifest
  fi

  mv ${pkgdir}${MINGW_PREFIX}/bin/cygdb{,2}-script.py
  mv ${pkgdir}${MINGW_PREFIX}/bin/cython{,2}-script.py
  mv ${pkgdir}${MINGW_PREFIX}/bin/cythonize{,2}-script.py
}

package_mingw-w64-i686-cython-git() {
  conflicts=("mingw-w64-i686-cython")
  provides=("mingw-w64-i686-cython")
  package_cython
}

package_mingw-w64-i686-cython2-git() {
  conflicts=("mingw-w64-i686-cython2")
  provides=("mingw-w64-i686-cython2")
  package_cython2
}

package_mingw-w64-x86_64-cython-git() {
  conflicts=("mingw-w64-x86_64-cython")
  provides=("mingw-w64-x86_64-cython")
  package_cython
}

package_mingw-w64-x86_64-cython2-git() {
  conflicts=("mingw-w64-x86_64-cython2")
  provides=("mingw-w64-x86_64-cython2")
  package_cython2
}
