#!/usr/bin/make -f

%:
	dh $@ --parallel

LIBNET_VER := $(shell dpkg --status libnet1-dev | awk '/^Version:/ {print $$2}')

ENABLE_IPV6 := $(shell \
 if dpkg --compare-versions $(LIBNET_VER) ge 1.1.6; \
 then echo ON; \
 else echo OFF; \
 fi)

#For some reasons ettercap FTBFS with -PIC enabled
#/usr/bin/ld.bfd.real: CMakeFiles/lib_ettercap.dir/dissectors/ec_ssh.c.o: relocation R_X86_64_PC32 against symbol `ssh_conn_key' can not be used when making a shared object; recompile with -fPIC
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all

override_dh_auto_configure:
	@echo LIBNET_VER = $(LIBNET_VER)
	@echo ENABLE_IPV6 = $(ENABLE_IPV6)
	@echo main configuration deferred until build, as it must be done twice

ETTERCAP_OPTIONS=-DENABLE_IPV6=$(ENABLE_IPV6) -DBUNDLED_LIBS=OFF -DINSTALL_EXEDIR=/usr/sbin -DCMAKE_BUILD_TYPE=RelWithDebInfo

override_dh_auto_build:
	@echo Build the text-only executable:
	dh_auto_configure -- $(ETTERCAP_OPTIONS) -DENABLE_GTK=OFF
	dh_auto_build
	mv obj*/src/ettercap ettercap-text-only
	@echo Build the graphics-enabled executable:
	dh_auto_configure -- $(ETTERCAP_OPTIONS) -DENABLE_GTK=ON
	dh_auto_build

override_dh_auto_install:
	dh_auto_install
	@echo to be dropped in the next ettercap release in favour of INSTALL_EXEDIR cmake option
	dh_auto_install
	mkdir -p debian/tmp/usr/sbin
	sed 's/bin/sbin/g' -i debian/tmp/usr/share/polkit-1/actions/org.pkexec.ettercap.policy

override_dh_install:
	dh_install --list-missing
	mkdir --parents debian/ettercap-text-only/usr/sbin/
	cp --preserve=mode ettercap-text-only debian/ettercap-text-only/usr/sbin/ettercap
	@echo Upstream sets RPATH, which is against Debian policy:
	chrpath --list debian/ettercap-text-only/usr/sbin/ettercap
	chrpath --delete debian/ettercap-text-only/usr/sbin/ettercap
	chrpath --list debian/ettercap-graphical/usr/sbin/ettercap
	chrpath --delete debian/ettercap-graphical/usr/sbin/ettercap

override_dh_installdocs:
	dh_installdocs -pettercap-common
	dh_installdocs -Nettercap-common --link-doc=ettercap-common

override_dh_strip:
	dh_strip --dbg-package=ettercap-dbg
