# Maintainer: KVT ICT <hephaestus@noreply.kvt.nl>
# AUR package kvt-rusty-teams: the KVT build of Rusty Teams, built from source at a release tag
# of git.kvt.nl/ict/rusty-teams. Next to kvt-rusty-teams-bin (prebuilt tarball from the release).
# Bump _tag, _commit and pkgver together for a new release (see packaging/aur-source/README.md).
pkgname=kvt-rusty-teams
pkgver=0.6.1.kvt.4
pkgrel=1
_tag=v0.6.1-kvt.4
_commit=e972869751c189818d20930bebdb6a0156b16db1
pkgdesc='Native Microsoft Teams client in Rust (KVT build, built from source)'
arch=('x86_64')
url='https://git.kvt.nl/ict/rusty-teams'
license=('MIT')
depends=('glibc' 'gcc-libs' 'fontconfig' 'freetype2' 'libxkbcommon' 'libxkbcommon-x11'
         'libx11' 'libxcb' 'wayland' 'alsa-lib' 'libpulse' 'libpipewire' 'libva' 'glib2' 'hicolor-icon-theme')
makedepends=('cargo' 'clang' 'mold' 'git' 'pkgconf' 'python'
             'libxext' 'libxfixes' 'libxdamage' 'libxrandr' 'libxcomposite')
options=('!lto' '!debug')
provides=('rusty-teams')
conflicts=('rusty-teams' 'rusty-teams-bin' 'rusty-teams-git' 'kvt-rusty-teams-bin')
# The build downloads the prebuilt libwebrtc (webrtc-sys); cargo itself runs offline after prepare().
source=("rusty-teams::git+https://git.kvt.nl/ict/rusty-teams.git#tag=${_tag}")
sha256sums=('SKIP')

prepare() {
  cd rusty-teams
  # The tag must still point at the commit this PKGBUILD was written for.
  [[ "$(git rev-parse HEAD)" == "$_commit" ]] || { echo "tag $_tag is not commit $_commit" >&2; return 1; }
  export RUSTUP_TOOLCHAIN=stable
  cargo fetch --locked --target x86_64-unknown-linux-gnu
}

build() {
  cd rusty-teams
  export RUSTUP_TOOLCHAIN=stable CARGO_TARGET_DIR=target
  # .cargo/config.toml links with clang and mold; webrtc-sys needs clang 21 or newer.
  export CC=clang CXX=clang++
  # makepkg's -O2/-flto flags land after the -O0 that aws-lc-sys's jitterentropy needs, and the
  # vendored C and C++ code (libwebrtc glue, aws-lc) picks its own flags.
  unset CFLAGS CXXFLAGS CPPFLAGS LDFLAGS
  # A tagged build: the app shows the tag as its version.
  export GITHUB_REF_NAME="$_tag" GITHUB_REF_TYPE=tag
  # pacman owns the install: the app never looks for updates (see also the marker file below).
  export RUSTY_TEAMS_PACKAGE_MANAGED=1
  cargo build -p app --release --frozen
}

package() {
  cd rusty-teams
  local id=rusty-teams component=io.github.danieldaeschle.RustyTeams
  install -Dm755 target/release/teams "$pkgdir/usr/bin/teams"
  install -Dm644 packaging/linux/$id.desktop "$pkgdir/usr/share/applications/$id.desktop"
  install -d "$pkgdir/usr/share/icons"
  cp -a packaging/linux/hicolor "$pkgdir/usr/share/icons/"
  sed -e "s/@VERSION@/${_tag#v}/g" -e "s/@DATE@/$(git log -1 --format=%cs)/g" \
    packaging/linux/$id.metainfo.xml.in > "$srcdir/$component.metainfo.xml"
  install -Dm644 "$srcdir/$component.metainfo.xml" "$pkgdir/usr/share/metainfo/$component.metainfo.xml"
  install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
  # The app never looks for updates when this file exists: pacman updates it.
  install -Dm644 /dev/null "$pkgdir/usr/lib/rusty-teams/package-managed"
}
