# Maintainer: Peter Budai <peterbudai@hotmail.com>

# PLEASE NOTE: due to npm bug the build process is stuck if
# logging is enabled. In order to build this recipe you need
# to TURN OFF logging and build like:
# makepkg -sCf

_realname=notebook
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=6.0.2
pkgrel=2
pkgdesc="The language-agnostic HTML notebook application for Project Jupyter"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://github.com/jupyter/notebook"
license=('BSD')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-jupyter_core"
         "${MINGW_PACKAGE_PREFIX}-python-jupyter_client"
         "${MINGW_PACKAGE_PREFIX}-python-jupyter-nbformat"
         "${MINGW_PACKAGE_PREFIX}-python-jupyter-nbconvert"
         "${MINGW_PACKAGE_PREFIX}-python-ipywidgets"
         "${MINGW_PACKAGE_PREFIX}-python-jinja"
         "${MINGW_PACKAGE_PREFIX}-python-traitlets"
         "${MINGW_PACKAGE_PREFIX}-python-tornado"
         "${MINGW_PACKAGE_PREFIX}-python-terminado"
         "${MINGW_PACKAGE_PREFIX}-python-send2trash"
         "${MINGW_PACKAGE_PREFIX}-python-prometheus-client"
#         "${MINGW_PACKAGE_PREFIX}-mathjax"
         )
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-nodejs"
             "${MINGW_PACKAGE_PREFIX}-yarn"
             "${MINGW_PACKAGE_PREFIX}-bower"
            )
source=("${_realname}-${pkgver}.tgz::https://github.com/jupyter/notebook/archive/${pkgver}.tar.gz"
        "001-jupyter-notebook-MINGW.patch")
sha256sums=('c87b63b7bcdc9404478940b23b652135ef61d0feea1c8cf1cf00cb34190a6774'
            '0362ef7944792188d8202bdedd7b51b9a0a06867ea43f480bf8318b163d3b616')

prepare(){
  cd "${srcdir}/${_realname}-${pkgver}"
  #patch -p1 -i ${srcdir}/001-jupyter-notebook-MINGW.patch
}

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  start //wait ${MINGW_PREFIX}/bin/npm install
  ${MINGW_PREFIX}/bin/python setup.py build
}

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

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

  # Remove hard coded library paths in css map files
  local TOKEN_TOREPLACE=$(cygpath -am ${srcdir}/${_realname}-${pkgver}/${_realname}/static)

  local _py3ver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")
  pushd "${pkgdir}${MINGW_PREFIX}/lib/python${_py3ver}/site-packages/notebook/static/style/" > /dev/null
  sed -s "s|${TOKEN_TOREPLACE}|..|g" -i ./ipython.min.css.map
  sed -s "s|${TOKEN_TOREPLACE}|..|g" -i ./style.min.css.map
  popd > /dev/null

  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*-script.py; do
    sed -e '1 { s/^#!.*$// }' -i ${_f}
  done
}
