# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=bareos
pkgver=24.0.1
pkgrel=0
pkgdesc="Bareos - Backup Archiving REcovery Open Sourced"
url="https://www.bareos.com/"
_php=php82
# chromium-chromedriver only present on these arches
arch="x86_64 armv7"
options="!check net"
license="AGPL-3.0-only"
makedepends="
	$_php
	$_php-dev
	acl-dev
	bsd-compat-headers
	chromium-chromedriver
	cmake
	gawk
	gettext-dev
	gtest-dev
	jansson-dev
	json-c-dev
	libcap-dev
	libdroplet-dev
	libpq-dev
	libtirpc-dev
	libxml2-dev
	linux-pam-dev
	lzo-dev
	ncurses-dev
	openssl-dev>3
	postgresql17
	py3-apache-libcloud
	py3-apache-libcloud
	py3-dateutil
	py3-psycopg2
	py3-psycopg2
	python3-dev
	readline-dev
	samurai
	"
install="$pkgname.pre-install $pkgname.post-install $pkgname.pre-upgrade"
subpackages="$pkgname-dev $pkgname-doc $pkgname-openrc
$pkgname-libs
$pkgname-storage:_sd
$pkgname-filedaemon:_fd
$pkgname-webui:_webui:noarch $pkgname-webui-apache2:_webui_apache2:noarch $pkgname-webui-nginx:_webui_nginx:noarch"
pkgusers=$pkgname
pkggroups=$pkgname
source="$pkgname-$pkgver.tar.gz::https://github.com/bareos/bareos/archive/Release/$pkgver.tar.gz
	$pkgname-dir.initd
	$pkgname-sd.initd
	$pkgname-fd.initd

	$pkgname-dir.confd
	$pkgname-sd.confd
	$pkgname-fd.confd
	$_php-fpm.conf

	add-libintl.patch
	nginx-conf.patch
	apache2-conf.patch
	path-mounted.patch
	support-alpine-dist.patch
	alpine-agpl3-compat.patch
	disable-werror.patch
	fix-pgctl-path.patch
	musl-fix-pthread.patch
	"
builddir="$srcdir"/$pkgname-Release-$pkgver

case "$CARCH" in
arm*|x86)
	;;
*)
	makedepends="$makedepends ceph-dev"
	;;
esac

# secfixes:
#   19.2.8-r0:
#     - CVE-2020-4042
#     - CVE-2020-11061

prepare() {
	default_prepare
	mkdir -pv core/platforms/alpine
	touch core/platforms/alpine/CMakeLists.txt

	sed -i '/bareos-symlink-default-db-backend.cmake/d' core/src/cats/CMakeLists.txt
}

build() {
	export CFLAGS="$CFLAGS -Wno-error"
	cmake -B build -G Ninja \
		-DCMAKE_VERBOSE_MAKEFILE=ON \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr \
		-DCMAKE_INSTALL_LIBDIR:PATH=/usr/lib \
		-DBUILD_SHARED_LIBS:BOOL=ON \
		-DVERSION_STRING=$pkgver \
		-Dprefix=/usr \
		-Dsysconfdir=/etc \
		-Dbasename=$pkgname \
		-Dhostname=localhost \
		-Dconfdir=/etc/$pkgname \
		-Dlibdir=/usr/lib \
		-Darchivedir=/var/lib/$pkgname/archive \
		-Dworkingdir=/var/lib/$pkgname \
		-Dscriptdir=/etc/$pkgname/scripts \
		-Dplugindir=/usr/lib/$pkgname \
		-Dbackenddir=/usr/lib/$pkgname \
		-Dlogdir=/var/log/$pkgname \
		-Dsubsysdir=/var/lock \
		-Dbatch-insert=yes \
		-Dscsi-crypto=yes \
		-Dlmdb=yes \
		-Dndmp=no \
		-Ddynamic-storage-backends=yes \
		-Dpostgresql=yes \
		-Dopenssl=yes \
		-Dtraymonitor=no \
		-Dsystemd=no \
		-Ddir-user=$pkgname \
		-Ddir-group=$pkgname \
		-Dsd-user=$pkgname \
		-Dsd-group=$pkgname
	cmake --build build
}

