# Maintainer: George Rawlinson <george@rawlinson.net.nz>

pkgname=nextcloud-app-client-push
_pkgname=notify_push
pkgver=0.2.4
pkgrel=1
pkgdesc="Push update support for Nextcloud clients"
arch=('x86_64')
url="https://apps.nextcloud.com/apps/notify_push"
license=('AGPL')
depends=('nextcloud')
makedepends=('rust')
optdepends=('redis: use a local redis server')
source=(
  "$pkgname-$pkgver.tar.gz::https://github.com/nextcloud/notify_push/archive/v$pkgver.tar.gz"
	'pacman.hook'
	'systemd.service'
)
b2sums=('5136f254c51994924f89eb986ae77f0034b51ac9f8c09e5acb17cd9070e3a5f1241e9795d8fde05ce17ee383b56a95c92f84f871d2471ea5f5ec401578421dcf'
        '4ed61b199e995ecd29ef41505e8ce08a7e784c9e08350e2512b6cb6ad3b30d2601052d208e0d2f7d78e025971fa25a3091deb8eed232475bbd7155776ab99400'
        '21f677df863f7d52b1fb5689e1270b1df592d4502d63fab21a922fe29191dc82402909b4c4b45b2f3f41e93965312fc375ec492ca44692dcab7aaa884f7300a9')

prepare() {
	# kludgy workaround to specify target architecture
	sed -i "s/ARCH/$CARCH/" systemd.service

  cd "$_pkgname-$pkgver"
  cargo fetch --locked --target "$CARCH-unknown-linux-gnu"
}

build() {
	cd "$_pkgname-$pkgver"
	cargo build --frozen --release --all-features
}

check() {
	cd "$_pkgname-$pkgver"
	cargo test --frozen --release --all-features
}

package() {
	# systemd integration
	install -vDm644 systemd.service "$pkgdir/usr/lib/systemd/system/$pkgname.service"

	cd "$_pkgname-$pkgver"

	# documentation
	install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" README.md
  install -vDm644 "$srcdir/pacman.hook" \
    "$pkgdir/usr/share/doc/$pkgname/$pkgname.hook"

	# app files
	local _appdir="$pkgdir/usr/share/webapps/nextcloud/apps/$_pkgname"
	install -vd "$_appdir"
	cp -va . "$_appdir"

	# binary
	install -vDm755 -t "$_appdir/bin/$CARCH" "target/release/$_pkgname"

	# remove unnecessary files & folders
	cd "$_appdir"
	rm .dockerignore
	cat .nextcloudignore | xargs echo rm -rf | sh
}