# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Jeff Bilyk <jbilyk at gmail>
# Contributor: Leonardo Arena <rnalrd@alpinelinux.org>
# Maintainer: Natanael Copa <ncopa@alpinelinux.org>
pkgname=zabbix
pkgver=3.4.5
pkgrel=0
pkgdesc="Enterprise-class open source distributed monitoring"
url="http://www.zabbix.com"
arch="all"
license="GPL"
depends="fping"
_php=php7
_php_depends="$_php ${_php}-gd ${_php}-curl ${_php}-bcmath ${_php}-sockets
	${_php}-iconv ${_php}-xmlreader ${_php}-xmlwriter ${_php}-ctype
	${_php}-gettext ${_php}-session ${_php}-simplexml ${_php}-json"
makedepends="postgresql-dev curl-dev net-snmp-dev libevent-dev pcre-dev
	sqlite-dev mariadb-dev curl-dev openipmi-dev unixodbc-dev
	libxml2-dev autoconf automake libssh2-dev gnutls-dev
	$_php_depends"
install="$pkgname.pre-install"
pkgusers="zabbix"
pkggroups="zabbix"
subpackages="$pkgname-doc $pkgname-agent $pkgname-pgsql $pkgname-mysql $pkgname-sqlite
	$pkgname-webif::noarch $pkgname-utils $pkgname-setup::noarch"
source="http://downloads.sourceforge.net/$pkgname/$pkgname-$pkgver.tar.gz
	zabbix-server.initd
	zabbix-server.confd
	zabbix-agentd.initd
	zabbix-proxy.initd

	zabbix-getloadavg.patch
	automake.patch
	musl-fix-includes.patch
	zabbix_server.conf.patch
	"

_builddir="$srcdir"/$pkgname-$pkgver

# security fixes:
#   3.0.4-r0:
#   - CVE N/A ZBX-11023

prepare() {
	cd "$_builddir"
	# update_config_sub || return 1
	for i in $source; do
		case $i in
		*eglibc*.patch)
			if [ "$CLIBC" == "eglibc" ]; then
				msg "Applying $i"
				patch -p1 -i "$srcdir"/$i || return 1
			fi
			;;
		*.patch)
			msg "Applying $i"
			patch -p1 -i "$srcdir"/$i || return 1
			;;
		esac
	done
	aclocal -I m4 && autoconf && autoheader \
		&& automake --add-missing || return 1
	# update_config_sub
	# Fix config file locations
	for file in server agentd proxy; do
		sed -i "$_builddir"/conf/zabbix_${file}.conf \
			-e 's|SNMPTrapperFile=/tmp|SNMPTrapperFile=/var/log/zabbix|' \
			-e 's|PidFile=/tmp|PidFile=/var/run/zabbix|' \
			-e 's|LogFile=/tmp|LogFile=/var/log/zabbix|' || return 1
	done
}

build() {
	# set default configure flags
	_configure="--prefix=/usr \
			--sysconfdir=/etc/zabbix \
			--mandir=/usr/share/man \
			--infodir=/usr/share/info \
			--enable-agent \
			--enable-proxy \
			--enable-ipv6 \
			--with-net-snmp \
			--with-libcurl \
			--with-libxml2 \
			--with-openipmi \
			--with-unixodbc \
			--with-ssh2 \
			--with-gnutls
			"
        # we run build for each db type
        # make sure prepare is same for each db
	for db in postgresql mysql sqlite3; do
		cd "$srcdir"
		msg "Building for $db"
		cp -r "$pkgname-$pkgver" "$pkgname-$pkgver-$db"
		cd "$_builddir-$db"
		case $db in
			sqlite3)
				./configure \
					--build=$CBUILD \
					--host=$CHOST \
					--with-$db \
					$_configure \
			;;
			*)
				./configure \
					--build=$CBUILD \
					--host=$CHOST \
					--enable-server \
					--with-$db \
					$_configure \
			|| return 1
		esac
		make || return 1
	done
}

package() {
        # doing manual install
	for i in agentd proxy server; do
		install -D -m755 "$_builddir"/man/zabbix_$i.man \
			"$pkgdir"/usr/share/man/man8/zabbix_$i.8
	done
	for i in get sender; do
		install -D -m755 "$_builddir"/man/zabbix_$i.man \
			"$pkgdir"/usr/share/man/man1/zabbix_$i.1
	done
	install -d -m0750 -o zabbix -g zabbix \
                "$pkgdir"/var/run/zabbix "$pkgdir"/var/log/zabbix
        install -D -m0644 "$_builddir"/conf/zabbix_server.conf \
		"$pkgdir"/etc/zabbix/zabbix_server.conf
        for i in server proxy; do
                install -D -m0755 "$srcdir"/zabbix-$i.initd \
                        "$pkgdir"/etc/init.d/zabbix-$i || return 1
        done
	install -D -m0644 "$srcdir"/zabbix-server.confd \
		"$pkgdir"/etc/conf.d/zabbix-server || return 1
}

setup() {
	pkgdesc="Zabbix images and sql files"
	depends=
	mkdir -p "$subpkgdir"/usr/share/zabbix/ || return 1
	mv "$_builddir"/database "$subpkgdir"/usr/share/zabbix/
}

