# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
# Maintainer: Kaarle Ritvanen <kaarle.ritvanen@datakunkku.fi>
pkgname=zoneminder
pkgver=1.30.4
pkgrel=3
_crud=c3976f1478c681b0bbc132ec3a3e82c3984eeed5
pkgdesc="Video camera surveillance system"
url="https://www.zoneminder.com/"
arch="x86_64"
license="GPL-2.0-or-later"
_php=php5
depends="bash ffmpeg perl perl-archive-zip perl-date-manip perl-dbd-mysql
         perl-dbi perl-device-serialport perl-libwww
         perl-lwp-protocol-https perl-mailtools perl-mime-lite
         perl-mime-tools perl-php-serialization perl-sys-mmap
         perl-time-hires $_php $_php-apcu $_php-gd $_php-pdo_mysql
         $_php-sockets procps sudo zip"
makedepends="bzip2-dev cmake curl-dev ffmpeg-dev gnutls-dev libgcrypt-dev
             libjpeg-turbo-dev mariadb-connector-c-dev pcre-dev perl-dev ${_php}-cli
             polkit-dev vlc-dev x264-dev gzip"
subpackages=$pkgname-doc
source="zoneminder-$pkgver.tar.gz::https://github.com/ZoneMinder/ZoneMinder/archive/${pkgver}.tar.gz
	crud-$_crud.tar.gz::https://github.com/FriendsOfCake/crud/archive/$_crud.tar.gz
	$pkgname.initd
	musl-fix.patch"

# secfixes:
#   1.30.2-r0:
#     - CVE-2016-10140
#     - CVE-2017-5595
#   1.30.2-r3:
#     - CVE-2017-5367
#     - CVE-2017-5368

_builddir=$srcdir/ZoneMinder-$pkgver
prepare() {
	local i
	cd "$_builddir"

	i=web/api/app/Plugin/Crud
	rmdir $i
	ln -s $srcdir/crud-$_crud $i

	for i in $source; do
		case $i in
		*.patch) msg $i; patch -p1 -i "$srcdir"/$i || return 1;;
		esac
	done

	utils/zmeditconfigdata.sh ZM_CHECK_FOR_UPDATES no
	utils/zmeditconfigdata.sh ZM_LOG_LEVEL_FILE 0
	utils/zmeditconfigdata.sh ZM_LOG_LEVEL_SYSLOG -5
	utils/zmeditconfigdata.sh ZM_PATH_ZMS /cgi-bin/zm/nph-zms
}

build() {
	cd "$_builddir"
	cmake \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DZM_CGIDIR=/usr/share/webapps/$pkgname/cgi-bin \
		-DZM_LOGDIR=/var/log/zoneminder \
		-DZM_RUNDIR=/var/run/zoneminder \
		-DZM_SOCKDIR=/var/run/zoneminder \
		-DZM_TMPDIR=/var/lib/zoneminder/temp \
		-DZM_WEBDIR=/usr/share/webapps/$pkgname/htdocs \
		. && make
}

package() {
	cd "$_builddir"
	local dir
	make install DESTDIR="$pkgdir"
	find "$pkgdir" -name perllocal.pod -delete
	install -D zmlinkcontent.sh "$pkgdir"/usr/sbin/zmlinkcontent.sh

	install -D -m 755 "$srcdir/$pkgname.initd" "$pkgdir/etc/init.d/$pkgname"
	chmod 640 "$pkgdir/etc/zm.conf"

	install -g wheel -m 2750 -d "$pkgdir"/var/log/$pkgname

	# create symlinks from webapps to default http location
	mkdir -p "$pkgdir"/var/www/localhost/htdocs \
		"$pkgdir"/var/www/localhost/cgi-bin
	ln -sf /usr/share/webapps/zoneminder/htdocs \
		"$pkgdir"/var/www/localhost/htdocs/zm
	ln -sf /usr/share/webapps/zoneminder/cgi-bin \
		"$pkgdir"/var/www/localhost/cgi-bin/zm
}
sha512sums="edb750c15802502418ed75bc5f479f6dcee4f4a0e73e80c207eb4bbc70c3cacdb684a017256f975c6104996a1629b38c698735f400ac37dfa7a1b9a20863dec7  zoneminder-1.30.4.tar.gz
f665741a1fbcca5e48ab8eddaa4686b23426aed7b22d68fdfa6c951fb6ce7dbbcfce5f1807e30a18fbc8fd4e3a8028a5687db17a070d05301d93d49f67952e44  crud-c3976f1478c681b0bbc132ec3a3e82c3984eeed5.tar.gz
ae02daacc41e2b32ca0da5cf407c05447a5ce8e0cf035421111856511dc1075da75d9da32f8f013940c631215a62f3133860b35225bda405230b94a6b097c865  zoneminder.initd
34a18a6db3ebc5bfba0b40894e85112ad50d0caedf4e69f23ec98dedcba03fe223d5f3dc1ad3c76269b9cb48d60464e1674e0571a52d840f95c3adeac42d601f  musl-fix.patch"
