# Maintainer: Konstantin Podsvirov <konstantin@podsvirov.su>

_realname=flask-babel
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgver=4.0.0
pkgrel=2
pkgdesc='An i18n and l10n support for Flask based on Babel and pytz (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://python-babel.github.io/flask-babel/'
msys2_repository_url='https://github.com/python-babel/flask-babel'
msys2_references=(
  'purl: pkg:pypi/flask-babel'
)
license=('spdx:BSD')
depends=("${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-python-babel"
         "${MINGW_PACKAGE_PREFIX}-python-flask"
         "${MINGW_PACKAGE_PREFIX}-python-jinja"
         "${MINGW_PACKAGE_PREFIX}-python-pytz")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-poetry-core"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
# We do not use the pypi tarball, as there is no docs directory in it
#source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname/-/_}-${pkgver}.tar.gz")
source=("${msys2_repository_url}/archive/v${pkgver}/${_realname}-${pkgver}.tar.gz"
        '0001-flask-babel-4.0.0-docs-no-theme.patch')
sha256sums=('6952ba8a6813a33a5c2ff65ddad61e8215307b5eb060d2d87e413822af9114a7'
            '128aa052a992c19d7cb9521cd63ffdf761faab187697ae5dd00a45d12a8894f9')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -p1 -i "${srcdir}/0001-flask-babel-4.0.0-docs-no-theme.patch"

  cd "${srcdir}"

  rm -rf "python-build-${MSYSTEM}" | true
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}"

  rm -rf "python-install-${MSYSTEM}" | true
}

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

  ${MINGW_PREFIX}/bin/python -m build --wheel --skip-dependency-check --no-isolation

  # Forward installation for building documentation
  _pkgdir="${srcdir}/python-install-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    "${MINGW_PREFIX}"/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${_pkgdir}" dist/*.whl

  msg "Build documentation"
  cd "${srcdir}/python-build-${MSYSTEM}/docs"
  _pyver=$("${MINGW_PREFIX}"/bin/python -c "import sys; print(f'{sys.version_info.major}.{sys.version_info.minor}')")
  PYTHONPATH="${_pkgdir}${MINGW_PREFIX}/lib/python${_pyver}/site-packages" \
    "${MINGW_PREFIX}"/bin/sphinx-build -M html . _build
}

package_python-flask-babel() {
  cd "${srcdir}/python-build-${MSYSTEM}"

  MSYS2_ARG_CONV_EXCL="--prefix=" \
    "${MINGW_PREFIX}"/bin/python -m installer --prefix=${MINGW_PREFIX} \
    --destdir="${pkgdir}" dist/*.whl

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

package_python-flask-babel-docs() {
  pkgdesc+=" (documentation)"
  depends=()

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}"
  cp -a "${srcdir}/python-build-${MSYSTEM}/docs/_build/html" "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}/html"
}

# template start; name=mingw-w64-splitpkg-wrappers; version=1.0;
# vim: set ft=bash :

# generate wrappers
for _name in "${pkgname[@]}"; do
  _short="package_${_name#${MINGW_PACKAGE_PREFIX}-}"
  _func="$(declare -f "${_short}")"
  eval "${_func/#${_short}/package_${_name}}"
done
# template end;

