# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Kyle Keen <keenerd@gmail.com>
# Contributor: minder

pkgname=python-pillow
pkgver=11.0.0
pkgrel=2
pkgdesc='Python Imaging Library (PIL) fork'
arch=('x86_64' 'aarch64')
url='https://python-pillow.org'
license=('MIT-CMU')
depends=(
  'glibc'
  'python'
  'python-packaging'
  'freetype2'
  'lcms2'
  'libraqm'
  'libtiff'
  'openjpeg2'
  'libjpeg-turbo'
  'libimagequant'
  'libxcb'
  'zlib'
)
makedepends=(
  'git'
  'python-build'
  'python-installer'
  'python-wheel'
  'python-setuptools'
  'libwebp'
  'tk'
)
checkdepends=(
  'python-pytest'
  'python-pytest-timeout'
)
optdepends=(
  'libwebp: for webp images'
  'tk: for the ImageTK module'
  'python-olefile: OLE2 file support'
  'python-pyqt6: for the ImageQt module'
)
source=(
  "$pkgname::git+https://github.com/python-pillow/Pillow#tag=$pkgver"
  'ftbfs-trove-classifiers.patch'
)
sha512sums=('93867f9ec6e58dc52d1cb67f72e127f17f42954735daf4920c853cb0da503c16bc7ee17a37f4dabc564949b589439f1fe7a3c712b89252b493b53bcc8ff4c13d'
            '5ac423ac371b8303d6273382af3586943bd4c68cb9b03445754fde551c8bab1ce58670443c30c51698284e532996795d81414cb1d39b3a438b285228e2fbb047')
b2sums=('f3c65ffd5df44276e524fedf01728de5e5db428b239d1481ece42f8fffd819ba30c3d77d151ce17e62e254793d6666befede0db46699575a48fc23a075abe5d6'
        'c429863610bf2a0ee389addc99e60c8ec76ec4b703d8e822afdb2ea2277c6ef3ccc055ed84af720d651373942944feaa85048692b5daf044180abf5dcaf3215d')

prepare() {
  cd "$pkgname"

  # FTBFS: why are we validating pyproject.toml?
  patch -p1 -i "$srcdir/ftbfs-trove-classifiers.patch"
}

build() {
  cd "$pkgname"

  python -m build --wheel --no-isolation
}

check() {
  cd "$pkgname"

  python -m venv --system-site-packages test-env
  test-env/bin/python -m installer dist/*.whl
  test-env/bin/python selftest.py
  test-env/bin/python -m pytest -v
}

package() {
  cd "$pkgname"

  python -m installer --destdir="$pkgdir" dist/*.whl

  local python_version=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))')
  install -vDm644 -t "$pkgdir/usr/include/python$python_version" src/libImaging/*.h

  # license
  install -vDm644 -t "$pkgdir/usr/share/licenses/$pkgname" LICENSE
}
