#!/usr/bin/make -f

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
include /usr/share/dpkg/buildflags.mk

# The build system doesn't use CPPFLAGS, pass them to CFLAGS/CXXFLAGS to
# enable the missing (hardening) flags
DEB_CFLAGS_MAINT_APPEND   = -MMD -Wall $(shell dpkg-buildflags --get CPPFLAGS) \
			    -Wno-error=implicit-function-declaration
DEB_CXXFLAGS_MAINT_APPEND = $(shell dpkg-buildflags --get CPPFLAGS)
export DEB_CFLAGS_MAINT_APPEND DEB_CXXFLAGS_MAINT_APPEND DEB_LDFLAGS_MAINT_APPEND

UCFLAGS   = -MMD -Wall -g -Os -fPIC

BINDIR    = /sbin
V = 1

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	CC=$(DEB_HOST_GNU_TYPE)-gcc
endif

export CC BINDIR V

DEB_HOST_ARCH_OS              ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
HOSTAPD_DOT_CONFIG            := debian/config/hostapd/$(DEB_HOST_ARCH_OS)
WPASUPPLICANT_DOT_CONFIG      := debian/config/wpasupplicant/$(DEB_HOST_ARCH_OS)
WPASUPPLICANT_UDEB_DOT_CONFIG := debian/config/wpasupplicant/$(DEB_HOST_ARCH_OS)-udeb

### start dh overrides
override_dh_auto_configure:

override_dh_auto_build:
	# build documentation
	dh_auto_build --sourcedirectory=wpa_supplicant/doc/docbook \
				  --buildsystem=makefile \
				  -- man
	cp -v --remove-destination $(WPASUPPLICANT_DOT_CONFIG) wpa_supplicant/.config
	dh_auto_build --sourcedirectory=wpa_supplicant \
				  --buildsystem=makefile \

	dh_auto_clean --sourcedirectory=src \
				  --buildsystem=makefile
	# build hostapd
	dh_auto_build --sourcedirectory=hostapd \
				  --buildsystem=makefile \

	dh_auto_clean --sourcedirectory=src \
				  --buildsystem=makefile

override_dh_auto_clean:
	dh_auto_clean --sourcedirectory=wpa_supplicant/doc/docbook \
				  --buildsystem=makefile
	dh_auto_clean --sourcedirectory=wpa_supplicant \
				  --buildsystem=makefile
	dh_auto_clean --sourcedirectory=hostapd \
				  --buildsystem=makefile

override_dh_auto_install:
	$(info Skip dh_auto_install ...)

override_dh_clean:
	dh_clean wpa_supplicant/.config

override_dh_install:
	dh_install
	install --mode=755 -D hostapd/hostapd \
		debian/cupid-hostapd/usr/sbin/cupid-hostapd
	install --mode=755 -D hostapd/hostapd_cli \
		debian/cupid-hostapd/usr/sbin/cupid-hostapd_cli
	install --mode=755 -D wpa_supplicant/wpa_cli \
		debian/cupid-wpasupplicant/usr/sbin/cupid-wpa_cli
	install --mode=755 -D wpa_supplicant/wpa_passphrase \
		debian/cupid-wpasupplicant/usr/bin/cupid-wpa_passphrase
	install --mode=755 -D wpa_supplicant/wpa_supplicant \
		debian/cupid-wpasupplicant/usr/sbin/cupid-wpa_supplicant
	install --mode=644 -D hostapd/hostapd.8 \
		debian/cupid-hostapd/usr/share/man/man8/cupid-hostapd.8
	install --mode=644 -D hostapd/hostapd_cli.1 \
		debian/cupid-hostapd/usr/share/man/man1/cupid-hostapd_cli.1
	install --mode=644 -D wpa_supplicant/doc/docbook/wpa_cli.8 \
		debian/cupid-wpasupplicant/usr/share/man/man8/cupid-wpa_cli.8
	install --mode=644 -D wpa_supplicant/doc/docbook/wpa_passphrase.8 \
		debian/cupid-wpasupplicant/usr/share/man/man8/cupid-wpa_passphrase.8
	install --mode=644 -D wpa_supplicant/doc/docbook/wpa_supplicant.8 \
		debian/cupid-wpasupplicant/usr/share/man/man8/cupid-wpa_supplicant.8
	install --mode=644 -D wpa_supplicant/doc/docbook/wpa_supplicant.conf.5 \
		debian/cupid-wpasupplicant/usr/share/man/man5/cupid-wpa_supplicant.conf.5

override_dh_installchangelogs:
	dh_installchangelogs --package=cupid-hostapd hostapd/ChangeLog
	dh_installchangelogs --package=cupid-wpasupplicant wpa_supplicant/ChangeLog

%:
	dh ${@}
