# Maintainer: Mohsen Seyedkazemi Moghadam <mohsen.seyedkazemi@gmail.com>
#
# AUR package for YazSes — local, offline voice dictation.
# Source: https://github.com/novafabric/yazses

pkgname=yazses
pkgver=0.4.0
pkgrel=1
pkgdesc='Local, offline voice dictation — hold a key, speak, release'
arch=('any')
url='https://github.com/novafabric/yazses'
license=('Apache-2.0')

depends=(
    'python>=3.11'
    'python-numpy'
    'python-typer'
    'python-platformdirs'
    'portaudio'                # libportaudio.so for sounddevice
    'systemd'                  # user services
)

# Faster-whisper, evdev, sounddevice live on PyPI but not in [extra]/[community]
# at this writing; install them via the pyproject. We ship a wheel/sdist for
# everything else.
optdepends=(
    'xdotool: text injection 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'
)

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

source=("${pkgname}-${pkgver}.tar.gz::${url}/archive/refs/tags/v${pkgver}.tar.gz")
sha256sums=('SKIP')   # set to the real sha256 of the GitHub source tarball.

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

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

    # Install the wheel into the system python sitelib.
    python -m installer --destdir="${pkgdir}" dist/*.whl

    # Pip-install runtime PyPI-only deps into the same prefix. AUR convention
    # discourages pip-on-package-install, but several deps (faster-whisper,
    # ctranslate2, evdev, sounddevice) aren't packaged in the official Arch
    # repos, so we pull them with --no-deps to avoid pulling in already-installed
    # ones a second time.
    PYTHONPATH="${pkgdir}/usr/lib/python3.13/site-packages" \
        pip install --root="${pkgdir}" --prefix=/usr --no-deps --no-warn-script-location \
            'faster-whisper>=1.2.1' \
            'evdev>=1.9.3' \
            'sounddevice>=0.5.5'

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

    # License + docs.
    install -Dm644 LICENSE "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
    install -Dm644 README.md "${pkgdir}/usr/share/doc/${pkgname}/README.md"

    # Example config.
    if [[ -f examples/config.example.toml ]]; then
        install -Dm644 examples/config.example.toml \
            "${pkgdir}/usr/share/doc/${pkgname}/config.example.toml"
    fi
}

post_install() {
    cat <<'EOF'

YazSes installed. Two manual steps remain:

  1. Add yourself to the input group (for /dev/input keyboard access):
        sudo usermod -aG input "$USER"
     Log out and back in (or reboot) for the change to take effect.

  2. Enable the user service:
        systemctl --user enable --now yazses.service

Hold Space anywhere, speak, release. Default config:
    ~/.config/yazses/config.toml

Run `yazses doctor` to verify everything.

EOF
}

post_upgrade() {
    post_install
}

post_remove() {
    cat <<'EOF'

YazSes removed. To clean user-level state:
    rm -rf ~/.config/yazses ~/.cache/yazses
    systemctl --user disable --now yazses.service 2>/dev/null || true

EOF
}
