# Maintainer: Christoph Reiter <reiter.christoph@gmail.com>

_realname=markdown
_pyname=Markdown
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=3.3.4
pkgrel=2
pkgdesc="Python implementation of John Gruber's Markdown (mingw-w64)"
url='https://pypi.python.org/pypi/Markdown'
license=('BSD')
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-pbr"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-yaml")
source=("https://pypi.io/packages/source/${_pyname:0:1}/${_pyname}/${_pyname}-${pkgver}.tar.gz")
sha256sums=('31b5b491868dcc87d6c24b7e3d19a0d730d59d3e46f4eea6430a321bed387a49')

prepare() {
  cd "${srcdir}"

  # bug in 2.4, some DOS line endings slipped in
  find "${_realname}-${pkgver}" -name '*py' -exec sed -i 's|\r||g' {} +

  # ImportError: No module named pkg_resources
  sed -i 's/^from pkg_resources.*$/import packaging.version/' "${_realname}-${pkgver}/markdown/__init__.py"

  cd "${srcdir}"
  rm -rf "python-build-${CARCH}" | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
}

build() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python setup.py build
}

check() {
  cd "${srcdir}/python-build-${CARCH}"
  ${MINGW_PREFIX}/bin/python -m unittest discover tests
}

package() {
  cd "${srcdir}/python-build-${CARCH}"

  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python setup.py install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --skip-build

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

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