package() {
	DESTDIR="$pkgdir" cmake --install build
	mkdir -p "$pkgdir"/usr/sbin
	mkdir -p "$pkgdir"/usr/bin
	mkdir -p "$pkgdir"/var/lib/bareos
	mkdir -p "$pkgdir"/var/log/bareos

	local daemon=dir
	install -Dm755 "$srcdir"/$pkgname-$daemon.initd \
		"$pkgdir"/etc/init.d/$pkgname-$daemon
	install -Dm755 "$srcdir"/$pkgname-$daemon.confd \
		"$pkgdir"/etc/conf.d/$pkgname-$daemon

	# Fix job to use shell script instead of perl script
	sed -i -e 's/make_catalog_backup.in/make_catalog_backup/' \
		"$pkgdir"/etc/bareos/bareos-dir.d/job/BackupCatalog.conf

	# remove http configuration
	rm -r "$pkgdir"/etc/httpd

	chown $pkgusers "$pkgdir"/var/lib/bareos
	chown $pkgusers "$pkgdir"/var/log/bareos
	chown -R $pkgusers "$pkgdir"/etc/bareos
}

_webui() {
	description="$description (webui)"
	depends="$_php $_php-gettext $_php-mbstring $_php-session $_php-ctype $_php-opcache $_php-openssl $_php-intl $_php-json $_php-curl"

	mkdir -p "$subpkgdir"/usr/share
	mv "$pkgdir"/usr/share/bareos-webui "$subpkgdir"/usr/share/

	mkdir -p "$subpkgdir"/etc
	mv "$pkgdir"/etc/bareos-webui "$subpkgdir"/etc/

	mkdir -p "$subpkgdir"/etc/bareos/bareos-dir.d/profile/
	mv "$pkgdir"/etc/bareos/bareos-dir.d/profile/webui-* "$subpkgdir"/etc/bareos/bareos-dir.d/profile/
}

_webui_apache2() {
	description="$description (webui apache2 configuration)"
	depends="$pkgname-webui apache2 apache-mod-fcgid $_php-fpm"

	mkdir -p "$subpkgdir"/etc/apache2/conf.d
	cp "$builddir"/webui/install/apache/bareos-webui.conf "$subpkgdir"/etc/apache2/conf.d/
	cp "$srcdir"/$_php-fpm.conf "$subpkgdir"/etc/apache2/conf.d/
}

_webui_nginx() {
	description="$description (webui nginx configuration)"
	depends="$pkgname-webui nginx $_php-fpm"

	mkdir -p "$subpkgdir"/etc/nginx/http.d
	cp "$builddir"/webui/install/nginx/bareos-webui.conf "$subpkgdir"/etc/nginx/http.d/
}

_sd() {
	description="$description (Storage Daemon)"
	local daemon=sd

	mkdir -p "$subpkgdir"/etc/bareos
	mkdir -p "$subpkgdir"/usr/sbin
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/etc/bareos/bareos-sd.d "$subpkgdir"/etc/bareos
	mv "$pkgdir"/usr/sbin/bareos-sd "$subpkgdir"/usr/sbin/

	install -Dm755 "$srcdir"/$pkgname-$daemon.initd \
		"$subpkgdir"/etc/init.d/$pkgname-$daemon
	install -Dm644 "$srcdir"/$pkgname-$daemon.confd \
		"$subpkgdir"/etc/conf.d/$pkgname-$daemon
}

_fd() {
	description="$description (File Daemon)"
	depends="$pkgname=$pkgver-r$pkgrel"
	local daemon=fd

	mkdir -p "$subpkgdir"/etc/bareos
	mkdir -p "$subpkgdir"/usr/sbin
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/etc/bareos/bareos-fd.d "$subpkgdir"/etc/bareos
	mv "$pkgdir"/usr/sbin/bareos-fd "$subpkgdir"/usr/sbin/

	install -Dm755 "$srcdir"/$pkgname-$daemon.initd \
		"$subpkgdir"/etc/init.d/$pkgname-$daemon
	install -Dm644 "$srcdir"/$pkgname-$daemon.confd \
		"$subpkgdir"/etc/conf.d/$pkgname-$daemon
}

