#!/usr/bin/make -f

CODENAME := $(shell lsb_release -cs)
DOCS_DIR := debian/intelmq/usr/share/doc/intelmq
export INTELMQ_SKIP_REDIS=1
export INTELMQ_SKIP_INTERNET=1

# Compile list of bot READMEs
BOTROOT := intelmq/bots
BOTCATEGORIES := collectors experts outputs parsers
BOTCATEGORIES := $(foreach cat,$(BOTCATEGORIES),$(BOTROOT)/$(cat))
BOTS	:= $(foreach bot,$(BOTCATEGORIES),$(wildcard $(bot)/*))
BOTDOCS := $(foreach bot,$(BOTS),$(wildcard $(bot)/*.md))

# This file is based on a version
# automatically generated by stdeb 0.8.5 at
# Wed, 23 Mar 2016 17:49:26 +0000
export PYBUILD_NAME=intelmq
%:
	dh $@ --with python3,sphinxdoc --without python2 --buildsystem=pybuild --with quilt --with systemd

build:
	if [ $(CODENAME) = 'xenial' ] || [ $(CODENAME) = 'jessie' ]; then\
		patch -p1 setup.py debian/patches/fix-dnspython-name.patch;\
	fi
	dh build --with python3,sphinxdoc --without python2 --buildsystem=pybuild --with quilt --with systemd
	# These tests frequently fail on Ubuntu and Debian systems.
	# On some systems this command will be executed twice, so -f
	rm -rf intelmq/tests/bots/collectors/tcp/

override_dh_auto_build:
	dh_auto_build
	$(MAKE) -C docs html

override_dh_auto_install: $(BOTDOCS)
	rm intelmq/bin/rewrite_config_files.py
	sed -i -e '/#!\/usr\/bin\//d' intelmq/bin/*.py
	sed -i -f debian/sedfile intelmq/bots/BOTS intelmq/etc/* docs/user/intelmqctl.rst docs/user/bots.rst setup.py contrib/logrotate/intelmq contrib/logcheck/logcheck.logfiles
	python3 setup.py install --root=debian/intelmq --prefix=/usr
	# these are already in /usr/bin/
	#rm %{buildroot}/%{python3_sitelib}/intelmq/bots/experts/maxmind_geoip/update-geoip-data
	#rm %{buildroot}/%{python3_sitelib}/intelmq/bots/experts/asn_lookup/update-asn-data
	#rm %{buildroot}/%{python3_sitelib}/intelmq/bots/experts/tor_nodes/update-tor-nodes
	# and rename those in /usr/bin
	mv debian/intelmq/usr/bin/update-geoip-data debian/intelmq/usr/bin/intelmq-update-geoip-data
	mv debian/intelmq/usr/bin/update-asn-data debian/intelmq/usr/bin/intelmq-update-asn-data
	mv debian/intelmq/usr/bin/update-tor-nodes debian/intelmq/usr/bin/intelmq-update-tor-nodes
	mv debian/intelmq/usr/bin/update-rfiprisk-data debian/intelmq/usr/bin/intelmq-update-rfiprisk-data
	# create directories
	mkdir -p debian/intelmq/var/log/intelmq
	mkdir -p debian/intelmq/var/lib/intelmq/bots/file-output
	mkdir -p debian/intelmq/etc/logrotate.d
	mkdir -p debian/intelmq/var/lib/intelmq/bots/modify/
	## BOTS
	# Include all bot READMEs
	for readme in $(foreach bot,$(BOTDOCS),$(subst intelmq/bots/,,$(bot))); \
	do \
		mkdir -p $(DOCS_DIR)/bots/$$(dirname $$readme); \
		cp intelmq/bots/$$readme $(DOCS_DIR)/bots/$$(dirname $$readme); \
	done
	# remove program not needed for packages
	rm debian/intelmq/usr/bin/intelmqsetup
	# create directory layout and empty state file
	ROOT_DIR=debian/intelmq/ PYTHONPATH=. python3 intelmq/bin/intelmqsetup.py --skip-ownership --state-file debian/intelmq/var/lib/intelmq/state.json

override_dh_install:
	dh_install
	install -Dm 644 contrib/logcheck/ignore debian/intelmq/etc/logcheck/ignore.d.server/intelmq
	install -Dm 644 contrib/logcheck/violations debian/intelmq/etc/logcheck/violations.d/intelmq
	install -Dm 644 contrib/logcheck/logcheck.logfiles debian/intelmq/etc/logcheck/logcheck.logfiles.d/intelmq

override_dh_systemd_start:
	dh_systemd_start --restart-after-upgrade
