# Maintainer: Spencer Bull <spencer@omarchy.org>

# Omarchy tracks Perplexity's own Debian repository, the feed the app updates
# itself from. .omarchy/upstream.sh rewrites the version and checksums below
# from that repository's package index.

pkgname=perplexity
pkgver=26.8.4+build50522
pkgrel=1
pkgdesc="Official Perplexity desktop app"
arch=('x86_64' 'aarch64')
url="https://www.perplexity.ai"
# Upstream ships no license text with the app; its terms live at
# https://www.perplexity.ai/hub/legal/terms-of-service
license=('custom')

depends=(
  'alsa-lib'
  'at-spi2-core'
  'bash'
  'bubblewrap'
  'cairo'
  'dbus'
  'expat'
  'gcc-libs'
  'gdk-pixbuf2'
  'glib2'
  'glibc'
  'gtk3'
  'hicolor-icon-theme'
  'libcups'
  'libdrm'
  'libglvnd'
  'libnotify'
  'libsecret'
  'libx11'
  'libxcb'
  'libxcomposite'
  'libxdamage'
  'libxext'
  'libxfixes'
  'libxkbcommon'
  'libxrandr'
  'libxss'
  'libxtst'
  'mesa'
  'nspr'
  'nss'
  'pango'
  'systemd-libs'
  'util-linux-libs'
  'vulkan-icd-loader'
  'xdg-utils'
)

optdepends=(
  'apparmor: confine the app with the bundled user-namespace profile'
  'libappindicator-gtk3: tray icon support'
)

makedepends=('libarchive')
options=('!debug' '!strip')

# Omarchy: the build image compresses with zstd at its default level. Maximum
# zstd buys roughly a third off this ~600 MB Electron tree for a few extra
# minutes of build time -- worth it for a package this large on the fast ring,
# same reasoning as openai-codex-desktop.
COMPRESSZST=(zstd -c -z -q --ultra -22 --threads=0 -)

# The pool filename carries a build number the index's Version field drops, and
# upstream rebuilds under the same marketing version, so pkgver is the full
# string from the filename. The pool's storage treats a literal '+' in a URL as
# a space and answers 403, hence the %2B in the download URL only.
_deb_x86_64="perplexity_${pkgver}_amd64.deb"
_deb_aarch64="perplexity_${pkgver}_arm64.deb"
_pool="https://packages.perplexity.ai/deb/pool/main/p/perplexity"
source=('perplexity-launcher.sh')
source_x86_64=("${_deb_x86_64}::${_pool}/${_deb_x86_64//+/%2B}")
source_aarch64=("${_deb_aarch64}::${_pool}/${_deb_aarch64//+/%2B}")
noextract=("${_deb_x86_64}" "${_deb_aarch64}")
sha256sums=('b180ca6fbd268712a277a34d4215f176001c0031a4f6360686458623f47fad65')
sha256sums_x86_64=('47ef208aaee9395497cefe8fbc65beb2a5c37dd8bd974125e4c4a0e3be2fd103')
sha256sums_aarch64=('5ac7b5f597e03a888f92a3aaa643138f6cc78a50fd891bc7afabfe86e1e2b994')

package() {
  cd "${srcdir}"

  local deb_var="_deb_${CARCH}"
  local deb="${!deb_var}"

  bsdtar -xOf "${deb}" data.tar.xz |
    bsdtar --no-same-owner -xf - -C "${pkgdir}"

  # pacman never runs the deb's postinst, so the /usr/bin entry it would have
  # symlinked is a launcher here instead, and the menu entry goes through it so
  # the flags file applies there too. The scheme handler stays upstream's.
  install -Dm755 perplexity-launcher.sh "${pkgdir}/usr/bin/perplexity"
  sed -i 's|^Exec=.*|Exec=perplexity %U|' \
    "${pkgdir}/usr/share/applications/perplexity.desktop"

  # Arch ships Chromium's and Electron's own sandbox helper setuid, which is
  # what keeps the sandbox up on a kernel that denies unprivileged user
  # namespaces -- linux-hardened, mainly -- instead of aborting the app. The
  # deb leaves this bit to its postinst.
  chmod 4755 "${pkgdir}/opt/Perplexity/chrome-sandbox"

  # The profile upstream's postinst would install. It names /opt/Perplexity
  # paths, which this package keeps.
  install -Dm644 "${pkgdir}/opt/Perplexity/resources/apparmor-profile" \
    "${pkgdir}/etc/apparmor.d/perplexity"

  # Debian package-policy files are not used on Arch Linux.
  rm -rf "${pkgdir}/usr/share/doc"
}
