# Maintainer: Omarchy <https://omarchy.org>

pkgname=superwhisper-bin
pkgver=1.0.0
_version=1.0.0
pkgrel=1
pkgdesc='Local and cloud voice dictation with Superwhisper'
arch=('x86_64')
url='https://superwhisper.com'
license=('LicenseRef-proprietary' 'MIT')
provides=('superwhisper')
conflicts=('superwhisper')
depends=('glibc' 'libgcc' 'libstdc++' 'libgomp' 'xdg-utils' 'python' 'python-gobject' 'python-cairo' 'gtk4'
         'gtk4-layer-shell' 'at-spi2-core' 'pipewire-pulse' 'libnotify'
         'wl-clipboard' 'ffmpeg' 'vulkan-icd-loader' 'librsvg' 'fontconfig'
         'hicolor-icon-theme' 'foot')
optdepends=('fcitx5: insert dictation directly through the input method'
            'vulkan-intel: local processing on Intel GPUs'
            'vulkan-radeon: local processing on AMD GPUs')
makedepends=('minisign')
options=('!debug' '!strip')
_release="https://tux.superwhisper.com/x86_64/v$_version/superwhisper-omarchy-x86_64-$_version.tar.zst"
source=("superwhisper-$pkgver.tar.zst::$_release"
        "superwhisper-$pkgver.tar.zst.minisig::$_release.minisig"
        'linux-release.pub'
        'superwhisper'
        'superwhisper.service'
        'superwhisper.desktop'
        'superwhisper-url.desktop'
        'setup-user')
noextract=("superwhisper-$pkgver.tar.zst")
sha256sums=('db0752301ccb6e2cf9af87dd4a28338ada11db45ee122dcebd27f52ba51998e7'
            '97e897dc276a4566386ef591a4976a8a88f780b4300370472761fb8b709b3c42'
            '0672b5230ed6390f9168eaea009c6ce1e8838f0961a87888d9073d1e1db5267d'
            '54a0fd574f2b123af24031e910fe0dc47be1d4c055ee5df775f1c681e6201c93'
            '8b9a2cb81a6ea015d663c9981ba67edafe934eb0e56904fab5030fbbfdd8506b'
            'c65169c8432223f26b072c8defe9ff65ef82c26be34cd92a9617ed112cb22570'
            'e67978d239e12a386ec335346b14e10cc7e3bed8b9b1d4bcbed4377f10d4695a'
            'ffbf2f42e403fca85eb73810742397884fbf949b715ed33cb98f618f33556c16')

prepare() {
  minisign -Vm "superwhisper-$pkgver.tar.zst" -x "superwhisper-$pkgver.tar.zst.minisig" -p linux-release.pub
  bsdtar -xf "superwhisper-$pkgver.tar.zst"
  local bundle="superwhisper-omarchy-x86_64-$_version"
  [[ $(< "$bundle/VERSION") == "$_version" ]] || return 1
  # Qt 6.12 adds a Color type that shadows Omarchy's palette singleton.
  python - "superwhisper-omarchy-x86_64-$_version/app/assets/omarchy-plugin/superwhisper-panel" <<'PYTHON'
from pathlib import Path
import re
import sys

for path in Path(sys.argv[1]).glob("*.qml"):
  original = path.read_text()
  updated = re.sub(r"(?<![\w.])Color\b", "Commons.Color", original)
  if updated != original:
    alias = r"(?m)^import qs\.Commons(?: [0-9.]+)? as Commons[ \t]*$"
    if not re.search(alias, updated):
      updated, imports = re.subn(r"(?m)^(import qs\.Commons(?: [0-9.]+)?[ \t]*)$", r"\1\nimport qs.Commons as Commons", updated, count=1)
      if imports != 1:
        raise SystemExit(f"Cannot qualify palette import in {path}")
    if len(re.findall(alias, updated)) != 1 or re.search(r"(?<![\w.])Color\b", updated):
      raise SystemExit(f"Invalid palette references in {path}")
    path.write_text(updated)
PYTHON
}

package() {
  local bundle="superwhisper-omarchy-x86_64-$_version"
  install -dm755 "$pkgdir/opt/superwhisper"
  cp -a --no-preserve=ownership "$bundle/app/." "$pkgdir/opt/superwhisper/"
  # Omarchy owns its Dictation menu; omit the portable installer's menu writer.
  rm -f "$pkgdir/opt/superwhisper/scripts/omarchy-menu-rows.py"
  install -Dm755 superwhisper "$pkgdir/usr/bin/superwhisper"
  install -Dm644 superwhisper.service "$pkgdir/usr/lib/systemd/user/superwhisper.service"
  install -Dm644 superwhisper.desktop "$pkgdir/usr/share/applications/superwhisper.desktop"
  install -Dm644 superwhisper-url.desktop "$pkgdir/usr/share/applications/superwhisper-url.desktop"
  install -Dm755 setup-user "$pkgdir/usr/share/superwhisper/setup-user"
  install -Dm644 -t "$pkgdir/usr/share/licenses/$pkgname/" "$bundle"/licenses/*
  install -Dm644 "$bundle/README.md" "$pkgdir/usr/share/doc/$pkgname/README.md"
  install -Dm644 "$bundle/app/assets/linux-status/SuperwhisperTUI.ttf" "$pkgdir/usr/share/fonts/superwhisper/SuperwhisperTUI.ttf"
  for size in 16 32 64 128 256 512 1024; do
    install -Dm644 "$bundle/app/assets/app-icon/$size.png" "$pkgdir/usr/share/icons/hicolor/${size}x${size}/apps/superwhisper.png"
  done
  install -dm755 "$pkgdir/usr/share/fcitx5/addon"
  sed 's|@LIBRARY@|/opt/superwhisper/lib/fcitx5/libsuperwhisper-fcitx5|' \
    "$bundle/app/assets/fcitx5/superwhisper.conf.in" > "$pkgdir/usr/share/fcitx5/addon/superwhisper.conf"
}
