# Maintainer: Antonio Cotos <acf981@protonmail.com>
# VCS package: builds the latest master. Install as `sping-git`.
pkgname=sping-git
_pkgname=sping
pkgver=1.8.0
pkgrel=1
pkgdesc="Terminal-native real-time connectivity monitor with network context diagnostics (git)"
arch=('x86_64' 'aarch64' 'armv7h')
url="https://github.com/LambdaBytes/sping"
license=('MIT')
depends=('gcc-libs')
makedepends=('cargo' 'git')
provides=("$_pkgname")
conflicts=("$_pkgname")
source=("$_pkgname::git+$url.git")
sha256sums=('SKIP')

pkgver() {
  cd "$_pkgname"
  # 1.5.0.r42.gabcdef1 — tag count + short hash, sortable by pacman.
  # pipefail: without it sed's status hides a failed `git describe` (no tags
  # reachable) and the fallback below never runs.
  (
    set -o pipefail
    git describe --long --tags --abbrev=7 2>/dev/null | sed 's/^v//;s/\([^-]*-g\)/r\1/;s/-/./g' \
      || printf "r%s.%s" "$(git rev-list --count HEAD)" "$(git rev-parse --short=7 HEAD)"
  )
}

prepare() {
  cd "$_pkgname"
  export RUSTUP_TOOLCHAIN=stable
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "$_pkgname"
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  # build.rs renders the man page and shell completions here (OUT_DIR
  # otherwise).
  export SPING_ASSETS_DIR="$PWD/target/assets"
  cargo build --frozen --release
}

check() {
  cd "$_pkgname"
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  cargo test --frozen --release
}

package() {
  cd "$_pkgname"
  install -Dm0755 "target/release/$_pkgname" "$pkgdir/usr/bin/$_pkgname"
  install -Dm0644 target/assets/man/sping.1 "$pkgdir/usr/share/man/man1/sping.1"
  install -Dm0644 target/assets/completions/sping.bash \
    "$pkgdir/usr/share/bash-completion/completions/sping"
  install -Dm0644 target/assets/completions/_sping \
    "$pkgdir/usr/share/zsh/site-functions/_sping"
  install -Dm0644 target/assets/completions/sping.fish \
    "$pkgdir/usr/share/fish/vendor_completions.d/sping.fish"
  install -Dm0644 LICENSE "$pkgdir/usr/share/licenses/$_pkgname/LICENSE"
  install -Dm0644 README.md "$pkgdir/usr/share/doc/$_pkgname/README.md"
}
