#!/usr/bin/make -f

# Find the arch we are building for, as this determines
# the location of plugins in /usr/lib
TOP = $(CURDIR)/debian/netdata
TEMPTOP = $(CURDIR)/debian/tmp

BASE_CONFIG = system/netdata.conf

SYSTEMD_VERSION = $(shell /bin/sh -c "systemd --version 2>&1 | head -n 1 | cut -f 2 -d ' '")

ifeq ($(shell test $(SYSTEMD_VERSION) -ge 235 && echo "1"), 1)
SYSTEMD_UNIT = system/netdata.service.v235
else
SYSTEMD_UNIT = system/netdata.service
endif

ifeq ($(shell test `uname -m` != "x86_64" && echo "1"), 1)
HAVE_EBPF = 0
EBPF_CONFIG = --disable-ebpf
else
HAVE_EBPF = 1
endif

%:
	dh $@

override_dh_installinit:
	echo "SystemD Version: $(SYSTEMD_VERSION)"
	echo "Using SystemD Unit: $(SYSTEMD_UNIT)"
	cp -v $(SYSTEMD_UNIT) debian/netdata.service

	dh_systemd_enable
	dh_installinit

override_dh_auto_configure:
	if [ $(HAVE_EBPF) -eq 1 ]; then \
		packaging/bundle-libbpf.sh . ${TOP}/usr/libexec/netdata/plugins.d; \
		packaging/bundle-ebpf-co-re.sh . ${TOP}/usr/libexec/netdata/plugins.d; \
	fi
	autoreconf -ivf
	dh_auto_configure -- --prefix=/usr --sysconfdir=/etc --localstatedir=/var --libdir=/usr/lib \
	--libexecdir=/usr/libexec --with-user=netdata --with-math --with-zlib --with-webdir=/var/lib/netdata/www \
	--disable-dependency-tracking $(EBPF_CONFIG)

override_dh_install:
	cp -v $(BASE_CONFIG) debian/netdata.conf

	dh_install

	# Set the CUPS plugin install rule
	#
	mkdir -p $(TOP)-plugin-cups/usr/libexec/netdata/plugins.d
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/cups.plugin \
	$(TOP)-plugin-cups/usr/libexec/netdata/plugins.d/cups.plugin

	# Add free IPMI plugin install rules
	#
	mkdir -p $(TOP)-plugin-freeipmi/usr/libexec/netdata/plugins.d
	mv -f $(TEMPTOP)/usr/libexec/netdata/plugins.d/freeipmi.plugin \
	$(TOP)-plugin-freeipmi/usr/libexec/netdata/plugins.d/freeipmi.plugin

	# Set the rest of the software in the main package
	#
	cp -rp $(TEMPTOP)/usr $(TOP)
	cp -rp $(TEMPTOP)/var $(TOP)
	cp -rp $(TEMPTOP)/etc $(TOP)
	mkdir -p "$(TOP)/var/log/netdata"
	mkdir -p "$(TOP)/var/cache/netdata"
	mkdir -p "$(TOP)/var/run/netdata"

	# Copy the updater script
	#
	cp -v packaging/installer/netdata-updater.sh $(TOP)/usr/libexec/netdata/netdata-updater.sh

	# Move files that local user shouldn't be editing to /usr/share/netdata
	#
	mkdir -p "$(TOP)/usr/share/netdata/www"
	for D in $$(find "$(TOP)/var/lib/netdata/www/" -maxdepth 1 -type d -printf '%f '); do \
		echo Relocating $$D; \
		mv "$(TOP)/var/lib/netdata/www/$$D" "$(TOP)/usr/share/netdata/www/$$D"; \
		ln -s "/usr/share/netdata/www/$$D" "$(TOP)/var/lib/netdata/www/$$D"; \
	done

	if [ $(HAVE_EBPF) -eq 1 ]; then \
		packaging/bundle-ebpf.sh . ${TOP}/usr/libexec/netdata/plugins.d; \
	fi

	# Install go
	#
	debian/install_go.sh $$(cat ${CURDIR}/packaging/go.d.version) $(TOP)/usr/lib/netdata $(TOP)/usr/libexec/netdata

override_dh_installdocs:
	dh_installdocs

	find . \
		-name README.md \
		-not -path './.travis/*' \
		-not -path './debian/*' \
		-not -path './contrib/*' \
		-exec cp \
		--parents \
		--target $(TOP)/usr/share/doc/netdata/ \
		{} \;

override_dh_fixperms:
	dh_fixperms

	# Updater script should be executable
	#
	chmod 0755 $(TOP)/usr/libexec/netdata/netdata-updater.sh

	# apps.plugin should only be runnable by the netdata user. It will be
	# given extra capabilities in the postinst script.
	#
	chmod 0750 $(TOP)/usr/libexec/netdata/plugins.d/apps.plugin
	chmod 0750 $(TOP)/usr/libexec/netdata/plugins.d/perf.plugin
	chmod 0750 $(TOP)/usr/libexec/netdata/plugins.d/slabinfo.plugin
	chmod 0750 $(TOP)/usr/libexec/netdata/plugins.d/go.d.plugin

	# CUPS plugin package
	chmod 0750 $(TOP)-plugin-cups/usr/libexec/netdata/plugins.d/cups.plugin

	# freeIPMI plugin package
	chmod 4750 $(TOP)-plugin-freeipmi/usr/libexec/netdata/plugins.d/freeipmi.plugin

override_dh_installlogrotate:
	cp system/netdata.logrotate debian/netdata.logrotate
	dh_installlogrotate

override_dh_clean:
	dh_clean

	# Tidy up copied/generated files
	#
	-[ -r $(CURDIR)/debian/netdata.logrotate ] && rm $(CURDIR)/debian/netdata.logrotate
	-[ -r $(CURDIR)/debian/netdata.conffiles ] && rm $(CURDIR)/debian/netdata.conffiles
