# Maintainer: Noel Kuntze <noel.kuntze@contauro.com>
pkgname=grommunio-sync
pkgver=2.0_git20240327
pkgrel=3
pkgdesc="Exchange ActiveSync interface for grommunio"
# s390x: blocked by grommunio-gromox
arch="noarch !riscv64 !s390x"
url="https://grommunio.com/"
license="AGPL-3.0-only"
options="!check" # No test suite
_php=php83
install="$pkgname.pre-install $pkgname.pre-upgrade"
depends="grommunio-gromox
	grommunio-mapi-header-php
	$_php
	$_php-curl
	$_php-gettext
	$_php-iconv
	$_php-json
	$_php-mbstring
	$_php-openssl
	$_php-pecl-redis
	$_php-pdo_sqlite
	$_php-posix
	$_php-session
	$_php-simplexml
	$_php-soap"

_commithash=655dec920595f2e181a0ad2c415d1fe2f5b04c9c

source="$pkgname-$pkgver.tar.gz::https://github.com/grommunio/grommunio-sync/archive/$_commithash.tar.gz
	$pkgname-$pkgver-vendor.tar.gz::https://nerd.hu/distfiles/grommunio-sync-$_commithash-vendor.tar.gz
	"

builddir="$srcdir/grommunio-sync-$_commithash/"

build() {
	:
}

package() {
	# webapp
	install -dm755 "$pkgdir"/usr/share/webapps/$pkgname/
	cp -rp -- * "$pkgdir"/usr/share/webapps/$pkgname/

	# vendor libraries
	cp -rp "$srcdir/vendor" "$pkgdir"/usr/share/webapps/$pkgname/

	# license
	install -Dm644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE

	# nginx conf
	sed -i "s@/var/log/grommunio/@/var/log/nginx/@" build/grommunio-sync.conf
	sed -i "s@/usr/share/grommunio-sync/@/usr/share/webapps/grommunio-sync/@" build/grommunio-sync.conf
	install -Dpm644 build/grommunio-sync.conf "$pkgdir"/usr/share/grommunio-common/nginx/locations.d/grommunio-sync.conf
	sed -i "s@/php-fpm/@/php-fpm${_php#php}/@" build/grommunio-sync-upstream.conf
	install -Dpm644 build/grommunio-sync-upstream.conf "$pkgdir"/usr/share/grommunio-common/nginx/upstreams.d/grommunio-sync.conf

	# php-fpm
	sed -i "s@/php-fpm/@/php-fpm${_php#php}/@" build/pool-grommunio-sync.conf
	install -Dpm644 build/pool-grommunio-sync.conf "$pkgdir"/etc/$_php/php-fpm.d/pool-grommunio-sync.conf

	# logrotate
	install -Dm644 build/grommunio-sync.lr "$pkgdir"/etc/logrotate.d/grommunio-sync

	# web config
	install -Dm644 config.php "$pkgdir"/etc/grommunio-sync/config.php
	ln -sf "/etc/grommunio-sync/config.php" "$pkgdir"/usr/share/webapps/grommunio-sync/config.php

	# remove unnecessary files
	rm -r "$pkgdir"/usr/share/webapps/grommunio-sync/build
	rm "$pkgdir"/usr/share/webapps/grommunio-sync/LICENSE
}

sha512sums="
e00414e470b61c0bfe6dde54de203c45ae409f5028d945371817430fdbdf203a4230e8a89f06ad52b7f892fea395651965d01bf20edb5786c02bfc2625e7bf51  grommunio-sync-2.0_git20240327.tar.gz
d49c39cdea8ca46bc7c41f0d044ab6b015c24a0bf15da61c2b11460b05845f065af4f352fd0a0a70739113ec1cb196c47f477ddb776f24ceb37d6ffa638a288d  grommunio-sync-2.0_git20240327-vendor.tar.gz
"
