# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=bareos
pkgver=23.0.3
pkgrel=1
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"
license="AGPL-3.0-only"
makedepends="
	$_php
	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
	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-postgresql:_postgresql
$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
	"
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 \
		-Dbsrdir=/run/$pkgname \
		-Dsubsysdir=/var/lock \
		-Dbatch-insert=yes \
		-Dscsi-crypto=yes \
		-Dlmdb=yes \
		-Dndmp=no \
		-Drpath=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

	# Install logrotate script
	install -Dm644 core/scripts/logrotate "$pkgdir"/etc/logrotate.d/$pkgname

	# 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
}

_postgresql() {
	description="$description (postgresql driver)"
	mkdir -p "$subpkgdir"/usr/lib/bareos

	for _i in drops creates grants updates; do
		mkdir -p "$subpkgdir"/etc/bareos/scripts/ddl/$_i
		mv "$pkgdir"/etc/bareos/scripts/ddl/$_i/postgresql*.sql "$subpkgdir"/etc/bareos/scripts/ddl/$_i/
	done
	for s in create_bareos_database  \
		delete_catalog_backup    \
		drop_bareos_database 	 \
		drop_bareos_tables	 \
		grant_bareos_privileges  \
		make_bareos_tables	 \
		make_catalog_backup	 \
		update_bareos_tables 	 \
	; do
		cp "$builddir"/core/src/cats/$s "$subpkgdir"/etc/bareos/scripts/ddl ;
	done
}

_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="
42ede6976c894f89836ae272598edce8b3dadbdceb3b429724ea4c01a980470c161f03e54b58b17bd3fa3a7f750b4a79e5f96070fee5aae4b4fe73e79b2d8b61  bareos-23.0.3.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
3f3a10d89d8284f1952f84d8cbd559453f94d62e8d00baa8561fc03a9b6c7f456ba48dc8eaab18004bbbac4d5e69ab240e6b4bf3a1fc29df062a68924d957a8c  disable-werror.patch
"
