# Maintainer: petrouil
# Contributor: Omarchy

pkgname=grok-bot
pkgver=0.47.0
pkgrel=1
_commit=c1e7d7a46549956d25f53e9c0b9f59666e03aa3a
pkgdesc='Grok Bot desktop agent'
arch=('x86_64' 'aarch64')
url='https://x.ai/bot'
license=('custom')
depends=(
  'alsa-lib'
  'at-spi2-core'
  'gtk3'
  'hicolor-icon-theme'
  'libnotify'
  'libsecret'
  'libxss'
  'libxtst'
  'nss'
  'util-linux-libs'
  'xdg-utils'
)
optdepends=('libappindicator-gtk3: tray support')
provides=('sand')
conflicts=('sand')
options=('!strip' '!debug')
install=grok-bot.install

_deb_x86_64="grok-bot_${pkgver}_amd64.deb"
_deb_aarch64="grok-bot_${pkgver}_arm64.deb"
source=(
  'grok-bot.sh'
  'grok-bot.desktop'
)
source_x86_64=(
  "${_deb_x86_64}::https://downloads.cursor.com/grokbot/stable/${_commit}/linux/x64/${_deb_x86_64}"
)
source_aarch64=(
  "${_deb_aarch64}::https://downloads.cursor.com/grokbot/stable/${_commit}/linux/arm64/${_deb_aarch64}"
)
sha256sums=('6dfa6c305941afa6cbaefbeaae06d05ab5a88f31630005d25a819a160c20c7a3'
            '3e2a2461ea58d17ac1777616be9ba660f7cb9ceefa9292016e36c55758bf78dd')
sha256sums_x86_64=('11ca0f51a535b97af51a352adf9c0f9ecd2e1b0430a69ae9451b688a7a065808')
sha256sums_aarch64=('836f8d19d3826c6573c31ac45c7a9b797abc73381ae0d2b1e7a8dae5410e7e46')
noextract=("${_deb_x86_64}" "${_deb_aarch64}")

package() {
  local deb_var="_deb_${CARCH}"
  local deb="${!deb_var}"

  bsdtar -xOf "${srcdir}/${deb}" data.tar.xz |
    bsdtar -x -C "${pkgdir}" -f -

  rm -rf "${pkgdir}/usr/share/doc" \
         "${pkgdir}/usr/share/applications/sand.desktop" \
         "${pkgdir}/usr/share/applications/grok-bot.desktop"

  # Always install our Wayland wrapper; do not keep any /usr/bin from the .deb.
  rm -f "${pkgdir}/usr/bin/grok-bot" "${pkgdir}/usr/bin/sand"
  install -Dm755 "${srcdir}/grok-bot.sh" "${pkgdir}/usr/bin/grok-bot"
  ln -s grok-bot "${pkgdir}/usr/bin/sand"
  install -Dm644 "${srcdir}/grok-bot.desktop" \
    "${pkgdir}/usr/share/applications/grok-bot.desktop"

  install -Dm644 "${pkgdir}/opt/Grok Bot/LICENSE.electron.txt" \
    "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE.electron.txt"
  install -Dm644 "${pkgdir}/opt/Grok Bot/LICENSES.chromium.html" \
    "${pkgdir}/usr/share/licenses/${pkgname}/LICENSES.chromium.html"

  # Ship chrome-sandbox without setuid. Upstream's build-time userns probe
  # would measure the CI container, not the user's machine, and a setuid
  # helper could not exec from "/opt/Grok Bot/" anyway (electron#44414).
  # grok-bot.install tells users on kernels without unprivileged user
  # namespaces how to run without the sandbox.
  chmod 0755 "${pkgdir}/opt/Grok Bot/chrome-sandbox"
}
