# Maintainer: mollusk <jitty+lc1iz0dc@protonmail.com>
#
# Test-pack split package: ONE `makepkg -si` builds + installs BOTH peerspeak
# (voice chat) and pixelpass (screen sharing) from the public gitbutter repos
# over https. pixelpass lands on /usr/bin so peerspeak's screen-share button
# finds it. Shared version string is derived from peerspeak's git.
#
# Clone this repo and build from here:
#   git clone https://gitbutter.xyz/mollusk/peerspeak.git
#   cd peerspeak/packaging/test-pack
#   makepkg -si
pkgbase=peerspeak-git
pkgname=('peerspeak-git' 'pixelpass')
pkgver=0.1.0
pkgrel=1
arch=('x86_64')
url="https://gitbutter.xyz/mollusk/peerspeak"
license=('custom' 'MIT' 'Apache-2.0' 'OFL-1.1')
makedepends=('git' 'cargo' 'pkgconf')
options=('!lto' '!debug')
source=("peerspeak::git+https://gitbutter.xyz/mollusk/peerspeak.git"
        "pixelpass::git+https://gitbutter.xyz/mollusk/pixelpass.git#branch=main")
sha256sums=('SKIP'
            'SKIP')

pkgver() {
  cd "$srcdir/peerspeak"
  # Shared across both split packages. 0.1.0.r<commits>.g<short-sha>.
  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() {
  # Vendor deps up front so build() can run --frozen (no surprise network).
  export CARGO_HOME="$srcdir/cargo-home"
  local host; host="$(rustc -vV | sed -n 's/host: //p')"
  cd "$srcdir/peerspeak";  cargo fetch --locked --target "$host"
  cd "$srcdir/pixelpass";  cargo fetch --locked --target "$host"
}

build() {
  export CARGO_HOME="$srcdir/cargo-home"
  export RUSTUP_TOOLCHAIN=stable
  export CARGO_TARGET_DIR=target

  cd "$srcdir/peerspeak"
  cargo build --frozen --release --bin peerspeak

  cd "$srcdir/pixelpass"
  # --features gui so the .desktop launcher (pixelpass --gui) works.
  cargo build --frozen --release --features gui
}

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

package_peerspeak-git() {
  pkgdesc="Decentralized peer-to-peer voice chat (Rust/iroh/PipeWire/Opus/iced)"
  depends=('pipewire' 'opus')
  optdepends=('pixelpass: screen sharing inside a room'
              'mpv: screen-share viewer (vlc is used as a fallback)')
  provides=('peerspeak')
  conflicts=('peerspeak')
  license=('custom')

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

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

package_pixelpass() {
  pkgdesc='P2P screen sharing over iroh — no port forwarding, no signup'
  depends=('gstreamer' 'gst-plugins-base' 'gst-plugins-good' 'gst-plugins-bad'
           'gst-libav' 'gst-plugin-va' 'libpulse' 'hicolor-icon-theme'
           'libglvnd' 'libxkbcommon' 'wayland')
  optdepends=('mpv: recommended stream viewer (the GUI launches mpv)'
              'vlc: alternative stream viewer'
              'gst-plugins-ugly: software x264 encoding for `pixelpass --no-hwencode`'
              'gst-plugin-pipewire: screen capture on Wayland sessions'
              'xorg-xwininfo: share a single window on X11 (`pixelpass --window`)')
  license=('MIT' 'Apache-2.0' 'OFL-1.1')

  cd "$srcdir/pixelpass"
  install -Dm0755 "target/release/pixelpass" "$pkgdir/usr/bin/pixelpass"
  install -Dm0644 assets/pixelpass.desktop \
    "$pkgdir/usr/share/applications/pixelpass.desktop"
  install -Dm0644 assets/pixelpass.svg \
    "$pkgdir/usr/share/icons/hicolor/scalable/apps/pixelpass.svg"
  install -Dm0644 README.md "$pkgdir/usr/share/doc/pixelpass/README.md"
  install -Dm0644 LICENSE-MIT "$pkgdir/usr/share/licenses/pixelpass/LICENSE-MIT"
  install -Dm0644 LICENSE-APACHE "$pkgdir/usr/share/licenses/pixelpass/LICENSE-APACHE"
  install -Dm0644 assets/NotoSans-OFL.txt \
    "$pkgdir/usr/share/licenses/pixelpass/NotoSans-OFL.txt"
}
