#!/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 \
 -DENABLE_LUA=OFF \
 -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_install:
	dh_install --list-missing
	mkdir --parents debian/ettercap-text-only/usr/bin/
	cp --preserve=mode ettercap-text-only debian/ettercap-text-only/usr/bin/ettercap
	@echo The ettercap-pkexec man page belongs in ettercap-graphical
	-rm --verbose debian/ettercap-common/usr/share/man/man*/ettercap-pkexec*
	@echo Upstream sets RPATH, which is against Debian policy:
	chrpath --list debian/ettercap-text-only/usr/bin/ettercap
	chrpath --delete debian/ettercap-text-only/usr/bin/ettercap
	chrpath --list debian/ettercap-graphical/usr/bin/ettercap
	chrpath --delete debian/ettercap-graphical/usr/bin/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
