# Contributor: Valery Kartel <valery.kartel@gmail.com>
# Contributor: Łukasz Jendrysik <scadu@yandex.com>
# Contributor: Carlo Landmeter <clandmeter@gmail.com>
# Maintainer: Carlo Landmeter <clandmeter@gmail.com>
pkgname=clamav
pkgver=0.99.4
pkgrel=0
pkgusers="clamav"
pkggroups="clamav"
pkgdesc="An anti-virus toolkit for UNIX eis-ng backport"
url="http://www.clamav.net/"
arch="all"
license="GPL-2.0"
depends="$pkgname-scanner $pkgname-daemon"
depends_dev="libressl-dev"
install="freshclam.pre-install
	clamav-daemon.pre-install clamav-daemon.pre-upgrade"
makedepends="ncurses-dev zlib-dev bzip2-dev pcre-dev linux-headers $depends_dev
	libmilter-dev fts-dev"
subpackages="$pkgname-doc $pkgname-dev $pkgname-lib $pkgname-libunrar
	$pkgname-daemon $pkgname-scanner $pkgname-db::noarch freshclam
	$pkgname-daemon-openrc:daemon_rc:noarch freshclam-openrc:freshclam_rc:noarch
	$pkgname-milter"
source="http://www.clamav.net/downloads/production/$pkgname-$pkgver.tar.gz
	clamd.initd
	clamd.confd
	freshclam.initd
	freshclam.confd
	clamd.logrotate
	freshclam.logrotate

	0001-libclamav-explicitly-use-top_srcdir-.git-dir.patch
	"

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

# secfixes:
#   0.99.4-r0:
#     - CVE-2012-6706
#     - CVE-2017-6419
#     - CVE-2017-11423
#     - CVE-2018-0202
#     - CVE-2018-1000085
#   0.99.3-r0:
#     - CVE-2017-12374
#     - CVE-2017-12375
#     - CVE-2017-12376
#     - CVE-2017-12377
#     - CVE-2017-12378
#     - CVE-2017-12379
#     - CVE-2017-12380

prepare() {
	update_config_sub
	default_prepare
}

build() {
	cd "$builddir"
	LIBS=-lfts ./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=/usr \
		--libdir=/usr/lib \
		--sysconfdir=/etc/clamav \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--without-iconv \
		--disable-llvm \
		--with-user=clamav \
		--with-group=clamav \
		--with-dbdir=/var/lib/clamav \
		--enable-clamdtop \
		--enable-bigstack \
		--with-pcre \
		--enable-milter
	make
}

check() {
	cd "$builddir"
	make check
}

package() {
	cd "$builddir"
	make DESTDIR="$pkgdir" install

	# set proper defaults
	sed -i -e "s:^\(Example\):\# \1:" \
		-e "s:.*\(PidFile\) .*:\1 /run/clamav/freshclam.pid:" \
		-e "s:.*\(DatabaseOwner\) .*:\1 clamav:" \
		-e "s:^\#\(UpdateLogFile\) .*:\1 /var/log/clamav/freshclam.log:" \
		-e "s:^\#\(NotifyClamd\).*:\1 /etc/clamav/clamd.conf:" \
		-e "s:^\#\(ScriptedUpdates\).*:\1 yes:" \
		-e "s:^\#\(AllowSupplementaryGroups\).*:\1 yes:" \
		"$pkgdir"/etc/clamav/freshclam.conf.sample

	# set proper defaults
	sed -i -e "s:^\(Example\):\# \1:" \
		-e "s:.*\(PidFile\) .*:\1 /run/clamav/clamd.pid:" \
		-e "s:.*\(LocalSocket\) .*:\1 /run/clamav/clamd.sock:" \
		-e "s:.*\(User\) .*:\1 clamav:" \
		-e "s:^\#\(LogFile\) .*:\1 /var/log/clamav/clamd.log:" \
		-e "s:^\#\(LogTime\).*:\1 yes:" \
	       -e "s:^\#\(AllowSupplementaryGroups\).*:\1 yes:" \
		"$pkgdir"/etc/clamav/clamd.conf.sample
}

check() {
	cd "$builddir"
	make check
}

lib() {
	pkgdesc="ClamAV library"
	replaces="clamav"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libclamav.so.* \
		"$subpkgdir"/usr/lib/
}

libunrar() {
	pkgdesc="ClamAV unrar libraries"
	replaces="clamav"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/libclamunrar* \
		"$subpkgdir"/usr/lib/
}

freshclam() {
	pkgdesc="Auto-updater for the Clam Antivirus scanner data-files"
	replaces="clamav clamav-db"
	mkdir -p "$subpkgdir"/usr/bin \
		"$subpkgdir"/etc/clamav

	mv "$pkgdir"/usr/bin/freshclam "$subpkgdir"/usr/bin
	mv "$pkgdir"/etc/clamav/freshclam.conf.sample \
		"$subpkgdir"/etc/clamav/freshclam.conf

	install -d -m755 -o clamav -g clamav \
		"$subpkgdir"/var/log/clamav \
		"$subpkgdir"/var/lib/clamav

	install -m644 -D "$srcdir"/freshclam.logrotate \
		"$subpkgdir"/etc/logrotate.d/freshclam
}

