# Maintainer: mollusk <jitty+lc1iz0dc@protonmail.com>
pkgname=peerspeak-git
_pkgname=peerspeak
pkgver=0.6.1.r310.g660261a
pkgrel=1
pkgdesc="Decentralized peer-to-peer voice chat (Rust/iroh/PipeWire/Opus/iced)"
arch=('x86_64')
url="https://gitbutter.xyz/mollusk/peerspeak"
license=('MIT')
depends=('pipewire' 'opus')
makedepends=('git' 'cargo' 'pkgconf')
optdepends=('pixelpass: screen sharing inside a room'
            'mpv: screen-share viewer (vlc is used as a fallback)')
provides=('peerspeak')
conflicts=('peerspeak')
options=('!lto' '!debug')
source=("$_pkgname::git+https://gitbutter.xyz/mollusk/peerspeak.git")
sha256sums=('SKIP')

pkgver() {
  cd "$srcdir/$_pkgname"
  # 0.1.0.r<commits>.g<short-sha> — sorts monotonically across rebuilds.
  printf '%s.r%s.g%s' \
    "$(awk -F'\"' '/^version =/{print $2; exit}' Cargo.toml)" \
    "$(git rev-list --count HEAD)" \
    "$(git rev-parse --short HEAD)"
}

prepare() {
  cd "$srcdir/$_pkgname"
  # Vendor deps up front so build() can run --frozen (no surprise network).
  export CARGO_HOME="$srcdir/cargo-home"
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "$srcdir/$_pkgname"
  export CARGO_HOME="$srcdir/cargo-home"
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target
  # Strip the build directory out of paths embedded in the binary (Rust bakes
  # source paths into panic/backtrace metadata that survives stripping), so the
  # package doesn't reference $srcdir. One remap covers our sources and the
  # vendored deps, since CARGO_HOME lives under $srcdir too.
  export RUSTFLAGS="${RUSTFLAGS:-} --remap-path-prefix=$srcdir=/"
  cargo build --frozen --release --bin "$_pkgname"
}

check() {
  cd "$srcdir/$_pkgname"
  export CARGO_HOME="$srcdir/cargo-home"
  export RUSTUP_TOOLCHAIN=stable
  # Library unit tests only — the integration suites bind real iroh/QUIC
  # endpoints and fail in a sandboxed/offline build environment.
  cargo test --frozen --release --lib
}

package() {
  cd "$srcdir/$_pkgname"

  install -Dm755 "target/release/$_pkgname" "$pkgdir/usr/bin/$_pkgname"
  install -Dm644 "packaging/$_pkgname.desktop" \
    "$pkgdir/usr/share/applications/$_pkgname.desktop"

  # Hicolor icon theme (scalable SVG + the rendered raster sizes).
  install -Dm644 "assets/icons/$_pkgname.svg" \
    "$pkgdir/usr/share/icons/hicolor/scalable/apps/$_pkgname.svg"
  local s
  for s in 16 24 32 48 64 128 256 512; do
    install -Dm644 "assets/icons/$_pkgname-$s.png" \
      "$pkgdir/usr/share/icons/hicolor/${s}x${s}/apps/$_pkgname.png"
  done

  # License + third-party attribution notices.
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$_pkgname/LICENSE"
  install -Dm644 THIRD_PARTY_LICENSES \
    "$pkgdir/usr/share/licenses/$_pkgname/THIRD_PARTY_LICENSES"
}
