# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Contributor: jahway603 <jahway603@protonmail.com>
# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
_pkgname=nextcloud
pkgver=29.0.7
pkgrel=0
is_latest=true
_pkgvermaj=${pkgver%%.*}
pkgname=nextcloud$_pkgvermaj
_replaced_ver=$(( _pkgvermaj - 1 ))
pkgdesc="A safe home for all your data"
url="https://nextcloud.com/"
arch="noarch"
license="AGPL-3.0-only"
_php=php83
_php_mods="-bcmath -ctype -curl -dom -gd -fileinfo -gmp -iconv -intl
	-mbstring -opcache -openssl -pcntl -posix -session
	-simplexml -xml -xmlreader -xmlwriter -zip"
depends="ca-certificates $_php ${_php_mods//-/$_php-}"
makedepends="xmlstarlet"
$is_latest && provides="$_pkgname=$pkgver-r$pkgrel
	$_pkgname-accessibility=$pkgver-r$pkgrel
	$pkgname-accessibility=$pkgver-r$pkgrel
	$_pkgname-bruteforcesettings=$pkgver-r$pkgrel
	$pkgname-bruteforcesettings=$pkgver-r$pkgrel
	$_pkgname-contactsinteraction=$pkgver-r$pkgrel
	$pkgname-contactsinteraction=$pkgver-r$pkgrel
	$_pkgname-cloud_federation_api=$pkgver-r$pkgrel
	$pkgname-cloud_federation_api=$pkgver-r$pkgrel
	$_pkgname-dav=$pkgver-r$pkgrel
	$pkgname-dav=$pkgver-r$pkgrel
	$_pkgname-files=$pkgver-r$pkgrel
	$pkgname-files=$pkgver-r$pkgrel
	$_pkgname-files_videoplayer=$pkgver-r$pkgrel
	$pkgname-files_videoplayer=$pkgver-r$pkgrel
	$_pkgname-federatedfilesharing=$pkgver-r$pkgrel
	$pkgname-federatedfilesharing=$pkgver-r$pkgrel
	$_pkgname-lookup_server_connector=$pkgver-r$pkgrel
	$pkgname-lookup_server_connector=$pkgver-r$pkgrel
	$_pkgname-oauth2=$pkgver-r$pkgrel
	$pkgname-oauth2=$pkgver-r$pkgrel
	$_pkgname-provisioning_api=$pkgver-r$pkgrel
	$pkgname-provisioning_api=$pkgver-r$pkgrel
	$_pkgname-related_resources=$pkgver-r$pkgrel
	$pkgname-related_resources=$pkgver-r$pkgrel
	$_pkgname-settings=$pkgver-r$pkgrel
	$pkgname-settings=$pkgver-r$pkgrel
	$_pkgname-theming=$pkgver-r$pkgrel
	$pkgname-theming=$pkgver-r$pkgrel
	$_pkgname-twofactor_totp=$pkgver-r$pkgrel
	$pkgname-twofactor_totp=$pkgver-r$pkgrel
	$_pkgname-twofactor_backupcodes=$pkgver-r$pkgrel
	$pkgname-twofactor_backupcodes=$pkgver-r$pkgrel
	$_pkgname-viewer=$pkgver-r$pkgrel
	$pkgname-viewer=$pkgver-r$pkgrel
	$_pkgname-workflowengine=$pkgver-r$pkgrel
	$pkgname-workflowengine=$pkgver-r$pkgrel
	" || provides="$pkgname-accessibility=$pkgver-r$pkgrel
	$pkgname-bruteforcesettings=$pkgver-r$pkgrel
	$pkgname-contactsinteraction=$pkgver-r$pkgrel
	$pkgname-cloud_federation_api=$pkgver-r$pkgrel
	$pkgname-dav=$pkgver-r$pkgrel
	$pkgname-files=$pkgver-r$pkgrel
	$pkgname-files_videoplayer=$pkgver-r$pkgrel
	$pkgname-federatedfilesharing=$pkgver-r$pkgrel
	$pkgname-lookup_server_connector=$pkgver-r$pkgrel
	$pkgname-oauth2=$pkgver-r$pkgrel
	$pkgname-provisioning_api=$pkgver-r$pkgrel
	$pkgname-related_resources=$pkgver-r$pkgrel
	$pkgname-settings=$pkgver-r$pkgrel
	$pkgname-theming=$pkgver-r$pkgrel
	$pkgname-twofactor_totp=$pkgver-r$pkgrel
	$pkgname-twofactor_backupcodes=$pkgver-r$pkgrel
	$pkgname-viewer=$pkgver-r$pkgrel
	$pkgname-workflowengine=$pkgver-r$pkgrel
	"
