# Maintainer: David Heinemeier Hansson <david@hey.com>

_pkgname=herdr
pkgname="$_pkgname-git"
pkgver=0.8.0.r129.ga5c69be
pkgrel=1
pkgdesc="Herdr terminal workspace manager built from upstream master"
arch=('x86_64' 'aarch64')
url="https://github.com/herdrdev/herdr"
license=('Apache-2.0')
depends=('gcc-libs' 'glibc')
makedepends=('cargo' 'git')
provides=("$_pkgname")
conflicts=("$_pkgname" 'omarchy-herdr')
replaces=("$_pkgname" 'omarchy-herdr')
options=('!debug' '!lto')

_commit=a5c69beabfc82d9c3f9563eb821139b2e0f3e14f
_zigver=0.15.2
source=("$_pkgname"::"git+$url.git#commit=$_commit")
source_x86_64=("zig-x86_64-linux-$_zigver.tar.xz::https://ziglang.org/download/$_zigver/zig-x86_64-linux-$_zigver.tar.xz")
source_aarch64=("zig-aarch64-linux-$_zigver.tar.xz::https://ziglang.org/download/$_zigver/zig-aarch64-linux-$_zigver.tar.xz")
sha256sums=('SKIP')
sha256sums_x86_64=('02aa270f183da276e5b5920b1dac44a63f1a49e55050ebde3aecc9eb82f93239')
sha256sums_aarch64=('958ed7d1e00d0ea76590d27666efbf7a932281b3d7ba0c6b01b0ff26498f667f')

pkgver() {
  cd "$_pkgname"

  # Preview tags sit on master between releases, so an unexcluded describe bases the version on
  # a build id. Failing beats any fallback: with no release tag every candidate sorts lower.
  ( set -o pipefail
    git describe --long --tags --abbrev=7 --exclude='preview-*' \
      | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g'
  )
}

prepare() {
  cd "$_pkgname"

  cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}

build() {
  cd "$_pkgname"

  export CARGO_TARGET_DIR=target
  export ZIG="$srcdir/zig-$CARCH-linux-$_zigver/zig"
  export ZIG_GLOBAL_CACHE_DIR="$srcdir/zig-cache"
  # Master's Cargo.toml already carries the next version, so an unmarked build claims a
  # release upstream has not published and speaks a protocol that release will not have.
  export HERDR_BUILD_CHANNEL=omarchy
  export HERDR_BUILD_ID="$(git rev-parse --short=7 HEAD)"
  cargo build --frozen --release
}

package() {
  cd "$_pkgname"

  install -Dm755 target/release/herdr "$pkgdir/usr/bin/herdr"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