freshclam_rc() {
	pkgdesc="Auto-updater for the Clam Antivirus scanner data-files (OpenRC init scripts)"
	install_if="freshclam=$pkgver-r$pkgrel openrc"
	replaces="clamav freshclam"
	mkdir -p "$subpkgdir"/etc/init.d \
		"$subpkgdir"/etc/conf.d

	install -m755 -D "$srcdir"/freshclam.initd \
		"$subpkgdir"/etc/init.d/freshclam
	install -m644 -D "$srcdir"/freshclam.confd \
		"$subpkgdir"/etc/conf.d/freshclam
}

db() {
	pkgdesc="ClamAV dummy package for compatibility"
	replaces="clamav"
	depends="freshclam"
	mkdir -p "$subpkgdir"/var/lib/ \
		"$subpkgdir"/usr/bin \
		"$subpkgdir"/etc/clamav
	install -d -m755 -o clamav -g clamav \
		"$subpkgdir"/var/log/clamav \
		"$subpkgdir"/var/lib/clamav
}

scanner() {
	pkgdesc="ClamAV command-line scanner and utils"
	replaces="clamav"
	depends="freshclam"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/clamscan \
		"$subpkgdir"/usr/bin/
	mv "$pkgdir"/usr/bin/sigtool \
		"$subpkgdir"/usr/bin/
	mv "$pkgdir"/usr/bin/clambc \
		"$subpkgdir"/usr/bin/
}

daemon() {
	pkgdesc="ClamAV daemon scanner"
	replaces="clamav"
	depends="freshclam"
	mkdir -p "$subpkgdir"/usr/bin \
		"$subpkgdir"/usr/sbin \
		"$subpkgdir"/etc/clamav
	install -d -m 755 -o clamav -g clamav \
		"$subpkgdir"/var/log/clamav \
		"$subpkgdir"/var/lib/clamav
	mv "$pkgdir"/usr/bin/clamconf \
		"$subpkgdir"/usr/bin/
	mv "$pkgdir"/usr/sbin/clamd \
		"$subpkgdir"/usr/sbin/
	mv "$pkgdir"/usr/bin/clamdtop \
		"$subpkgdir"/usr/bin/
	mv "$pkgdir"/usr/bin/clamdscan \
		"$subpkgdir"/usr/bin/
	mv "$pkgdir"/etc/clamav/clamd.conf.sample \
		"$subpkgdir"/etc/clamav/clamd.conf
	install -m644 -D "$srcdir"/clamd.logrotate \
		"$subpkgdir"/etc/logrotate.d/clamd
}

daemon_rc() {
	pkgdesc="ClamAV anti-virus scanner daemon (OpenRC init scripts)"
	replaces="clamav"
	depends="freshclam-openrc"
	install_if="$pkgname-daemon=$pkgver-r$pkgrel openrc"

	install -m755 -D "$srcdir"/clamd.initd \
		"$subpkgdir"/etc/init.d/clamd
	install -m644 -D "$srcdir"/clamd.confd \
		"$subpkgdir"/etc/conf.d/clamd
}

milter() {
	pkgdesc="ClamAV milter"
	mkdir -p "$subpkgdir"/usr/sbin \
		"$subpkgdir"/etc/clamav
	mv "$pkgdir"/usr/sbin/clamav-milter \
		"$subpkgdir"/usr/sbin/
	mv "$pkgdir"/etc/clamav/clamav-milter.conf.sample \
		"$subpkgdir"/etc/clamav/clamav-milter.conf
}

sha512sums="778d5ef510d8d4bdfac5dc33d92469ed4283c414b3d42da6e1a0b13ed70e37755d5c837622dc336bc728ba1f8bf5485fc8a8d3a67a90e9aaa9e4dc71ece0691d  clamav-0.99.4.tar.gz
ed81be79bf9a25eec071312252121cc76c96838407377b75077bf94922055f1de99f327982ac4dccd5be85003baa95385e5d002fabab32bb851bb30178475edd  clamd.initd
59c561b3dcb0b616b647cd8e4ebc46a2cc5e7144c8c7ea0054cc1c3021d1da8f67e4dad5c083c3fe712ed887aaabfca91b538f4759537e7c4c9ab71ba4fd5794  clamd.confd
00daed8afb67a6e4a29893340246c8840cce970dd9103d26557ecdd26ef60b12551d2291c214fc657faaaa339484052079347411b0cad65e3a33ece56d57cf16  freshclam.initd
ba181fe1abaac7b898ccb40b0713455aa3c9d5e25ad21d687b6cac09b0105b9e376526e7c776a44636234d8db819709d8d6a6cc76119bc3e98b637b1a3f26c08  freshclam.confd
3ae493dd1610a819402c015f6b8c0f080f926b72dc43d2bded60030bf6a55040e4b88e0f64d3aae299dc1133d7e1b89855e7346b4665a64e8b82592f7b75cf6a  clamd.logrotate
30cff378bc28c76b795e00c92ae5ee623f3abe4a19bed61dd8403c96e72658bb02b7f040d26a6258104af754464d25ea7d9646918c4b47d2ba9a8cbf4687056c  freshclam.logrotate
7eeac468d1e2bc6fde8d1e4cbe0b7fad646d41798fe860718bf0a8ed6026920222daae125804a58f8add02487bf9c9e72a618f87441b20782910668915c99a75  0001-libclamav-explicitly-use-top_srcdir-.git-dir.patch"
