# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=rst2pdf
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}")
provides=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}"
          "${MINGW_PACKAGE_PREFIX}-rst2pdf")
conflicts=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
replaces=("${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.98
pkgrel=2
pkgdesc="Create PDFs from simple text markup, no LaTeX required (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64')
url="https://rst2pdf.org"
license=('MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-docutils"
         "${MINGW_PACKAGE_PREFIX}-python-jinja"
         "${MINGW_PACKAGE_PREFIX}-python-pdfrw"
         "${MINGW_PACKAGE_PREFIX}-python-pygments"
         "${MINGW_PACKAGE_PREFIX}-python-reportlab>=2.4"
         "${MINGW_PACKAGE_PREFIX}-python-six"
         "${MINGW_PACKAGE_PREFIX}-python-smartypants")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-setuptools")
options=('staticlibs' 'strip' '!debug')
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/${_realname}/${_realname}/archive/${pkgver}.tar.gz")
sha256sums=('22fb02032b3c2eed834b47f974755457a65958554543fd577680458f2e24816b')

prepare() {
  rm -rf python-build-${CARCH} | true
  cp -r "${_realname}-${pkgver}" "python-build-${CARCH}"
  sed -i "s/use_scm_version=True/version='${pkgver}'/g" "python-build-${CARCH}/setup.py"
}

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

_install_doc_files() {
  for _fname in "$@"
  do
    if [ -f $_fname ]; then
      install -Dm644 $_fname "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/$_fname"

      rm -rf $_fname
    fi
  done
}

package() {
  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  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}" --optimize=1 --skip-build

  _install_doc_files CHANGES.rst \
    CODE_OF_CONDUCT.rst \
    Contributors.txt \
    MAINTAINERS.rst \
    ISSUE_TEMPLATE.md \
    README.rst

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

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