# Maintainer: petrouil
# Contributor: Omarchy

pkgname=grok-bot
pkgver=0.18.0
pkgrel=1
_commit=1618156107ae6cc9b7ab5dc1e3ba5ea5aa1425de
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=('380726acd4372b8a5668340f6ff7faf01b9bb1d4864d074a62ce7e461d17408b'
            '2bdc0c7002200adce8a8676efb02733537df1388cb85c4257452009257570819'
            'a5a8dc8e31485325c336185487b95e1e69c715f498025f1ae774532fe99290b9')
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"

  install -Dm644 "${pkgdir}/usr/share/icons/hicolor/1024x1024/apps/sand.png" \
    "${pkgdir}/usr/share/icons/hicolor/1024x1024/apps/grok-bot.png"
  install -Dm644 "${pkgdir}/usr/share/icons/hicolor/1024x1024/apps/grok-bot.png" \
    "${pkgdir}/usr/share/icons/hicolor/512x512/apps/grok-bot.png"
  rm -f "${pkgdir}/usr/share/icons/hicolor/1024x1024/apps/sand.png"

  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
}
