# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=baculum
pkgver=13.0.3
pkgrel=1
pkgdesc="API layer to Baculum WebGUI tool for Bacula Community program"
url="https://bacula.org/"
arch="noarch"
license="AGPL-3.0-or-later"
_php=php82
_common_depends="
	$_php-common
	$_php-ctype
	$_php-curl
	$_php-dom
	$_php-json
	$_php-session
	$_php-xml
	"
_api_depends="
	$_php-bcmath
	$_php-mysqlnd
	$_php-pdo
	$_php-pgsql
	"
_web_depends="
	$_php-mbstring
	"
_apache_depends="apache2 $_php-apache2"
_lighttpd_depends="lighttpd lighttpd-mod_auth $_php-cgi"
makedepends="
	coreutils
	$_common_depends
	$_apache_depends
	$_api_depends
	$_lighttpd_depends
	$_web_depends
	"
pkggroups="www-data"
subpackages="$pkgname-common:_common
	$pkgname-api:_api
	$pkgname-web:_web
	$pkgname-api-apache2:_api_apache2
	$pkgname-api-lighttpd:_api_lighttpd
	$pkgname-web-apache2:_web_apache2
	$pkgname-web-lighttpd:_web_lighttpd
	"
source="https://downloads.sourceforge.net/project/bacula/bacula/$pkgver/bacula-gui-$pkgver.tar.gz
	 fix-locale-symlinks.patch
	 shared-config-dir.patch
	fix-php8.patch
	 "
builddir="$srcdir/"bacula-gui-$pkgver/baculum
options="!check"

package() {
	mkdir -p "$pkgdir"/etc/baculum
	make -j1 DESTDIR="$pkgdir" HTTPDNAME=apache2
	# will be symlinks
	rmdir "$pkgdir"/usr/share/baculum/htdocs/assets \
		"$pkgdir"/usr/share/baculum/htdocs/protected/runtime \
		"$pkgdir"/var/cache/baculum

	chgrp -R www-data "$pkgdir"/etc/baculum/Config-*
	chmod g+w "$pkgdir"/etc/baculum/Config-*
	chmod g+rw "$pkgdir"/etc/baculum/Config-*/baculum.users

	rm -rf "$pkgdir"/usr/lib/systemd
	rm "$pkgdir"/baculum-install-checker.sh
}

_common() {
	pkgdesc="Common libraries for Baculum"
	depends="$_common_depends"
	amove usr/share/baculum/htdocs/protected/Common \
		usr/share/baculum/htdocs/protected/application.xml \
		usr/share/baculum/htdocs/protected/autoload.php \
		usr/share/baculum/htdocs/themes \
		usr/share/baculum/htdocs/LICENSE \
		usr/share/baculum/htdocs/AUTHORS \
		usr/share/baculum/htdocs/README \
		usr/share/baculum/htdocs/INSTALL \
		usr/share/baculum/htdocs/index.php
	install -d -m775 -g www-data "$subpkgdir"/var/cache/baculum \
		"$subpkgdir"/var/log/baculum
	ln --relative -s "$subpkgdir"/var/cache/baculum \
		"$subpkgdir"/usr/share/baculum/htdocs/assets
	ln --relative -s "$subpkgdir"/var/cache/baculum \
		"$subpkgdir"/usr/share/baculum/htdocs/protected/runtime
	ls -la "$subpkgdir"/var/cache/baculum
}

_api() {
	pkgdesc="Baculum API files"
	depends="$pkgname-common=$pkgver-r$pkgrel $_api_depends"
	amove etc/baculum/Config-api \
		usr/share/baculum/htdocs/protected/API \
		usr/share/locale/*/LC_MESSAGES/baculum-api.mo

	# will be symlinks
	rm -r "$subpkgdir"/usr/share/baculum/htdocs/protected/API/Config \
		"$subpkgdir"/usr/share/baculum/htdocs/protected/API/Logs
	ln --relative -s "$subpkgdir"/etc/baculum/Config-api \
		"$subpkgdir"/usr/share/baculum/htdocs/protected/API/Config
	ln --relative -s "$subpkgdir"/var/cache/baculum \
		"$subpkgdir"/usr/share/baculum/htdocs/protected/API/Logs
}

_web() {
	pkgdesc="Baculum Web files"
	depends="$pkgname-common=$pkgver-r$pkgrel $_web_depends"
	amove etc/baculum/Config-web \
		usr/share/baculum/htdocs/protected/Web \
		usr/share/locale/*/LC_MESSAGES/baculum-web.mo

	# will be symlinks
	rm -r "$subpkgdir"/usr/share/baculum/htdocs/protected/Web/Config \
		"$subpkgdir"/usr/share/baculum/htdocs/protected/Web/Logs
	ln --relative -s "$subpkgdir"/var/cache/baculum \
		"$subpkgdir"/usr/share/baculum/htdocs/protected/Web/Logs
	ln --relative -s "$subpkgdir"/etc/baculum/Config-web \
		"$subpkgdir"/usr/share/baculum/htdocs/protected/Web/Config
}

_api_apache2() {
	pkgdesc="Apache configuration for Baculum API"
	depends="$pkgname-api=$pkgver-r$pkgrel $_apache_depends"
	amove etc/apache2/conf.d/baculum-api.conf
}

_api_lighttpd() {
	pkgdesc="Lighttpd configuration for Baculum API"
	depends="$pkgname-api=$pkgver-r$pkgrel $_lighttpd_depends"
	amove etc/lighttpd/baculum-api.conf
}

_web_apache2() {
	pkgdesc="Apache configuration for Baculum WebGUI"
	depends="$pkgname-web=$pkgver-r$pkgrel $_apache_depends"
	amove etc/apache2/conf.d/baculum-web.conf
}

_web_lighttpd() {
	pkgdesc="Lighttpd configuration for Baculum WebGUI"
	depends="$pkgname-web=$pkgver-r$pkgrel $_lighttpd_depends"
	amove etc/lighttpd/baculum-web.conf
}

sha512sums="
886bb2b9b0bfb9ed665ef12523eac13385111ae776726f40da47726bc4fee88391daffba8385591b82455af1b1fbbeec879b2c82b625da666b07abe9880d3f53  bacula-gui-13.0.3.tar.gz
88e2d8311a96f55a327b54a267dc1a694107d09f49640f6eb484e6e67ce9985750cfce20fa16755d2db82230d966001304fe033479d484f985af7aaa921a8915  fix-locale-symlinks.patch
7b3f008e16151657aae57f4573b04f45fb2d791b90efce51ac74318f539240f73a4c8589d98f820b82bd11f5c4b3deeea052a3b33d7769357d63578096a1e0ae  shared-config-dir.patch
c1cd5ea52049b1cf621dfa481af59bca103368b3bb69754e8ec62b9a64de0b7785ca80bf6f9fe1ff549bf9cc7e03e4a7dc99153b75578fa05c0a4480d20cac7e  fix-php8.patch
"
