# Maintainer: Patrick Stewart <patstew@gmail.com>

_realname=ipykernel
pkgbase=mingw-w64-python2-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}")
pkgver=4.9.0
pkgrel=2
pkgdesc="The ipython kernel for Jupyter (mingw-w64)"
arch=('any')
url="https://ipython.org/"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-python2"
         "${MINGW_PACKAGE_PREFIX}-python2-backports.shutil_get_terminal_size"
         "${MINGW_PACKAGE_PREFIX}-python2-pathlib2"
         "${MINGW_PACKAGE_PREFIX}-python2-pyzmq"
         "${MINGW_PACKAGE_PREFIX}-python2-ipython")
makedepends=("${MINGW_PACKAGE_PREFIX}-python2-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python2-ipython"
             "${MINGW_PACKAGE_PREFIX}-python2-jupyter_client"
             "${MINGW_PACKAGE_PREFIX}-python2-jupyter_core"
             "${MINGW_PACKAGE_PREFIX}-python2-enum34")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python2-pytest"
             "${MINGW_PACKAGE_PREFIX}-python2-pytest-cov"
             "${MINGW_PACKAGE_PREFIX}-python2-nose"
             "${MINGW_PACKAGE_PREFIX}-python2-mock")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/ipython/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('c4fa19dcdacde4499c85526d8d29fe840efcf9b144c5c7708fc44e329b868c71')

prepare() {
  cd "${srcdir}"
  rm -rf python2-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python2-build-${CARCH}"
  # https://github.com/ipython/ipykernel/issues/332
  sed -i "s/executable='python'/executable='python2'/" python2-build-${CARCH}/setup.py
}

build() {
  msg "Python 2 build for ${CARCH}"  
  cd "${srcdir}/python2-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python2 setup.py build
}

check() {
  msg "Python 2 test for ${CARCH}"
  cd "${srcdir}/python2-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python2 -m pytest || warning "tests failed"
}

package() {
  cd "${srcdir}/python2-build-${CARCH}"
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --root="${pkgdir}" --prefix=${MINGW_PREFIX}  --optimize=1 --skip-build

  find "${pkgdir}/" -name "*.pyc" -delete
  find "${pkgdir}/" -type d -empty -delete

  # setuptools doesnt see mingw-w64-python's readline
  find "${pkgdir}/" -name 'requires.txt' -delete

  install -Dm644 COPYING.md "$pkgdir${MINGW_PREFIX}/share/licenses/python2-${_realname}/LICENSE"
}
