#!/usr/bin/make -f

# without "-relro,-pie" building on Stretch breaks
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)
DEB_CXXFLAGS_MAINT_APPEND = $(shell dpkg-buildflags --get CPPFLAGS)
DEB_LDFLAGS_MAINT_APPEND  = -Wl,--as-needed
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

VERSION                       := $(shell dpkg-parsechangelog | sed -ne 's,^Version: *\([0-9]*:\)\?\(.*\)$$,\2,p')

override_dh_auto_build:
	# build hostapd
	cp -v --remove-destination debian/config/config_realtek hostapd/.config
	dh_auto_build --sourcedirectory=hostapd \
	              --buildsystem=makefile \
		      --parallel
	dh_auto_clean --sourcedirectory=src --buildsystem=makefile

override_dh_auto_clean:
	dh_auto_clean --sourcedirectory=hostapd \
	              --buildsystem=makefile

override_dh_auto_install:
	$(info Skip dh_auto_install ...)

override_dh_clean:
	dh_clean hostapd/.config

override_dh_install:
	dh_install
	install --mode=755 -D debian/ifupdown/hostapd.sh \
		debian/hostapd-realtek/etc/hostapd/ifupdown.sh
	install --mode=644 -D debian/config/hostapd.conf \
		debian/hostapd-realtek/etc/hostapd.conf

override_dh_installchangelogs:
	dh_installchangelogs --package=hostapd-realtek hostapd/ChangeLog

override_dh_builddeb:
	dh_builddeb -- -Zxz -z6

override_dh_installinit:
	dh_installinit --name=hostapd

### end dh overrides

%:
	dh ${@} --parallel