# Maintainer: Blake Bratcher <blake at blakebratcher dot com>
#
# Release package for monocoat 1.2.0, built from the PyPI sdist (the GitHub
# repository is private, so its release assets are not downloadable).
# For the next release: set pkgver, reset pkgrel=1, run `updpkgsums`, then
# `makepkg --printsrcinfo > .SRCINFO`.

pkgname=monocoat
pkgver=1.2.0
pkgrel=1
pkgdesc='One TOML for every Linux theming surface: GTK 2/3/4, Qt/Kvantum, window manager, notifications, cursor, fonts, palette, wallpaper'
arch=('any')
url='https://github.com/blakebratcher/monocoat'
license=('MIT')
# The CLI and every layer are stdlib-only; everything else is optional and
# only needed for the surface (or desktop) that uses it.
depends=('python')
makedepends=('python-build' 'python-installer' 'python-hatchling' 'python-wheel')
checkdepends=('python-pytest')
optdepends=(
  'xfconf: XFCE settings, xfwm4 and xfce4-notifyd backend (xfconf-query)'
  'glib2: gsettings backend for GNOME/GTK settings'
  'xsettingsd: settings broadcast on bare window managers'
  'kvantum: Qt Kvantum theme engine for the qt layer'
  'qt5ct: Qt5 platform theme the qt and fonts layers configure'
  'qt6ct: Qt6 platform theme the qt and fonts layers configure'
  'python-pyqt5: write qt5ct fonts (probed with the system python3)'
  'python-pyqt6: write qt6ct fonts (probed with the system python3)'
  'python-gobject: monocoat dock and the Monocoat settings window (GTK3)'
  'python-cairo: monocoat dock and the Monocoat settings window (cairo drawing)'
  'xorg-xrdb: cursor theme for X resources'
  'xorg-xrandr: display DPI detection on X11'
  'xfce4-notifyd: notification styling on XFCE'
  'plank: dock layer'
  'rofi: launcher layer'
  'polybar: panel layer'
  'python-pywal: panel template rendering (wal; AUR)'
  'picom: effects layer compositor'
  'kitty: live palette reload for the kitty terminal (kitty @ set-colors)'
  'libnotify: notify-send for `monocoat demo`'
  'maim: screenshots for `monocoat demo`'
  'scrot: screenshots for `monocoat demo` (used when maim is absent)'
)
source=("https://files.pythonhosted.org/packages/source/${pkgname::1}/$pkgname/$pkgname-$pkgver.tar.gz")
sha256sums=('81c739d8bbe2016efc37e232ef294502f14e9de5020a6547f15b6bdaf7563348')

prepare() {
  cd "$pkgname-$pkgver"
  # depends=('python') is only true for a source whose core has no hard
  # dependencies. v1.1.1 still required rich and textual (and its test suite
  # predates the PATH guard), so refuse it rather than package a wrong recipe.
  if ! grep -qx 'dependencies = \[\]' pyproject.toml; then
    error "pyproject.toml declares hard Python dependencies; update depends=() first"
    return 1
  fi
}

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

check() {
  cd "$pkgname-$pkgver"
  # Keep the suite off the packager's live desktop whatever the source
  # version: point HOME/XDG at a scratch dir AND shadow every desktop-mutating
  # tool on PATH with a stub that exits 127 ("not installed"). The HOME
  # sandbox alone does not stop subprocesses -- v1.1.1's cursor tests ran a
  # real `xrdb -merge` and `systemctl --user set-environment`.
  local sandbox="$srcdir/check-home" stubs="$srcdir/check-stubs" tool
  local -a desktop_tools=(
    xfconf-query gsettings dconf systemctl pkill pgrep wal xrdb xrandr
    xsettingsd xfsettingsd plank polybar picom rofi notify-send dbus-send
    busctl gdbus qdbus qdbus6 kwriteconfig5 kwriteconfig6 dunstctl makoctl
    swaymsg hyprctl kitty xfdesktop feh swaybg hyprpaper matugen maim scrot
  )
  mkdir -p "$sandbox" "$stubs"
  for tool in "${desktop_tools[@]}"; do
    printf '#!/bin/sh\nexit 127\n' > "$stubs/$tool"
    chmod 755 "$stubs/$tool"
  done
  PATH="$stubs:$PATH" \
  HOME="$sandbox" \
  XDG_CONFIG_HOME="$sandbox/.config" \
  XDG_CACHE_HOME="$sandbox/.cache" \
  XDG_DATA_HOME="$sandbox/.local/share" \
    python -m pytest -q
}

package() {
  cd "$pkgname-$pkgver"
  python -m installer --destdir="$pkgdir" dist/*.whl
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  install -Dm644 README.md "$pkgdir/usr/share/doc/$pkgname/README.md"
  install -Dm644 docs/org.monocoat.Settings.desktop "$pkgdir/usr/share/applications/org.monocoat.Settings.desktop"
}