replaces="nextcloud$_replaced_ver"
install="$pkgname.pre-install $pkgname.pre-upgrade $pkgname.post-upgrade $pkgname.post-install
	$pkgname-initscript.post-install"
subpackages="$pkgname-doc $pkgname-initscript $pkgname-mysql $pkgname-pgsql $pkgname-sqlite
	$pkgname-default-apps:_default_apps $pkgname-occ"
source="https://download.nextcloud.com/server/releases/nextcloud-$pkgver.tar.bz2
	nextcloud-dont-chmod.patch
	dont-update-htaccess.patch
	disable-integrity-check-as-default.patch
	use-external-docs-if-local-not-avail.patch

	$_pkgname-config.php
	$_pkgname.logrotate
	$_pkgname.confd
	$_pkgname.cron
	$_pkgname-mysql.cnf
	fpm-pool.conf
	occ
	"
options="!check"
pkgusers="nextcloud"
pkggroups="www-data"
builddir="$srcdir"/$_pkgname

# List of bundled apps to separate into subpackages. Keep it in sync!
# Note: Don't add "bruteforcesettings", "contactsinteraction",
# "cloud_federation_api", "dav", "files",
# "federatedfilesharing", "lookup_server_connector", "provisioning_api",
# "oauth2", "settings", "twofactor_backupcodes", "twofactor_totp",
# "theming", "viewer", "workflowengine", "related_resources"
# here, these should be always installed.
_apps="activity
	admin_audit
	circles
	comments
	dashboard
	encryption
	federation
	files_external
	files_pdfviewer
	files_reminders
	files_sharing
	files_trashbin
	files_versions
	firstrunwizard
	logreader
	nextcloud_announcements
	notifications
	password_policy
	photos
	privacy
	recommendations
	serverinfo
	support
	sharebymail
	survey_client
	suspicious_login
	systemtags
	text
	user_ldap
	user_status
	weather_status
	"
for _i in $_apps; do
	subpackages="$subpackages $pkgname-$_i:_package_app"
done

# Directory for apps shipped with Nextcloud.
_appsdir="usr/share/webapps/$_pkgname/apps"

