# Maintainer: Luke T. Shumaker <lukeshu@parabola.nu>
# Maintainer (AUR:pyglet): konez2k@aur
# Contributor (AUR:pyglet): bruceutut@aur
# Maintainer (Arch:community/pyglet): Kyle Keen <keenerd@gmail.com>
# Contributor (Arch:community/pyglet): Douglas Soares de Andrade <douglas@archlinux.org>
# Contributor (Arch:community/pyglet): Ryan Coyner <rcoyner@gmail.com>
# Contributor (Arch:community/pyglet): Daniel J Griffiths <ghost1227@archlinux.us>

# Rationale for inclusion in Parabola: Dependency of pcr/printrun.

_pkgname=pyglet
pkgname=('python-pyglet')
pkgver=2.1.11
pkgrel=1
pkgdesc="A cross-platform windowing and multimedia library for Python"
arch=('any')
url="https://pyglet.org/"
license=('BSD-3-Clause')
depends=('python' 'libgl')
optdepends=(
    # pyglet/image/codecs/__init__.py:add_default_codecs()
    #'...: For `pyglet.image.codecs.dds`' # only uses stdlib, I think
    #'gdk-pixbuf2: For `pyglet.image.codecs.gdkpixbuf2`' # "Library gdk-x11-2.0 not found", GDK doesn't have gdk-x11 anymore
    'python-pillow: For `pyglet.image.codecs.pil`, and for font scaling'
    #'...: For `pyglet.image.codecs.png`' # only uses stdlib, I think
    #'...: For `pyglet.image.codecs.bmp`' # Windows-only, even though it doesn't check compat_platform

    # pyglet/media/codecs/__init__.py:add_default_codecs()
    #'python-wave: For `pyglet.media.codecs.wave`' # wave is part of stdlib
    'python-gobject: (together with gstreamer) For `pyglet.media.codecs.gstreamer`'
    'gstreamer: (together with python-gobject) For `pyglet.media.codecs.gstreamer`'
    'ffmpeg: For `pyglet.media.codecs.ffmpeg`'
    #'python-pyogg': For `pyglet.media.codecs.pyogg`' # python-pyogg isn't packaged

    # pyglet/media/drivers/__init__.py
    'openal: For `pyglet.media.drivers.openal` audio playback driver'
    'libpulse: For `pyglet.media.drivers.pulse` audio playback driver'

    'libxi: For X11 fancy input devices' # `pyglet.input.linux` -> `pyglet.input.linux.x11_xinput` -> `pyglet.libs.x11.xinput`
    'libxinerama: For X11 multiple-screen support' # `pyglet.display.xlib` -> `pyglet.libs.x11.xinerama`
    'libxkbcommon: For Wayland support' # `pyglet.libs.wayland.xkbcommon`
)
makedepends=('python-build' 'python-flit-core' 'python-installer' "${optdepends[@]%%:*}")
checkdepends=('xorg-server-xvfb' 'python-pytest' 'ttf-liberation' 'pulseaudio')
source=("$_pkgname-$pkgver.zip::https://github.com/pyglet/pyglet/archive/refs/tags/v$pkgver.zip")
sha256sums=('531ab3752d00780bc0b2e0185f6c5bc1b7a7459aca1354636676bb322e24c8f5')

build() {
    cd "$srcdir/$_pkgname-$pkgver"
    python -m build --wheel --no-isolation
}

check() {
    cd "$srcdir/$_pkgname-$pkgver"

    # Make sure we got all the optdepends:
    find pyglet -name '*.py' |
	sed -e 's,/__init__\.py$,,' -e 's/\.py$//' -e 's,/,.,g' |
	grep -v \
	     -e macos -e cocoa -e quartz -e darwin -e coreaudio -e agl \
	     -e win32 -e '\.dwrite\b' -e directsound -e xaudio2 -e wincodec -e wmf -e bmp -e '\.wic\b' -e gdiplus -e wgl \
	     -e pyogg -e gdkpixbuf2 |
	sort |
	sed 's/.*/print("import &")\nimport &/' | xvfb-run python

    # Run the test suite.
    if ! pactl info; then
	tmpdir="$(mktemp -d)"
	trap 'rm -rf "$tmpdir"' RETURN

	export DBUS_SESSION_BUS_ADDRESS=unix:path="$tmpdir/dbus.sock"
	dbus-daemon --session --address="$DBUS_SESSION_BUS_ADDRESS" --fork

	pulseaudio --daemonize --use-pid-file=false --log-target=stderr

	pactl info
    fi
    xvfb-run pytest tests --ignore=tests/interactive
}

package() {
    cd "$srcdir/$_pkgname-$pkgver"
    python -m installer --destdir="$pkgdir" dist/*.whl
    install -D -m644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
