#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

include /usr/share/dpkg/pkg-info.mk

CFLAGS=$(shell dpkg-buildflags --get CFLAGS) -no-pie
CXXFLAGS=$(shell dpkg-buildflags --get CXXFLAGS) -no-pie

# Disable tests as they require root to run
export DEB_BUILD_MAINT_OPTIONS=nocheck

%:
	dh $@ --buildsystem=cmake --with python3 --no-parallel

override_dh_auto_configure:
	rm -rf $(CURDIR)/src/cc/libbpf && ln -s $(CURDIR)/libbpf  $(CURDIR)/src/cc/
	sed -i "s/\/lib//" $(CURDIR)/tools/*.sh
	dh_auto_configure -- -DREVISION_LAST=$(DEB_VERSION_UPSTREAM) -DREVISION=$(DEB_VERSION_UPSTREAM) -DLLVM_DEFINITIONS="-D_GNU_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS" -DPYTHON_CMD="python3"


override_dh_auto_install:
	dh_auto_install
	test -d $(CURDIR)/debian/tmp/usr/share/bcc/tools/doc && mv $(CURDIR)/debian/tmp/usr/share/bcc/tools/doc/* $(CURDIR)/debian/tmp/usr/share/bcc/doc/ && rm -d $(CURDIR)/debian/tmp/usr/share/bcc/tools/doc

	for eachFile in $(CURDIR)/debian/tmp/usr/share/bcc/tools/* ; do \
	    test -f  $$eachFile && mv $$eachFile $$eachFile-bpfcc ; \
	done

	for eachMan in $(CURDIR)/debian/tmp/usr/share/bcc/man/man8/* ; do \
	    test -f $$eachMan && manPage=`basename $$eachMan | cut -d "." -f1`; \
	    mv $$eachMan $(CURDIR)/debian/tmp/usr/share/bcc/man/man8/$$manPage-bpfcc.8 ; \
	done
	
	# Some binaries end up in wrong paths like /usr/sbin/lib/
	# THis fixes those
	test -d $(CURDIR)/debian/tmp/usr/share/bcc/tools/lib/ && mv $(CURDIR)/debian/tmp/usr/share/bcc/tools/lib/* $(CURDIR)/debian/tmp/usr/share/bcc/tools/ && rm -rf $(CURDIR)/debian/tmp/usr/share/bcc/tools/lib/

override_dh_python3:
	# See Debian Bug #926187
	dh_python3 --shebang=/usr/bin/python3

override_dh_installdocs:
	dh_installdocs -A debian/README.Debian

override_dh_auto_test:
ifeq (,$(filter nocheck,$(DEB_BUILD_MAINT_OPTIONS)))
    dh_auto_test
endif