package() {
	local basedir="var/lib/$_pkgname"
	local datadir="$basedir/data"
	local wwwdir="usr/share/webapps/$_pkgname"
	local confdir="etc/$_pkgname"

	mkdir -p "$pkgdir"
	cd "$pkgdir"

	mkdir -p ./${wwwdir%/*}
	cp -a "$builddir" ./$wwwdir

	chmod +x ./$wwwdir/occ
	chmod 664 ./$wwwdir/.htaccess \
		./$wwwdir/.user.ini

	# Let's not ship upstream's 'updatenotification' app and updater, which
	# has zero chance of working and a big chance of blowing things up.
	rm -r ./$wwwdir/apps/updatenotification \
		./$wwwdir/lib/private/Updater/VersionCheck.php

	# Replace bundled CA bundle with ours.
	ln -sf /etc/ssl/certs/ca-certificates.crt ./$wwwdir/resources/config/ca-bundle.crt

	install -d -m 770 -o nextcloud -g www-data \
		./$confdir ./$datadir ./$basedir/apps
	install -d -m 775 -o nextcloud -g www-data \
		./var/log/$_pkgname

	# Create symlink from web root to site-apps, so web server can find
	# assets w/o explicit configuration for this layout.
	ln -s /$basedir/apps ./$wwwdir/apps-appstore

	mv ./$wwwdir/config/* ./$confdir/
	rm -r ./$wwwdir/config
	ln -s /$confdir ./$wwwdir/config

	mkdir -p ./usr/share/doc/$pkgname
	mv ./$wwwdir/core/doc ./usr/share/doc/$pkgname/core

	install -m 660 -o nextcloud -g www-data \
		"$srcdir"/$_pkgname-config.php ./$confdir/config.php

	install -m 644 -D "$srcdir"/$_pkgname.logrotate ./etc/logrotate.d/$_pkgname

	install -m 755 -D "$srcdir"/occ ./usr/bin/occ

	# Clean some unnecessary files.
	find . -name .gitignore -delete \
		-o -name .bower.json -delete \
		-o -name 'README*' -delete \
		-o -name 'CHANGELOG*' -delete \
		-o -name 'CONTRIBUTING*' -delete
	find . -name .github -type d -prune -exec rm -r {} \;
}

doc() {
	replaces="nextcloud$_replaced_ver-doc"
	$is_latest && provides="$_pkgname-doc=$pkgver-r$pkgrel"
	default_doc

	local target="$subpkgdir"/usr/share/webapps/$_pkgname/core/doc
	mkdir -p "${target%/*}"
	ln -s ../../../doc/$pkgname/core "$target"
	install -m644 README.alpine "$subpkgdir"/usr/share/webapps/$_pkgname/README.alpine
}

initscript() {
	pkgdesc="Init script that runs Nextcloud with php-fpm"
	depends="$pkgname $_php-fpm"
	replaces="nextcloud$_replaced_ver-initscript"
	$is_latest && provides="$_pkgname-initscript=$pkgver-r$pkgrel"

	local confdir="$subpkgdir/etc/$_php/php-fpm.d"
	local fpm_name="php-fpm${_php#php}"

	install -m 644 -D "$srcdir"/fpm-pool.conf "$confdir"/$_pkgname.conf
	install -m 644 -D "$srcdir"/$_pkgname.confd "$subpkgdir"/etc/conf.d/$_pkgname
	install -m 755 -D "$srcdir"/$_pkgname.cron "$subpkgdir"/etc/periodic/15min/$_pkgname

	mkdir -p "$subpkgdir"/etc/init.d
	ln -s $fpm_name "$subpkgdir"/etc/init.d/$_pkgname
}

pgsql() {
	pkgdesc="Nextcloud PostgreSQL support"
	depends="$pkgname $_php-pgsql $_php-pdo_pgsql"
	replaces="nextcloud$_replaced_ver-pgsql"
	$is_latest && provides="$_pkgname-pgsql=$pkgver-r$pkgrel"
	mkdir -p "$subpkgdir"
}

sqlite() {
	pkgdesc="Nextcloud SQLite support"
	depends="$pkgname $_php-sqlite3 $_php-pdo_sqlite"
	replaces="nextcloud$_replaced_ver-sqlite"
	$is_latest && provides="$_pkgname-sqlite=$pkgver-r$pkgrel"
	mkdir -p "$subpkgdir"
}

mysql() {
	pkgdesc="Nextcloud MySQL support"
	depends="$pkgname $_php-pdo_mysql"
	replaces="nextcloud$_replaced_ver-mysql"
	$is_latest && provides="$_pkgname-mysql=$pkgver-r$pkgrel"

	mkdir -p "$subpkgdir"
	install -m 644 -D "$srcdir"/$_pkgname-mysql.cnf "$subpkgdir"/etc/my.cnf.d/$_pkgname.cnf
}

occ() {
       pkgdesc="Nextcloud OCC cmd"
       replaces="nextcloud$_replaced_ver-occ"
       $is_latest && provides="$_pkgname-occ=$pkgver-r$pkgrel"
       mkdir -p "$subpkgdir/usr/share/webapps/$_pkgname"
       amove "usr/share/webapps/$_pkgname/occ"
       amove "usr/bin/occ"
}

_default_apps() {
	pkgdesc="Nextcloud default apps"
	depends="$pkgname"
	replaces="nextcloud$_replaced_ver-default-apps"
	$is_latest && provides="$_pkgname-default-apps=$pkgver-r$pkgrel"

	local path; for path in "$pkgdir"/"$_appsdir"/*; do
		if grep -q '<default_enable\s*/>' "$path"/appinfo/info.xml; then
			depends="$depends $pkgname-${path##*/}"
		fi
	done

	mkdir -p "$subpkgdir"
}

_package_app() {
	local appname="${subpkgname#"$pkgname"-}"
	local appinfo="$pkgdir/$_appsdir/$appname/appinfo/info.xml"

	local name=$(xmlstarlet sel -t -v 'info/name/text()' "$appinfo")
	pkgdesc="Nextcloud ${name:-$appname} app"
	replaces="nextcloud$_replaced_ver-$appname"
	$is_latest && provides="$_pkgname-$appname=$pkgver-r$pkgrel"

	local php_deps=$(xmlstarlet sel -t -v 'info/dependencies/lib/text()' "$appinfo" \
		| xargs -r -n1 printf "$_php-%s\n")
	local app_deps=""

	case "$appname" in
		files_sharing)	app_deps="-federatedfilesharing"
		;;
		serverinfo) app_deps="-files_sharing"
	esac

	depends="$pkgname $php_deps ${app_deps//-/$pkgname-}"

	mkdir -p "$subpkgdir"/$_appsdir
	mv "$pkgdir"/$_appsdir/$appname "$subpkgdir"/$_appsdir/
}

