# Maintainer: Kyle Kauffman <kyle.j.kauffman@gmail.com>

_realname=ipython
pkgbase=mingw-w64-python3-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=7.1.1
pkgrel=1
pkgdesc="An enhanced Interactive Python shell (mingw-w64)"
arch=('any')
url="https://ipython.org/"
license=('BSD')
# Mote: prompt_toolkit 2.0 is required for this series.  There
# is some breakage between prompt_toolkit 1.0 and 2.0.
depends=('winpty'
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-python3-jedi"
         "${MINGW_PACKAGE_PREFIX}-python3-decorator"
         "${MINGW_PACKAGE_PREFIX}-python3-pickleshare"
         "${MINGW_PACKAGE_PREFIX}-python3-simplegeneric"
         "${MINGW_PACKAGE_PREFIX}-python3-traitlets"
         "${MINGW_PACKAGE_PREFIX}-python3-prompt_toolkit>=2.0"
         "${MINGW_PACKAGE_PREFIX}-python3-pygments"
         "${MINGW_PACKAGE_PREFIX}-python3-simplegeneric"
         "${MINGW_PACKAGE_PREFIX}-python3-backcall"
         "${MINGW_PACKAGE_PREFIX}-python3-pexpect"
         "${MINGW_PACKAGE_PREFIX}-python3-colorama"
         "${MINGW_PACKAGE_PREFIX}-python3-win_unicode_console")
# new optional thing: ipyparallel
optdepends=("${MINGW_PACKAGE_PREFIX}-python3-nose: for IPython's test suite"
            "${MINGW_PACKAGE_PREFIX}-python3-qtconsole: for ipython qtconsole"
            "${MINGW_PACKAGE_PREFIX}-python3-ipython-ipyparallel: for IPython.parallel")
makedepends=("${MINGW_PACKAGE_PREFIX}-python3-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python3-requests"
              "${MINGW_PACKAGE_PREFIX}-python3-testpath"
              "${MINGW_PACKAGE_PREFIX}-python3-pygments" 
              "${MINGW_PACKAGE_PREFIX}-python3-jupyter-nbformat"
              "${MINGW_PACKAGE_PREFIX}-python3-ipykernel"
              "${MINGW_PACKAGE_PREFIX}-python3-numpy")
install=${_realname}3-${CARCH}.install
source=("${_realname}-${pkgver}.tar.gz::https://github.com/ipython/ipython/archive/${pkgver}.tar.gz")
sha256sums=('9222f658b1870e525ad758d34ab3c38ff8f4d6ba93697f5615e49f47d42a353a')

prepare() {
  cd "${srcdir}"
  rm -rf python3-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python3-build-${CARCH}"
}

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

check() {
    msg "Python 3 test for ${CARCH}"
    cd "${srcdir}/python3-build-${CARCH}"
    ${MINGW_PREFIX}/bin/python3 -m test || warning "Tests fail"
}

package() {
  cd "${srcdir}/python3-build-${CARCH}"
  # see https://github.com/ipython/ipython/issues/2057
  #export LC_ALL=en_US.UTF-8
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install \
     --prefix=${MINGW_PREFIX} --root=${pkgdir} --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

  local PREFIX_WIN=$(cygpath -wm ${MINGW_PREFIX})
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.cmd; do
     sed -e "s|${PREFIX_WIN}/bin/||g" -i ${_f}
  done

# For the command to work on the bash prompt, you must use
# winpty so include that in the Shebang before the python command.
# I don't like this but what I like really doesn't matter.
  for _f in iptest iptest3 ipython ipython3; do
     sed -e "s|/usr/bin/env |${MINGW_PREFIX}/|g" \
         -e "s|${PREFIX_WIN}|${MINGW_PREFIX}|g" \
         -e "s|#!|#!winpty |" -i "${pkgdir}${MINGW_PREFIX}/bin/${_f}"
  done

  install -Dm644 docs/source/about/license_and_copyright.rst "${pkgdir}${MINGW_PREFIX}/share/licenses/ipython/LICENSE"

}
