# 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.9.6+build89647
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'
  'docker: containerized local GPU runtime for on-device models'
  'nvidia-container-toolkit: containerized local GPU runtime for on-device models'
)

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=('c08baea924b9591367ec176493d1638d0175a0e48f13373d621bc03273ead9f6')
sha256sums_aarch64=('914b0b39dd8ec4658823c39093c127815fa1e6a7ad2257b03e1ff23b4f9f286e')

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

  # 26.9.1 started shipping a systemd unit under /lib, and owning /lib -- a
  # symlink owned by the filesystem package -- makes pacman refuse the whole
  # transaction. The unit is also a no-op here: its setup script apt-installs
  # Docker and the NVIDIA Container Toolkit and exits on any distro but Ubuntu
  # (install those yourself; see optdepends). Delete it, and its parents once
  # emptied; then anything else left outside opt/ and usr/ -- whatever its
  # type, an empty bin/ or lib64/ included, since those are filesystem-owned
  # symlinks too -- stops the build rather than shipping the next conflict.
  (
    cd "${pkgdir}"
    rm -f lib/systemd/system/perplexity-local-runtime-setup.service
    rmdir -p lib/systemd/system 2>/dev/null || true
  )

  local unexpected
  unexpected=$(cd "${pkgdir}" && find . -mindepth 1 \
    -path ./opt -prune -o -path ./usr -prune -o -print)
  if [[ -n "${unexpected}" ]]; then
    echo "Unexpected entries outside opt/ and usr/ in the upstream deb:" >&2
    echo "${unexpected}" >&2
    return 1
  fi

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