# Maintainer: Sarah Ottinger

_realname=click
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgver=8.2.2
pkgrel=1
pkgdesc='A simple wrapper around optparse for powerful command line utilities (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://click.palletsprojects.com/"
msys2_repository_url='https://github.com/pallets/click'
msys2_references=(
  'archlinux: python-click'
  'gentoo: dev-python/click'
  'purl: pkg:pypi/click'
)
license=('spdx:BSD-3-Clause')
depends=(
  "${MINGW_PACKAGE_PREFIX}-python"
  "${MINGW_PACKAGE_PREFIX}-python-colorama"
)
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-python-build"
  "${MINGW_PACKAGE_PREFIX}-python-installer"
  "${MINGW_PACKAGE_PREFIX}-python-flit-core"
  "${MINGW_PACKAGE_PREFIX}-python-myst-parser"
  "${MINGW_PACKAGE_PREFIX}-python-pallets-sphinx-themes"
  "${MINGW_PACKAGE_PREFIX}-python-sphinxcontrib-log-cabinet"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx-issues"
  "${MINGW_PACKAGE_PREFIX}-python-sphinx-tabs"
)
source=("https://github.com/pallets/click/archive/${pkgver}/${_realname}-$pkgver.tar.gz"
        460af4cfee15072aee432b3c8c38ff58d9b0fe11.patch)
sha512sums=('3eaa13c40354c84769c919a4326903e35860af4922478d2d2c2c4596faf6399809d71289d0d1d7656ca6bf8407a38102244bd02dea7a296bbe8231fb77bfd5fc'
            '5f69e91ba208c8ea573319f1ac7d5fb2bf7c92b5b5b9ec40c1b24a15742ed656d7d090bfa938006e7162b169c7f6e89c18ddff94fdc0ea229fb07efd58bf5da5')

revert_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Reverting $_patch"
    patch -Rbp1 -i "${srcdir}/$_patch"
  done
}

prepare() {
  cd "${srcdir}"/"${_realname}-${pkgver}"
  revert_patch_with_msg \
    460af4cfee15072aee432b3c8c38ff58d9b0fe11.patch
}

build() {
  cp -r "${_realname}-${pkgver}" "python-build-${MSYSTEM}" && cd "python-build-${MSYSTEM}"

  python -m build --wheel --skip-dependency-check --no-isolation

  # Forward installation for building documentation
  _pkgdir="${srcdir}/python-install-${MSYSTEM}"
  MSYS2_ARG_CONV_EXCL="--prefix=" \
    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" \
    make html
}

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

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

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

package_python-click-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;
