# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Francesco Colista <fcolista@alpinelinux.org>
# Maintainer: Francesco Colista <fcolista@alpinelinux.org>
pkgname=bareos
pkgver=17.2.4
pkgrel=1
pkgdesc="Bareos - Backup Archiving REcovery Open Sourced"
url="http://www.bareos.org"
arch="all"
options="!check"
license="AGPL-3.0"
depends="perl"
makedepends="file libtool libintl libpcap-dev lzo-dev \
	sqlite-dev postgresql-dev gnutls-dev mariadb-connector-c-dev acl-dev \
	qt-dev python-dev readline-dev ncurses-dev jansson-dev"
install="$pkgname.pre-install $pkgname.post-install"
subpackages="$pkgname-dev $pkgname-doc $pkgname-trayicon"
pkgusers=$pkgname
pkggroups=$pkgname
source="$pkgname-$pkgver.tar.gz::https://github.com/$pkgname/$pkgname/archive/Release/$pkgver.tar.gz
	$pkgname-dir.initd
	$pkgname-sd.initd
	$pkgname-fd.initd
	path-mounted.patch
	mariadb.patch
	"
builddir="$srcdir"/${pkgname}-Release-${pkgver}

prepare() {
	update_config_guess
	default_prepare
}

build() {
	cd "$builddir"
	./configure \
		--prefix=/usr \
		--sysconfdir=/etc \
		--localstatedir=/var \
		--with-basename=$pkgname \
		--with-hostname=localhost \
		--with-confdir=/etc/$pkgname \
		--with-archivedir=/var/lib/$pkgname/archive \
		--with-scriptdir=/etc/$pkgname/scripts \
		--with-working-dir=/var/lib/$pkgname \
		--with-plugindir=/usr/lib/$pkgname \
		--with-backenddir=/usr/lib/$pkgname \
		--with-pid-dir=/run/$pkgname \
		--with-logdir=/var/log/$pkgname \
		--with-subsys-dir=/var/lock \
		--enable-smartalloc \
		--enable-largefile \
		--enable-readline \
		--enable-traymonitor \
		--enable-batch-insert \
		--enable-acl \
		--enable-xattr \
		--enable-scsi-crypto \
		--enable-lmdb \
		--enable-ipv6 \
		--enable-dynamic-cats-backends \
		--enable-dynamic-debian-package-list \
		--enable-sql-pooling \
		--enable-includes \
		--disable-conio \
		--disable-nls \
		--disable-rpath \
		--with-postgresql \
		--with-mysql \
		--with-sqlite3 \
		--with-gnutls \
		--with-python \
		--without-openssl \
		--without-systemd \
		--with-dir-user=$pkgname \
		--with-dir-group=$pkgname \
		--with-sd-user=$pkgname \
		--with-sd-group=$pkgname \
		--with-fd-user=root \
		--with-fd-group=$pkgname
	make
}

package() {
	cd "$builddir"
	make DESTDIR="$pkgdir" install
	mkdir -p "$pkgdir"/usr/sbin
	mkdir -p "$pkgdir"/usr/bin

	local daemon; for daemon in dir sd fd; do
		install -Dm755 "$srcdir"/$pkgname-$daemon.initd \
			"$pkgdir"/etc/init.d/$pkgname-$daemon
	done

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

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

trayicon() {
	local dir
	provides="$pkgname-tray-monitor"
	depends="$pkgname"
	pkgdesc="$pkgdesc (qt tray monitor)"
	mkdir -p "$subpkgdir"/etc/$pkgname "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/share "$subpkgdir"/usr
	mv "$pkgdir"/etc/xdg "$subpkgdir"/etc
	mv "$pkgdir"/etc/$pkgname/tray-monitor.d "$subpkgdir"/etc/$pkgname
	mv "$pkgdir"/usr/bin/bareos-tray-monitor "$subpkgdir"/usr/bin
	for dir in bareos-dir.d/console bareos-fd.d/director bareos-sd.d/director; do
		mkdir -p "$subpkgdir"/etc/$pkgname/$dir
		mv "$pkgdir"/etc/$pkgname/$dir/bareos-mon.conf \
			"$subpkgdir"/etc/$pkgname/$dir
	done
}

sha512sums="165bee4dcfabac7e72839831de7067b4dafd1f82dd1615d7fa08ca331b1b3efa516555e33ca2d2ef42482c9b5104769c5ef3dd40a12e1954934874b6884ea26b  bareos-17.2.4.tar.gz
eb1e7072b579bf9ae21f2e351d6900abb277db64e373f4760bac8188b82929376e4a196d2c935cefe1ae4cc2c396f2fcba1a25642b26e2f92a0d008fbdc4b5f2  bareos-dir.initd
c770b1d041fafef93d4eb0269ba8d9733e85ef465657fe8dd5d5c68a27ec773cec9c5c582d4a16596d95bbf6dbd3f7194dc9c0d8ed73138e9fb438fba9aa9445  bareos-sd.initd
c6347079dbcef5f4a69ec0c4ecc31803520d715d599d89c6bbfbb3741a86c50d7295c30432889b13ee9c16f2feaa84b1c6ae992cfee6505d569c6493d7e85a5b  bareos-fd.initd
eac4614c1b29ff0f12061837e425ae495890076021b6d1b0f1beb93501cfb905170342dac5dab69b09f825d5b9416eea25fa02e2174b5a704315c7feb08ff3d3  path-mounted.patch
fd4b06cc8f118c05afbc03238376c7c6bedaf3f564090819e3fb1ab01320c79e1515ea7a81a307648015cc98a2a14aceb67684a166b986c5314bfd23b1e5ebde  mariadb.patch"
