#!/usr/bin/make -f

VERSION := $(shell dpkg-parsechangelog | awk '/^Version: / { print $$2 }' | cut -d+ -f1)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/architecture.mk

ifeq ($(DEB_HOST_ARCH),s390x)
export DEB_CFLAGS_MAINT_APPEND  = -Wall -O2
else
export DEB_CFLAGS_MAINT_APPEND  = -Wall -O3
endif

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

%:
	dh ${@}

override_dh_auto_configure:
# metric collectors written in Go
# H2O web server (experimental)
# eBPF programs for kernels without BTF support
# Linux eBPF metric collection
# Xen domain monitoring
	dh_auto_configure -- \
		-DENABLE_PLUGIN_GO:BOOL=OFF \
		-DENABLE_H2O:BOOL=OFF \
		-DENABLE_LEGACY_EBPF_PROGRAMS:BOOL=OFF \
		-DENABLE_PLUGIN_EBPF:BOOL=OFF \
		-DENABLE_PLUGIN_XENSTAT=OFF \
		-DENABLE_PROMETHEUS_REMOTE_WRITE:BOOL=ON \
		-DENABLE_DASHBOARD=OFF \
		-DCMAKE_INSTALL_PREFIX:PATH=/

execute_after_dh_auto_install:
	# Removing unused files
	rm -f debian/tmp/etc/netdata/.install-type
	rm -f debian/tmp/usr/libexec/netdata/install-service.sh
	rm -f debian/tmp/usr/lib/netdata/conf.d/netdata-updater.conf
	rm -f debian/tmp/usr/libexec/netdata/netdata-updater.sh
	rm -rf debian/tmp/usr/lib/netdata/system

	# Building without aws kinesis backend (needs aws sdk)
	rm -f debian/tmp/usr/lib/netdata/conf.d/aws_kinesis.conf

override_dh_install-arch:
	find debian/tmp -name .keep -delete

	# Copy architecture dependent plugins
	mkdir -p debian/netdata/etc/netdata
	cp debian/local/netdata/netdata.conf debian/netdata/etc/netdata
	touch debian/netdata/etc/netdata/.opt-out-from-anonymous-statistics

	mkdir -p debian/netdata/usr/libexec/netdata/plugins.d
	for plugin in cgroup-network apps.plugin nfacct.plugin perf.plugin slabinfo.plugin; \
	do \
		cp debian/tmp/usr/libexec/netdata/plugins.d/$${plugin} \
		   debian/netdata/usr/libexec/netdata/plugins.d; \
	done

	dh_install -a

override_dh_install-indep:
	find debian/tmp -name .keep -delete

	dh_install -i

	# Move architecture dependent plugins
	mkdir -p debian/netdata/usr/libexec/netdata/plugins.d
	for plugin in cgroup-network apps.plugin nfacct.plugin perf.plugin slabinfo.plugin; \
	do \
		mv -f debian/netdata-plugins-bash/usr/libexec/netdata/plugins.d/$${plugin} \
		   debian/netdata/usr/libexec/netdata/plugins.d; \
	done

	mkdir -p debian/netdata-plugins-python/usr/libexec/netdata/plugins.d
	for plugin in python.d.plugin; \
	do \
		mv debian/netdata-plugins-bash/usr/libexec/netdata/plugins.d/$${plugin} \
		   debian/netdata-plugins-python/usr/libexec/netdata/plugins.d; \
	done

override_dh_installsystemd:
	dh_installsystemd -p netdata --name=netdata
	dh_installsystemd --remaining-packages

override_dh_installlogrotate:
	dh_installlogrotate -p netdata --name=netdata
	dh_installlogrotate --remaining-packages

override_dh_fixperms-arch:
	dh_fixperms -a

	# apps.plugin should only be runnable by the netdata user. It will be
	# given extra capabilities in the postinst script.
	chmod 0754 debian/netdata/usr/libexec/netdata/plugins.d/apps.plugin
	chmod 4754 debian/netdata/usr/libexec/netdata/plugins.d/perf.plugin

override_dh_fixperms-indep:
	dh_fixperms -i

	# apps.plugin should only be runnable by the netdata user. It will be
	# given extra capabilities in the postinst script.
	chmod 0644 debian/netdata-plugins-bash/usr/libexec/netdata/charts.d/*.sh
	chmod 0644 debian/netdata-plugins-bash/usr/libexec/netdata/plugins.d/*.sh.inc
	chmod 0644 debian/netdata-plugins-python/usr/libexec/netdata/python.d/*.py

upstream:
	git clone --recursive https://github.com/netdata/netdata netdata-$(VERSION)+dfsg
	cd netdata-$(VERSION)+dfsg && git checkout -f v$(VERSION)

	# removing embedded python modules
	rm -rf netdata-$(VERSION)+dfsg/src/collectors/python.d.plugin/python_modules/pyyaml3
	rm -rf netdata-$(VERSION)+dfsg/src/collectors/python.d.plugin/python_modules/third_party
	rm -rf netdata-$(VERSION)+dfsg/src/collectors/python.d.plugin/python_modules/urllib3

	# removing embedded shared libraries
	rm -rf netdata-$(VERSION)+dfsg/src/ml/dlib
	rm -rf netdata-$(VERSION)+dfsg/src/web/server/h2o/libh2o

	# removing git metadata
	rm -rf netdata-$(VERSION)+dfsg/.git
