# Maintainer: Mohsen Seyedkazemi Ardebili <mohsen.seyedkazemi@gmail.com>
#
# AUR package for YazSes — local, offline voice dictation.
# Source: https://github.com/MSKazemi/yazses
#
# SOURCE OF TRUTH. This file is the original; the AUR repo at
# ssh://aur@aur.archlinux.org/yazses.git holds a published COPY. Edit this file and
# let the release copy it over, never the other way round: publish-channels.yml does
# `cp packaging/arch/PKGBUILD packaging/arch/.SRCINFO aur/`, a whole-file overwrite,
# so a patch applied in the AUR repo is discarded by the next release without a
# conflict, a warning, or any trace. That is not hypothetical -- it is exactly what
# happened to a contributor's one-line fix on the Homebrew tap, which sat merged
# nowhere while two releases shipped over it.
#
# Built from the PyPI sdist rather than a GitHub tag tarball: it is the artifact the
# release actually publishes, its checksum is stable and quotable, and it contains
# everything installed below (man page, systemd unit, example config, LICENSE).
#
# Two dependencies live in the AUR rather than [extra] — python-faster-whisper and
# python-sounddevice. An AUR package may depend on AUR packages, so this is fine; the
# earlier revision of this file pip-installed them inside package() instead, which
# downloads from PyPI at build time, is unreproducible, and is exactly what the AUR
# guidelines tell you not to do.

pkgname=yazses
pkgver=2.39.1
pkgrel=1
pkgdesc='Offline hold-to-talk voice dictation that types into the focused app - local faster-whisper, no cloud'
arch=('any')
url='https://github.com/MSKazemi/yazses'
license=('Apache-2.0')
install="${pkgname}.install"

depends=(
    'python>=3.11'
    'python-numpy'
    'python-typer'
    'python-platformdirs'
    'python-cryptography'
    'python-evdev'
    'python-xlib'
    # Qt is an optional extra upstream (v2.18.0 moved PySide6 out of the base
    # install; a headless one is ~650 MB lighter). It stays a hard dependency
    # here on purpose: pyside6 is a shared system package on Arch, not a 650 MB
    # private copy, and someone installing a desktop dictation app expects the
    # tray icon to appear rather than to discover a second install step.
    'pyside6'                  # voice-activity overlay + tray icon
    'portaudio'                # libportaudio.so, for sounddevice
    'python-faster-whisper'    # AUR
    'python-sounddevice'       # AUR
)

optdepends=(
    'xdotool: text injection and window targeting on X11'
    'ydotool: text injection on Wayland (preferred)'
    'wtype: text injection on Wayland (alternative)'
    'xclip: clipboard fallback on X11'
    'wl-clipboard: clipboard fallback on Wayland'
    'python-sherpa-onnx: speaker labels for `yazses transcribe --diarize` (AUR)'
    # The Parakeet STT engine needs onnx-asr, which is in neither [extra] nor the AUR.
    # It is an opt-in engine and the factory falls back to faster-whisper without it, so
    # its absence costs nothing here — install it with pip if you want Parakeet.
)

makedepends=(
    'python-build'
    'python-installer'
    'python-wheel'
    'python-hatchling'
)

source=("https://files.pythonhosted.org/packages/source/y/${pkgname}/${pkgname}-${pkgver}.tar.gz")
sha256sums=('5c832024a936e9c16aa4f6acc39264d9a950e00b1e97dfc0358d5253e72c220e')

build() {
    cd "${pkgname}-${pkgver}"
    python -m build --wheel --no-isolation
}

package() {
    cd "${pkgname}-${pkgver}"

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

    # systemd user unit: `systemctl --user enable --now yazses.service`
    install -Dm644 contrib/yazses.service \
        "${pkgdir}/usr/lib/systemd/user/yazses.service"

    install -Dm644 man/yazses.1 "${pkgdir}/usr/share/man/man1/yazses.1"
    install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
    install -Dm644 README.md "${pkgdir}/usr/share/doc/${pkgname}/README.md"
    install -Dm644 examples/config.example.toml \
        "${pkgdir}/usr/share/doc/${pkgname}/config.example.toml"
}
