# Ghostty for x86_64 and aarch64, built from the upstream release source tarball.
#
# Ghostty 1.3.x requires Zig 0.15.2 exactly. Distribution toolchains can move
# ahead, so use the verified upstream toolchain for each architecture only at
# package-build time rather than publishing a second Zig package.

pkgbase=ghostty
pkgname=(ghostty ghostty-shell-integration ghostty-terminfo ghostty-nautilus)
pkgver=1.3.1
pkgrel=3
pkgdesc='Fast, native, feature-rich terminal emulator pushing modern features'
arch=(x86_64 aarch64)
url='https://github.com/ghostty-org/ghostty'
license=(MIT)
depends=(
  bzip2
  fontconfig
  freetype2
  glib2
  glibc
  gtk4
  gtk4-layer-shell
  harfbuzz
  libadwaita
  libpng
  oniguruma
  pixman
  wayland
  zlib
)
makedepends=(
  blueprint-compiler
  curl
  gettext
  pkgconf
)
_zigver=0.15.2
_archive="$pkgbase-$pkgver"
source=(
  "https://release.files.ghostty.org/$pkgver/$_archive.tar.gz"
  'build-data-llvm.patch'
)
sha256sums=(
  '3349d25600ffbda281197a18314f7d18791969cffe9474f0ff16a45a9ebfccdb'
  'd9f5781b748651fa1ff7b919f4a79cd8570118faefe2848f64f02ea4220257ba'
)
source_x86_64=("https://ziglang.org/download/$_zigver/zig-x86_64-linux-$_zigver.tar.xz")
sha256sums_x86_64=('02aa270f183da276e5b5920b1dac44a63f1a49e55050ebde3aecc9eb82f93239')
source_aarch64=("https://ziglang.org/download/$_zigver/zig-aarch64-linux-$_zigver.tar.xz")
sha256sums_aarch64=('958ed7d1e00d0ea76590d27666efbf7a932281b3d7ba0c6b01b0ff26498f667f')

prepare() {
  cd "$_archive"
  # Zig's native x86 linker cannot read .sframe relocations in Arch's crt1.o.
  # Use bundled LLVM for the build-data helper, as the main executable does.
  if [[ "$CARCH" == x86_64 ]]; then
    patch -Np1 -i "$srcdir/build-data-llvm.patch"
  fi
  PATH="$srcdir/zig-$CARCH-linux-$_zigver:$PATH" \
    ZIG_GLOBAL_CACHE_DIR="$srcdir/zig-global-cache" \
    ./nix/build-support/fetch-zig-cache.sh
}

build() {
  cd "$_archive"
  # A '-' suffix is a SemVer prerelease and selects Ghostty's tip channel.
  # Keep the package revision as build metadata on the stable release.
  PATH="$srcdir/zig-$CARCH-linux-$_zigver:$PATH" \
    DESTDIR=build \
    zig build \
      --prefix /usr \
      --system "$srcdir/zig-global-cache/p" \
      -Doptimize=ReleaseFast \
      -Dgtk-x11=true \
      -Dcpu=baseline \
      -Dpie=true \
      -Demit-docs=false \
      -Dversion-string="$pkgver+omarchy.$pkgrel" \
      --build-id=sha1
}

package_ghostty() {
  depends+=(ghostty-shell-integration ghostty-terminfo)
  optdepends=('ghostty-nautilus: Open in Ghostty context menu in GNOME Files')

  cd "$_archive"
  cp -a build/* "$pkgdir/"
  install -Dm0644 LICENSE "$pkgdir/usr/share/licenses/ghostty/LICENSE"
  rm -r "$pkgdir/usr/share/terminfo" \
    "$pkgdir/usr/share/ghostty/shell-integration" \
    "$pkgdir/usr/share/nautilus-python"
}

package_ghostty-shell-integration() {
  pkgdesc='Shell integration scripts for Ghostty'
  depends=()

  cd "$_archive"
  install -d "$pkgdir/usr/share/ghostty/shell-integration"
  cp -a build/usr/share/ghostty/shell-integration/. \
    "$pkgdir/usr/share/ghostty/shell-integration/"
  install -Dm0644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

package_ghostty-terminfo() {
  pkgdesc='Terminfo for Ghostty'
  depends=()

  cd "$_archive"
  install -d "$pkgdir/usr/share/terminfo"
  cp -a build/usr/share/terminfo/x "$pkgdir/usr/share/terminfo/"
  install -Dm0644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}

package_ghostty-nautilus() {
  pkgdesc='Open in Ghostty for GNOME Files'
  depends=(ghostty nautilus-python)
  license=(GPL-2.0-or-later)

  cd "$_archive"
  install -d "$pkgdir/usr/share/nautilus-python"
  cp -a build/usr/share/nautilus-python/. "$pkgdir/usr/share/nautilus-python/"
}
