# Maintainer: omarchy-mac
# Contributor: Burgess Leo (AUR zed-bin)
#
# Zed for aarch64, repacked from the official vendor release.
# Keep the canonical name used by Omarchy and Arch's x86_64 package.
# Adapted from omarchy-mac/omarchy-pkgs-aarch64 PR #25.

pkgname=zed
pkgver=1.22.0
pkgrel=1
pkgdesc='A high-performance, multiplayer code editor from the creators of Atom and Tree-sitter'
arch=('aarch64')
url='https://zed.dev'
license=('GPL-3.0-or-later' 'AGPL-3.0-or-later' 'Apache-2.0')
depends=('alsa-lib'
         'glib2'
         'glibc'
         'gcc-libs'
         'libx11'
         'libxcb'
         'libxkbcommon'
         'libxkbcommon-x11'
         'vulkan-driver'
         'vulkan-icd-loader'
         'wayland'
         'zlib')
optdepends=('openbsd-netcat: SSH remote development support'
            'clang: improved C/C++ language support'
            'cmake: building C/C++ projects'
            'gopls: improved Go language support'
            'pyright: improved Python language support'
            'rust-analyzer: improved Rust language support'
            'typescript-language-server: improved TypeScript/JavaScript support'
            'org.freedesktop.secrets: to keep you logged into your Zed account')
conflicts=('zed-bin' 'zed-git' 'zed-preview-bin')
# The vendor binaries are already stripped.
options=('!strip')
source_aarch64=("${pkgname}-${pkgver}-aarch64.tar.gz::https://github.com/zed-industries/zed/releases/download/v${pkgver}/zed-linux-aarch64.tar.gz")
sha256sums_aarch64=('8b3c5d6e506056a9456ed33072081fd64db84ce47cdf34d4936442cc4f08394a')

package() {
  cd 'zed.app'

  # CLI as zed.real, wrapped so the editor never self-updates: it reads
  # ZED_UPDATE_EXPLANATION at startup and skips its own update polling when
  # it is set. Updates arrive through pacman.
  install -Dm755 'bin/zed' "${pkgdir}/usr/bin/zed.real"
  cat > "${pkgdir}/usr/bin/zed" <<'SH'
#!/bin/sh
export ZED_UPDATE_EXPLANATION='Updates are handled by your package manager'
exec /usr/bin/zed.real "$@"
SH
  chmod 755 "${pkgdir}/usr/bin/zed"
  # Arch extra names the CLI zeditor; keep that spelling working too.
  ln -s zed "${pkgdir}/usr/bin/zeditor"

  # The CLI looks for ../libexec/zed-editor, then ../lib/zed/zed-editor.
  install -Dm755 'libexec/zed-editor' "${pkgdir}/usr/lib/zed/zed-editor"

  # Vendor desktop entry verbatim: Exec=zed and Icon=zed resolve through
  # PATH and hicolor. gtk-launch dev.zed.Zed needs exactly this file name.
  install -Dm644 'share/applications/dev.zed.Zed.desktop' \
    "${pkgdir}/usr/share/applications/dev.zed.Zed.desktop"

  local size
  for size in 512x512 1024x1024; do
    install -Dm644 "share/icons/hicolor/${size}/apps/zed.png" \
      "${pkgdir}/usr/share/icons/hicolor/${size}/apps/zed.png"
  done

  install -Dm644 'licenses.md' "${pkgdir}/usr/share/licenses/${pkgname}/licenses.md"

  # zed.app/lib (bundled libxcb, libstdc++, libxkbcommon, ...) is deliberately
  # not shipped; the depends above provide them. Both binaries carry
  # RPATH=$ORIGIN/../lib, which is harmless when that directory is absent.
}
