# Maintainer: Leonardo Arena <rnalrd@alpinelinux.org>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
pkgname=nextcloud
_pkgver="13.0.0"
pkgver=13.0.1
pkgrel=0
pkgdesc="A safe home for all your data"
url="http://nextcloud.com"
arch="noarch"
license="AGPL-3.0-only"
_php=php7
_php_mods="-ctype -curl -dom -gd -fileinfo -iconv -json -mbstring -openssl
	-pcntl -posix -session -simplexml -xml -xmlreader -xmlwriter -zlib -zip"
depends="ca-certificates $_php ${_php_mods//-/$_php-}"
makedepends="xmlstarlet"
provides="$pkgname-dav=$pkgver-r$pkgrel
	$pkgname-files=$pkgver-r$pkgrel
	$pkgname-provisioning_api=$pkgver-r$pkgrel
	$pkgname-federatedfilesharing=$pkgver-r$pkgrel"
install="$pkgname.pre-install $pkgname.pre-upgrade $pkgname.post-upgrade
	$pkgname-initscript.post-install"
subpackages="$pkgname-doc $pkgname-initscript $pkgname-mysql $pkgname-pgsql $pkgname-sqlite
	$pkgname-default-apps:_default_apps"
# source="https://download.nextcloud.com/server/releases/$pkgname-$pkgver.zip
source="https://download.nextcloud.com/server/prereleases/nextcloud-$_pkgver.zip
	nextcloud10-dont-chmod.patch
	dont-update-htaccess.patch
	disable-integrity-check-as-default.patch
	app-encryption-info-add-mcrypt.patch
	iconv-ascii-translit-not-supported.patch
	use-external-docs-if-local-not-avail.patch

	$pkgname-config.php
	$pkgname.logrotate
	$pkgname.confd
	$pkgname.cron
	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 "files", "dav", and "provisioning_api" here, these should
# be always installed.
_apps="activity
	admin_audit
	comments
	encryption
	federation
	files_external
	files_pdfviewer
	files_sharing
	files_texteditor
	files_trashbin
	files_versions
	files_videoplayer
	firstrunwizard
	gallery
	logreader
	lookup_server_connector
	nextcloud_announcements
	notifications
	oauth2
	password_policy
	serverinfo
	sharebymail
	survey_client
	systemtags
	theming
	twofactor_backupcodes
	user_external
	user_ldap
	workflowengine
	"
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

	# 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() {
	default_doc

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

initscript() {
	pkgdesc="Init script that runs Nextcloud with php-fpm"
	depends="$pkgname $_php-fpm"

	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

	install -m 700 -o nextcloud -d "$subpkgdir"/var/tmp/$pkgname
}

pgsql() {
	pkgdesc="Nextcloud PostgreSQL support"
	depends="$pkgname $_php-pgsql $_php-pdo_pgsql"
	mkdir -p "$subpkgdir"
}

sqlite() {
	pkgdesc="Nextcloud SQLite support"
	depends="$pkgname $_php-sqlite3 $_php-pdo_sqlite"
	mkdir -p "$subpkgdir"
}

mysql() {
	pkgdesc="Nextcloud MySQL support"
	depends="$pkgname $_php-pdo_mysql"
	mkdir -p "$subpkgdir"
}

_default_apps() {
	pkgdesc="Nextcloud default apps"
	depends="$pkgname"

	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"

	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";;

		# XXX: Provides/replaces for backward compatibility with <12.0.0-r2.
		files_pdfviewer | files_texteditor | files_videoplayer)
			provides="$pkgname-${appname#files_}"
			replaces="$provides"
		;;
		user_ldap)
			provides="$pkgname-ldap"
			replaces="$provides"
		;;
	esac

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

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

sha512sums="b1ce432ecbfe6418fcb91554839d04346d83473a905fe776cffd397ecea657555313c6ab7022ba869178a0fd4f0ef1d8928c17ab6adc487dc03032b9cfc9860a  nextcloud-13.0.0.zip
9c20237c4952ff543171768160c17c3608cc43ad9592fecccaeb337787d566f039f27f30f118359dfff7db58cd12d811c05be15e5994efee57a9ed75787d71f5  nextcloud10-dont-chmod.patch
aef3c92497d738d6968e0f0b0d415b4953500db24ae14af41ef972665cf7eff00cb6c53dc953845fdbb389c3c965a75b8b14b9247513c05cf4130fe1cfc61731  dont-update-htaccess.patch
04783385ca35be9e8b895e91294b082cd2a1f50c4e17dc195901885ecd421db9a6fe45360d7eac623873ae4efad80b92f31ebde59f4ed76d832c8cac450dac85  disable-integrity-check-as-default.patch
d6f951cbcb7ca48beb88d77e02a8669677e87bc32189b1eb7e63edc29c293bd6e1ee471c37b622e6b38fd2baf6cf198c872f92b256ffab17c92c31a3a05b5aa0  app-encryption-info-add-mcrypt.patch
9bd1cdb73928d8e746286a8ededf79262835caee8fdb4a18e117535ed95784b5a0b04fad55c720084dd8ae1ed4fe123be55b41d3100c9bb0333af485084bc4b1  iconv-ascii-translit-not-supported.patch
478f5cd7c5d30380ea619d3e8ec623217a06a09b27534266f00297545c7d276b068c5d984673eebc5676e8bac7f45112549498944ce3fa678ac8a69541d7c430  use-external-docs-if-local-not-avail.patch
89c941e2719629ee633421c4c75b55026c1a0a6b255e7f1f2c14612c10325045051b16dc5f17975af4a2a34ec187008de2eb15a53ac4cd06d26a5cbbd79fb73a  nextcloud-config.php
f224d72799ee5819979089eb58978225454223bee597c938681a4f6279eb49297fe9250ac54ccf8bcb33ae262bce43d085affb77723492ee662263710d4008c9  nextcloud.logrotate
35cf156839215113b5d8fb8842b4c1e19a50be3c16be7048879fdd808674e4875dbacf3e2dd884fd182258595b7a137d7d3c2dc602a7ff5613c8b65fae0abe67  nextcloud.confd
edb699ea6127b231793254115b334006c2d50a0d2ecc846188c3521ddffc3c0e19c5e2944f03cae81e6c645c859258380691081b1c522a22d40939b31db36e8a  nextcloud.cron
ebe88e451de5dc84ee8bbcd9af48e2ceae4807546b9c069cf15ecc73e2c408a6e8c7502870f7b45a2e5e0632fc7dec878cad24c1d3a20ff3b812067dd7b07de8  fpm-pool.conf
959852e34f010e635470829d66713f3e22c47717ec2c6487759eed2b6aeff9fd1421fe0271d494a02781bd1c98beb2823583623ee2cf03057cd5db794627d6c2  occ"
