# Maintainer: Spencer Bull <spencerbull2554@gmail.com>

# cua-driver ships prebuilt from the trycua/cua monorepo release feed, so this
# repackages the vendor tarball. Upstream keeps the CLI, its cursor-theme
# compiler, and the SDK artifacts together in one directory and exposes the
# CLI through a symlink -- the binary resolves its helpers as siblings of
# /proc/self/exe -- so the whole tree lands under /usr/lib/cua-driver with a
# /usr/bin symlink. Upstream updates are held ("sync": false): Omarchy bumps
# the version and checksums below by hand, from the release's checksums.txt.
# .omarchy/upstream.sh still selects releases for when the hold lifts.
#
# The binary carries its own updater: `cua-driver update --apply` pipes the
# vendor installer into bash, which would install a second copy under
# ~/.cua-driver and link it into ~/.local/bin, stepping around pacman and
# this repository's release gate. Driver 0.27.0 detects pacman ownership, but
# deliberately treats failed and timed-out ownership queries as unmanaged.
# A /usr/bin wrapper would not cover MCP configurations that record the
# resolved executable, so prepare() rewrites the installer URL inside the
# binary to point at pm.sh, a stand-in that declines and names pacman instead.

pkgname=cua-driver-bin
# Held at 0.28.2: 0.28.3 and newer break screenshots. Bump by hand once a
# fixed release is verified.
pkgver=0.28.2
pkgrel=3
pkgdesc="Computer-use driver for native GUI apps: accessibility-tree snapshots and input injection"
arch=('x86_64' 'aarch64')
url="https://github.com/trycua/cua"
license=('MIT')
# at-spi2-core carries the AT-SPI accessibility bus the driver reads GUI
# trees through; the X libraries are linked, not dlopen'd.
depends=(
  'at-spi2-core'
  'gcc-libs'
  'glibc'
  'libx11'
  'libxcb'
  'libxext'
  'libxi'
  'libxkbcommon'
)
provides=("cua-driver=${pkgver}")
conflicts=('cua-driver')
# Prebuilt Rust binaries ship byte-exact apart from the installer rewrite
# below: their build ids are what upstream symbolication matches.
options=('!strip' '!debug')
source=('LICENSE' 'pm.sh')
source_x86_64=("https://github.com/trycua/cua/releases/download/cua-driver-rs-v${pkgver}/cua-driver-rs-${pkgver}-linux-x86_64.tar.gz")
source_aarch64=("https://github.com/trycua/cua/releases/download/cua-driver-rs-v${pkgver}/cua-driver-rs-${pkgver}-linux-arm64.tar.gz")
sha256sums=('c0779290c1d4783169aa3dbfb55feb505e563ef8a004bbf55298ceffcfbda8d9'
            'c76e251c3ed424200eac52bec35ba534336307fabd83a175ab0b47e2084ab0d8')
sha256sums_x86_64=('8f3e5b669e2bcd98d0eecc64f40640aac77f358b6332a06abc6ee79991620f7d')
sha256sums_aarch64=('cadd7e6b757c3ce50f2b5f6e273c154ea48450fb5fcaff744209b382915eddf5')

case "${CARCH}" in
  x86_64) _platform="linux-x86_64" ;;
  aarch64) _platform="linux-arm64" ;;
esac
_vendor_tree="cua-driver-rs-${pkgver}-${_platform}"

# Rust strings carry their length out of band, so the replacement has to be
# exactly as long as the original, which is what fixes the
# stand-in's short name and location.
_vendor_installer='https://cua.ai/driver/install.sh'
_pacman_installer='file:///usr/lib/cua-driver/pm.sh'

prepare() {
  cd "${srcdir}/${_vendor_tree}"

  if (( ${#_vendor_installer} != ${#_pacman_installer} )); then
    echo "installer URLs must be the same length to rewrite in place" >&2
    return 1
  fi

  # In 0.28.1 the URL appears in the updater, the printed reinstall command,
  # and two embedded copies of Skills/cua-driver/README.md. Rewrite all four
  # so the embedded instructions also defer to pacman. Any other count means
  # the release layout changed and needs review before packaging.
  local expected=4 found
  found=$(grep -obUaF "${_vendor_installer}" cua-driver | wc -l)
  if (( found != expected )); then
    echo "expected the vendor installer URL ${expected} times in cua-driver, found ${found}" >&2
    return 1
  fi

  local size_before size_after
  size_before=$(stat -c %s cua-driver)
  sed -i "s|${_vendor_installer//./\\.}|${_pacman_installer}|g" cua-driver
  size_after=$(stat -c %s cua-driver)

  if (( size_before != size_after )) \
      || grep -qUaF "${_vendor_installer}" cua-driver \
      || (( $(grep -obUaF "${_pacman_installer}" cua-driver | wc -l) != expected )); then
    echo "installer URL rewrite did not land cleanly in cua-driver" >&2
    return 1
  fi
}

package() {
  cd "${srcdir}/${_vendor_tree}"

  # The vendor tree stays together: cua-driver execs cua-cursor-theme as a
  # sibling of the resolved binary, and the SDK library, node runtime, ABI
  # header, and the GNOME wayland-helper extension are versioned with it.
  install -d "${pkgdir}/usr/lib/cua-driver"
  cp -a . "${pkgdir}/usr/lib/cua-driver/"
  chmod -R a+rX "${pkgdir}/usr/lib/cua-driver"

  # The path prepare() wrote into the binary.
  install -Dm755 "${srcdir}/pm.sh" "${pkgdir}/usr/lib/cua-driver/pm.sh"

  install -d "${pkgdir}/usr/bin"
  ln -s ../lib/cua-driver/cua-driver "${pkgdir}/usr/bin/cua-driver"

  install -Dm644 "${srcdir}/LICENSE" "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
}
