# Maintainer: petrouil
# Contributor: Omarchy

pkgname=grok-bot
pkgver=0.29.0
pkgrel=1
_commit=f0e5bfcee649ea84c0c61369cf896cd146d72136
pkgdesc='Grok Bot desktop agent'
arch=('x86_64')
url='https://x.ai/bot'
license=('custom')
depends=(
  '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')
source=(
  "${pkgname}_${pkgver}.deb::https://downloads.cursor.com/grokbot/stable/${_commit}/linux/x64/Grok_Bot_${pkgver}.deb"
  'grok-bot.sh'
  'grok-bot.desktop'
)
sha256sums=('d223b5830282aef11d5c46d8f4d1edd239bf992e336405cbd288d4476b9233d4'
            '6dfa6c305941afa6cbaefbeaae06d05ab5a88f31630005d25a819a160c20c7a3'
            '856056c9ca63dda5d01158ce8fb6a9a7cbb3f67c13a92b573cd196d3e50f26e7')
noextract=("${pkgname}_${pkgver}.deb")

package() {
  bsdtar -xOf "${srcdir}/${pkgname}_${pkgver}.deb" data.tar.xz |
    bsdtar -x -C "${pkgdir}" -f -

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

  local icon1024="${pkgdir}/usr/share/icons/hicolor/1024x1024/apps"
  if [[ -f "${icon1024}/sand.png" && ! -f "${icon1024}/grok-bot.png" ]]; then
    install -Dm644 "${icon1024}/sand.png" "${icon1024}/grok-bot.png"
  fi
  rm -f "${icon1024}/sand.png"
  if [[ -f "${icon1024}/grok-bot.png" ]]; then
    install -Dm644 "${icon1024}/grok-bot.png" \
      "${pkgdir}/usr/share/icons/hicolor/512x512/apps/grok-bot.png"
  fi

  # 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"

  if ! { [[ -L /proc/self/ns/user ]] && unshare --user true; }; then
    chmod 4755 "${pkgdir}/opt/Grok Bot/chrome-sandbox"
  else
    chmod 0755 "${pkgdir}/opt/Grok Bot/chrome-sandbox"
  fi
}
