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

pkgname=superwhisper-bin
pkgver=1.0.2
_version=1.0.2
pkgrel=1
pkgdesc='Local and cloud voice dictation with Superwhisper'
arch=('x86_64' 'aarch64')
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'
            'vulkan-asahi: local processing on Apple M1 and M2 GPUs'
            'vulkan-freedreno: local processing on Snapdragon X GPUs'
            'nvidia-utils: local processing on NVIDIA GPUs')
makedepends=('minisign')
options=('!debug' '!strip')
_release_x86_64="https://tux.superwhisper.com/x86_64/v$_version/superwhisper-omarchy-x86_64-$_version.tar.zst"
_release_aarch64="https://tux.superwhisper.com/aarch64/v$_version/superwhisper-omarchy-aarch64-$_version.tar.zst"
source=('linux-release.pub'
        'superwhisper'
        'superwhisper.service'
        'superwhisper.desktop'
        'superwhisper-url.desktop'
        'setup-user')
source_x86_64=("superwhisper-$pkgver-x86_64.tar.zst::$_release_x86_64"
               "superwhisper-$pkgver-x86_64.tar.zst.minisig::$_release_x86_64.minisig")
source_aarch64=("superwhisper-$pkgver-aarch64.tar.zst::$_release_aarch64"
                "superwhisper-$pkgver-aarch64.tar.zst.minisig::$_release_aarch64.minisig")
sha256sums=('0672b5230ed6390f9168eaea009c6ce1e8838f0961a87888d9073d1e1db5267d'
            '54a0fd574f2b123af24031e910fe0dc47be1d4c055ee5df775f1c681e6201c93'
            '8b9a2cb81a6ea015d663c9981ba67edafe934eb0e56904fab5030fbbfdd8506b'
            'c65169c8432223f26b072c8defe9ff65ef82c26be34cd92a9617ed112cb22570'
            'e67978d239e12a386ec335346b14e10cc7e3bed8b9b1d4bcbed4377f10d4695a'
            'ffbf2f42e403fca85eb73810742397884fbf949b715ed33cb98f618f33556c16')
sha256sums_x86_64=('c51b4688f8bc95ba532617f212f625d674ca493b069749c61b9f9dc5a1bd3680'
                   '85de7213db0f8129456fca06d7191f2cff4fefecb28dc72a4488afb129a16a6c')
sha256sums_aarch64=('4ced4418194c95ee0af5030b6ad25961abbbb4f617871b7775c066ead1f47727'
                    '035f74112b0a4b5dfec33cde6e9fcd25ed084b3147de419c2158315d61215cf2')
noextract=("superwhisper-$pkgver-$CARCH.tar.zst")

prepare() {
  minisign -Vm "superwhisper-$pkgver-$CARCH.tar.zst" -x "superwhisper-$pkgver-$CARCH.tar.zst.minisig" -p linux-release.pub
  bsdtar -xf "superwhisper-$pkgver-$CARCH.tar.zst"
  local bundle="superwhisper-omarchy-$CARCH-$_version"
  [[ $(< "$bundle/VERSION") == "$_version" ]] || return 1
  # Qt 6.12 adds a Color type that shadows Omarchy's palette singleton.
  python - "superwhisper-omarchy-$CARCH-$_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-$CARCH-$_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"
}