_do_db() {
        pkgdesc="Zabbix server with $1 database support"
        depends="$pkgname $2"
	local i=
	mkdir -p "$subpkgdir"/usr/sbin
	if [ "$1" != "sqlite3" ]; then
		mv "$_builddir-$1"/src/zabbix_server/zabbix_server \
			"$subpkgdir"/usr/sbin/ || return 1
	fi
	mv "$_builddir-$1"/src/zabbix_proxy/zabbix_proxy \
		"$subpkgdir"/usr/sbin/ || return 1
        install -D -m0644 "$_builddir"/conf/zabbix_proxy.conf \
		"$pkgdir"/etc/zabbix/zabbix_proxy.conf || return 1
	cd "$_builddir"
	for i in upgrades/dbpatches/*/$1; do
		[ -e $i ] || continue
		mkdir -p "$subpkgdir"/usr/share/zabbix/${i%/*}
		mv "$i" "$subpkgdir"/usr/share/zabbix/$i
	done
        return 0
}

pgsql()	{ _do_db postgresql "!$pkgname-mysql !$pkgname-sqlite";}
mysql()	{ _do_db mysql "!$pkgname-pgsql !$pkgname-sqlite";}
sqlite(){ _do_db sqlite3 "!$pkgname-pgsql !$pkgname-mysql";}

utils() {
	pkgdesc="Zabbix client utilities"
	depends=
	mkdir -p "$subpkgdir"/usr/bin
	mv "$_builddir-postgresql"/src/zabbix_get/zabbix_get \
		"$subpkgdir"/usr/bin
        mv "$_builddir-postgresql"/src/zabbix_sender/zabbix_sender \
	                "$subpkgdir"/usr/bin
}

webif() {
	pkgdesc="Zabbix web-interface"
	depends="$_php_depends"
	_wwwdir="$subpkgdir"/usr/share/webapps/zabbix
	mkdir -p $_wwwdir
	mv "$_builddir"/frontends/php/* "$_wwwdir"
}

agent() {
	pkgdesc="Zabbix Network Monitoring Agent"
	depends=
	install="$subpkgname.pre-install"
        install -d -m0750 -o zabbix -g zabbix \
                "$subpkgdir"/var/run/zabbix "$subpkgdir"/var/log/zabbix
	install -D -m0644 "$_builddir"/conf/zabbix_agentd.conf \
		"$subpkgdir"/etc/zabbix/zabbix_agentd.conf
	install -D -m0755 "$srcdir"/zabbix-agentd.initd \
		"$subpkgdir"/etc/init.d/zabbix-agentd
	mkdir -p "$subpkgdir"/usr/sbin
        mv "$_builddir-postgresql"/src/zabbix_agent/zabbix_agentd \
	                "$subpkgdir"/usr/sbin/

}

sha512sums="e77c8693d687c9f1e3c160f2f41161b5e4ac663f0db69f4bff424da4ba607815ad0148448c6f80a2b9357ad7472016610f4beed173e40c6fcf661874d75c69a4  zabbix-3.4.5.tar.gz
9998ee172a28002d98bacc3f76038ff52b8cf2b206e101418d76b4ca3de94afaf92cb4f7a6235ecf177f74beb9dd3ea1f3983c4f164b4f60bb601acba65aa175  zabbix-server.initd
9c06527bf653c40585fa7eeb3f7a0b2fc454031d24cd0d1633aed87b78a681c5227a193c5b9fcfcea0839135874e27ba7dd9b198573f905f680a2856f79e9512  zabbix-server.confd
523013cab3ba79cbc00db92f09d4c5d514fd6aa9cbebf8f29227dc91fbc19d2f8375af74c21d2037e4f3380a818f808194dbc94e69709ef2cf90f66e715895c4  zabbix-agentd.initd
a26e7ac422ff60a4b8eed3603022c3a1bde640870bb9286ab061c3cb5c2fd7e91ddb317cb3d1cf61034adda0a080fc212ad416c9e2853a1deb03c5279753f4e2  zabbix-proxy.initd
9fc413b11a01c8202c7ee1c7950d6ca3de2d2d6cd01bea994cd4bc412533b53c4e4b1f58fc3c8df16ea70902053e278e2c5dcc936ce3e0a686a6eac62310ef53  zabbix-getloadavg.patch
7f70dfd602aa164ec8cc65ebb7e8274c685975f6aea9051933928051b8d9b6e368e5a673a07e7084a2105468c5085d72fa7b9f934460f10648d594f28f031a91  automake.patch
72709df7624d99b2eaab8d91d95167580e80da10b3ac65b7f27f12d858f0d051d4f9143bcabceae2bfd51aeb7c4ca93e2b74670637ec0925b026e3c52475d52b  musl-fix-includes.patch
183e2d99800d8eebb932cb31c5a3e8742ce6a15e87aec50dc49d9c20b35840eb7ede9920aa62d2c7a5e3f584f5c3df398aa08e93f98fa537a3ba6db63b472a1f  zabbix_server.conf.patch"
