# Contributor: Will Sinatra <wpsinatra@gmail.com>
# Maintainer: Will Sinatra <wpsinatra@gmail.com>
pkgname=grocy
pkgver=4.2.0
pkgrel=1
pkgdesc="web-based self-hosted groceries & household management solution for your home"
url="https://grocy.info/"
license="MIT"
arch="noarch"
_php=php83
makedepends="composer yarn $_php-dev $_php-sqlite3 $_php-gd $_php-intl"
depends="$_php
	$_php-fpm
	$_php-ctype
	$_php-fileinfo
	$_php-gd
	$_php-iconv
	$_php-intl
	$_php-mbstring
	$_php-pdo_sqlite
	$_php-simplexml
	$_php-sqlite3
	$_php-tokenizer
	sqlite
	"
source="$pkgname-$pkgver.zip::https://github.com/grocy/grocy/releases/download/v$pkgver/grocy_$pkgver.zip
	$pkgname-$pkgver.tar.gz::https://github.com/grocy/grocy/archive/refs/tags/v$pkgver.tar.gz
	grocy_nginx.conf
	grocy_nginx_fpm.conf"
options="!check" #no checks defined
subpackages="$pkgname-nginx:_nginx"

unpack() {
	#Web application is pre-packaged
	#Default unpack dumps this directly into the srcdir
	unzip $srcdir/$pkgname-$pkgver.zip -d $srcdir/$pkgname-$pkgver

	#But the web application lacks yarn & composer files to build with
	#so we pull and extract both
	mkdir $srcdir/source
	tar -xzvf $srcdir/$pkgname-$pkgver.tar.gz -C $srcdir/source
}

prepare() {
	default_prepare

	#Pull yarn & composer files from tarball, and insert into webapp directory
	for f in yarn.lock composer.lock composer.json composer.lock package.json; do
		cp $srcdir/source/$pkgname-$pkgver/$f $srcdir/$pkgname-$pkgver/
	done
}

build() {
	$_php /usr/bin/composer.phar dump-autoload --no-cache --no-interaction --no-dev --optimize

	yarn install --modules-folder public/node_modules --production
	yarn cache clean
}

package() {
	_instdir="$pkgdir"/usr/share/webapps/grocy
	mkdir -p "$_instdir" "$pkgdir"/etc/webapps/grocy "$pkgdir"/var/lib/webapps

	cp -r "$builddir"/* "$_instdir"
	mv "$pkgdir"/usr/share/webapps/grocy/data "$pkgdir"/var/lib/webapps/grocy

	ln -s /var/lib/webapps/grocy "$pkgdir"/usr/share/webapps/grocy/data
	ln -s /etc/webapps/grocy/config.php "$pkgdir"/var/lib/webapps/grocy/config.php

	mv "$builddir"/config-dist.php "$pkgdir"/etc/webapps/grocy/config.php

	chown -R root:www-data "$pkgdir"/usr/share/webapps/grocy
	chown -R root:www-data "$pkgdir"/var/lib/webapps/grocy
	chmod -R 0775 "$pkgdir"/var/lib/webapps/grocy
}

_nginx() {
	pkgdesc="Nginx configuration for Grocy"
	depends="nginx !grocy-lighttpd"
	install="$subpkgname".pre-install

	install -d "$subpkgdir"/etc/nginx/sites-available
	install -d "$subpkgdir"/etc/$_php/conf-available
	install -Dm0644 "$srcdir"/grocy_nginx.conf -t "$subpkgdir"/etc/nginx/sites-available
	install -Dm0644 "$srcdir"/grocy_nginx_fpm.conf -t "$subpkgdir"/etc/$_php/conf-available
}

sha512sums="
19da86b0587855d04b56a1e0421b4f8fc22623e1f29827372e48041d8b17be7dc9fc238c6e9430e29224c5e7692358d95fbd1bf04435143ec002968d42446d83  grocy-4.2.0.zip
774ad8e0321ff95c8d0acd175ce56b0d669ba84f7268dbfbbe1e6727b2aab099b4742e2f70ae7914fbf942c6e1379f1406d6f9d808786c9dc5556f006d363e28  grocy-4.2.0.tar.gz
20f698a7b634ef6390f31275f2c0f8ca645e626521cb252c5c248c33616bd744ec0270f62bd7ffb3b56220dc37829ec8cc2692789ea1efffad8ba098e4c5caae  grocy_nginx.conf
707cb5c9837d75f6506b390207e544456d75fc2851791060261286d4cfa9534a785b84f26769d8d706e045a0f0cd1a0f3a30dcd3468b7c77ab237492580dc151  grocy_nginx_fpm.conf
"
