# Maintainer: aur.chaotic.cx

: ${CARGO_HOME:=$SRCDEST/cargo-home}
: ${CARGO_TARGET_DIR:=target}
: ${RUSTUP_TOOLCHAIN:=stable}
export CARGO_HOME CARGO_TARGET_DIR RUSTUP_TOOLCHAIN

_pkgname="niri"
pkgname="$_pkgname-git"
pkgver=25.08.r155.g7f19d26
pkgrel=2
pkgdesc="Scrollable-tiling Wayland compositor"
url="https://github.com/YaLTeR/niri"
license=('GPL-3.0-or-later')
arch=('x86_64')

depends=(
  'cairo'
  'glib2'
  'libdisplay-info'
  'libinput'
  'libpipewire'
  'libxkbcommon'
  'mesa'
  'pango'
  'pixman'
  'seatd'
  'systemd-libs'
  'xdg-desktop-portal-impl'
)
makedepends=(
  'clang'
  'git'
  'rust'
)
optdepends=(
  'alacritty: a suggested GPU-accelerated terminal emulator'
  'fuzzel: a suggested Wayland application launcher'
  'mako: a suggested Wayland notification daemon'
  'org.freedesktop.secrets: for apps to rely on secrets portal'
  'swaybg: a suggested Wayland wallpaper tool'
  'swaylock: a suggested Wayland screen locker'
  'waybar: a suggested Wayland customizable desktop bar'
  'xdg-desktop-portal-gnome: a XDG desktop portal required for screencasting'
  'xdg-desktop-portal-gtk: a suggested XDG desktop portal'
  'xwayland-satellite: for running X11 apps in XWayland'
)

provides=(
  "$_pkgname"
  'wayland-compositor'
)
conflicts=("$_pkgname")

options=('!lto')

_pkgsrc="$_pkgname"
source=("$_pkgsrc"::"git+$url.git")
sha256sums=('SKIP')

pkgver() {
  cd "$_pkgsrc"
  git describe --long --tags --abbrev=7 --exclude='*[a-zA-Z][a-zA-Z]*' \
    | sed -E 's/^[^0-9]*//;s/([^-]*-g)/r\1/;s/-/./g'
}

build() {
  cd "$_pkgsrc"
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
  cargo build --frozen --release --features default

  # generate shell completions
  for shell in bash fish zsh; do
    cargo run --frozen --release --bin niri -- \
      completions "$shell" > "$shell-completions"
  done

  # Prevent warning: 'Package contains reference to $srcdir'
  #export CARGO_ENCODED_RUSTFLAGS="--remap-path-prefix=${srcdir}=/"
}

package() {
  cd "$_pkgsrc"
  install -Dm755 {$CARGO_TARGET_DIR/release/$_pkgname,resources/$_pkgname-session} -t "$pkgdir/usr/bin/"
  install -Dm644 resources/$_pkgname{.service,-shutdown.target} -t "$pkgdir/usr/lib/systemd/user/"
  install -Dm644 resources/$_pkgname.desktop -t "$pkgdir/usr/share/wayland-sessions/"
  install -Dm644 resources/$_pkgname-portals.conf -t "$pkgdir/usr/share/xdg-desktop-portal/"
  install -Dm644 resources/default-config.kdl README.md -t "$pkgdir/usr/share/doc/$_pkgname/"

  # shell auto-completions
  install -Dm644 bash-completions "$pkgdir/usr/share/bash-completion/completions/niri"
  install -Dm644 fish-completions "$pkgdir/usr/share/fish/vendor_completions.d/niri.fish"
  install -Dm644 zsh-completions "$pkgdir/usr/share/zsh/site-functions/_niri"
}
