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

_realname=geographiclib
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-python-${_realname}-docs")
pkgver=2.1
pkgrel=1
pkgdesc='Python implementation of the geodesic routines in GeographicLib (mingw-w64)'
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://geographiclib.sourceforge.io/Python/doc/'
msys2_repository_url='https://github.com/geographiclib/geographiclib-python'
msys2_changelog_url='https://geographiclib.sourceforge.io/Python/doc/package.html#change-log'
msys2_references=(
  'archlinux: python-geographiclib'
  'gentoo: dev-python/geographiclib'
  'purl: pkg:pypi/geographiclib'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-python")
makedepends=("${MINGW_PACKAGE_PREFIX}-python-build"
             "${MINGW_PACKAGE_PREFIX}-python-installer"
             "${MINGW_PACKAGE_PREFIX}-python-setuptools"
             "${MINGW_PACKAGE_PREFIX}-python-sphinx")
checkdepends=("${MINGW_PACKAGE_PREFIX}-python-pytest")
options=('!strip')
source=("https://pypi.org/packages/source/${_realname::1}/${_realname}/${_realname}-${pkgver}.tar.gz"
        "${msys2_repository_url}/archive/v${pkgver}/${_realname}-python-${pkgver}.tar.gz")
sha256sums=('6a6545e6262d0ed3522e13c515713718797e37ed8c672c31ad7b249f372ef108'
            'a6d32de8fa8fc9d44d6d469e2ec43a8bae532fd7a1d7611e3417500c1768b599')

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

  msg "Python build for ${MSYSTEM}"
  python -m build --wheel --skip-dependency-check --no-isolation

  msg "Build documentation"
  cd "${srcdir}/python-build-${MSYSTEM}/doc"
  sphinx-build . _build/html
}

check() {
  cd "python-build-${MSYSTEM}"

  python -m pytest
}

package_python-geographiclib() {
  cd "python-build-${MSYSTEM}"

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

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

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

  mkdir -p "${pkgdir}${MINGW_PREFIX}/share/doc/python-${_realname}"
  cp -a "${srcdir}/python-build-${MSYSTEM}/doc/_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;