sha512sums="
ec510282f9b4bc5b80fddd706111ed891d776a713f4f2548fc48ea45bb59f636b92ae0c2d146c11c598f3d47a5bd9975220b0435123d084ed351ca3053110a8c  bareos-24.0.1.tar.gz
4c11b4e42b10cb1d22bdcb73f78116cb756d7d239d6fcc6f15fb0af5535666cd2290650097750dfeb5a151a0286cb19e11ea57efe7d8a5a188b4bd7161b696b4  bareos-dir.initd
407af1b90672959bee9206d3f241226e46a7c3091a79b91da70b6e5abb171f93dec53d5151713627096ec9fb81d0f7690a20bcf4c361abc8a018c6cc967db8b7  bareos-sd.initd
cf0403ca8f3efb5cfb57915df9203ff16ed69a72d8d0bc189f5f221d36233127d9ed5c8391a46969b0de5943ff4717f52ffae9a5afa1ae24e27631bd1c58b438  bareos-fd.initd
9858964a291ee9849e144f968a239758e3e6905bb5420717652114d8a583e3342e50f046efa41730f30cfc3f047b485251e93085fb13b327a6f2272d7c4c0309  bareos-dir.confd
eb5d942edb45d349804c90eb0379cfabca765d63277b21feb4f1937ef8b655b6ba9feb3b671b61f582996ff4f9740614c197ae990c00879731ec43d6da8f4286  bareos-sd.confd
4bdf1e9d5f2f7a85efdf9aaaa0d35daadf83e766afeccdba69a94490a2e59c0b46fbbbdb1dcd5be389603ac5a196fe35c0fb389bc241eab0bf414c9ec432451f  bareos-fd.confd
89269be79b5ccc3bddeb938a9aee4f80704219aa31423eedc42b0637ba00ebedeaa1617972005a8db6697cb0905e4321c4aedaaa44b1129be09d5ce080c06029  php82-fpm.conf
9d12f26d77d384ab122fdb133667b17a1e83c748d9fca9ca810c368b8f4f3cb2fc258f7c54628e96a0347e7f996c00f46d89655aad2fee373a61e7433fd783b9  add-libintl.patch
46b21a0b3c81d7be7d992d267441684c23b39eb2227326d7a8500aef8de3100146f2af98835cea555830d6490dd16fa58575095ba1ab833b697c24bddb68babd  nginx-conf.patch
d7950b298306986d28b7c2e6b1d93d5a289c0e29cdd05825e667ea72da34ac2f078c7de344124d7cab2f342067f67725c40117a06cf95b1ef67f19f67e682521  apache2-conf.patch
2053eb72bbdb8c02d9ee7dcf387a973e0203485eb85304f1e1513ad501f6738d9fc0bb97596d6d9a1a8c96f6675fd2d2edafc85e961a5bd370b5f22f320c7cbf  path-mounted.patch
e427e32bd5d589618cc8efc6d9a679f62e01904a76f2ce3f56bdbba7df31bf12922380c848bb409291f685fdb80a6ed25d842d44728a363103ab99591b473232  support-alpine-dist.patch
846cf5a6885fdd3aaa66650edab682a9eaade311d246fc25d0db4e43bd6f97db9b5d80d9636d155513efd430044c851d880e8c9c34ed354b5cbcef3aff7c3581  alpine-agpl3-compat.patch
777d7daa2449a91025f1f5fdb5ff72d0a195a38f813f3ae643d280bb65f1b2d4c9007cc33ee31a3e254dfa1549a4c53b9cfe0752620c62865547130026d3ee93  disable-werror.patch
821d19e23aa50abf7bcd202cbfed762a70a971fdfd0195be5036674dc3cd97942c2b3e3cc19010504d673533bba709c465f71c1989b22ceba2f3fe4e635e32ed  fix-pgctl-path.patch
d5fcf97934316279c38bf193e0ced2cbcabf4177e9d1bbeca3d0bfccdc1afe08daf6e58b0b42c4c988b2d66d51c019e37b88fc6fbebfcc910e16da1622be561d  musl-fix-pthread.patch
"