sha512sums="
9a87ea11870aec4b26499e85c9ef91e38644d947c810c619f22f617307014b58adc24ebd19841af8aa3b5185427610f278951e48bb7ae242fa4de1eb3e6abb63  nextcloud-29.0.7.tar.bz2
aea0adb2c3a48ec6af2958c6ccfe13adff86316a56084e763b7e6df9e21aa3435b13305b7c15cc2b795e83c9388b05006862f6465c29e3dc2c1fbd8eb8befcb9  nextcloud-dont-chmod.patch
12f4a39aef0f81a0115c81bf2b345cc194537a7e8300748b800b0e35bc07928091296074b23c2019c17aced69854a11d1ed7225f67eefd27cf00c3969a75c5b0  dont-update-htaccess.patch
cb04252d01407c7030e87dd54616c621ea0f85ef0212674b1161288182538cae0fb31c67e7cc07c66f9607075774c64e386009cc66365b1f1b155f6ad4f83ac0  disable-integrity-check-as-default.patch
c0a9b7c31c8beaca711f8e97d98441007b3dca7fb3d316d2eacd28a73b5233def6f846c02d98202f75efb9cb248b8787a80e20b07c32d1c7534a0e54bb20feab  use-external-docs-if-local-not-avail.patch
5f73cd9399fa484ef15bd47e803c93381deffbc7699eceadbb5c27e43b20156806d74e5021a64d28f0165ef87b519e962780651711a37bceb9f0b04455dfdce1  nextcloud-config.php
7388458a9e8b7afd3d3269718306410ffa59c3c23da4bef367a4d7f6d2570136fae9dd421b19c1441e7ffb15a5405e18bb5da67b1a15f9f45e8b98d3fda532ba  nextcloud.logrotate
dcc57735d7d4af4a7ebbdd1186d301e51d2ae4675022aea6bf1111222dfa188a3a490ebd6e7c8a7ac30046cb7d93f81cec72a51acbc60d0c10b7fb64630c637a  nextcloud.confd
06a62deae219d09df7acbf38ccb1dcac691dd882459ef70243b5583d7ed21d1ea81dbf0751b4e7199c0de9878755a3882e139d9ccc280bf2e90cbe33fb565487  nextcloud.cron
b9ad5434c384c96608f00d65c45f782e279c6756da8fb706f62ecaf7b7aa420077cb6989da5e85becc47418884ec0672e7db874174454ca679fdca84a50f537f  nextcloud-mysql.cnf
78ef204ee7c12b228c0b7b04333514e561c1c8e19153f5507224fa4fdd112aaaa6331747014f3b72181298f52ecd4223bcff4bd963b49b49153265254b07e79b  fpm-pool.conf
be54ad9308c8250ff3aef3514b10b228487fc2fbdefa1d28dbbb18a4770f7d9fda90e80c722de8e3c25ce752d124ff79314f16f783b1e5ad67df4e1fe6e880f9  occ
"
