# Maintainer: Antonio Cotos <acf981@protonmail.com>
# The AUR name `sping` belongs to another program: this package is
# lambdabytes-sping, its command is still `sping`.
pkgname=lambdabytes-sping
_pkgname=sping
pkgver=1.8.0
pkgrel=1
pkgdesc="Terminal-native real-time connectivity monitor with network context diagnostics"
arch=('x86_64' 'aarch64' 'armv7h')
url="https://github.com/LambdaBytes/sping"
license=('MIT')
depends=('glibc' 'libgcc')
makedepends=('cargo')
# Each of these installs /usr/bin/sping: sping and sping-git are other
# programs, sping-bin is a third party's repackaging of this one's release
# binaries.
conflicts=('sping' 'sping-git' 'sping-bin')
# The release profile strips the binary (Cargo.toml): a -debug package
# would be empty.
options=('!debug')
source=("$_pkgname-$pkgver.tar.gz::$url/archive/refs/tags/v$pkgver.tar.gz")
# Replace SKIP with the real digest once a v$pkgver tag exists:
#   updpkgsums   (from pacman-contrib)
sha256sums=('SKIP')

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

build() {
  cd "sping-$pkgver"
  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 "sping-$pkgver"
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  cargo test --frozen --release
}

package() {
  cd "sping-$pkgver"
  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"
}
