#!/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)
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 V

DEB_HOST_ARCH_OS              ?= $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
HOSTAPD_DOT_CONFIG            := debian/config/hostapd/$(DEB_HOST_ARCH_OS)
VERSION                       := $(shell dpkg-parsechangelog | sed -ne 's,^Version: *\([0-9]*:\)\?\(.*\)$$,\2,p')


### start dh overrides
override_dh_auto_build:
	# build hostapd
	cp -v --remove-destination $(HOSTAPD_DOT_CONFIG) hostapd/.config
	dh_auto_build --sourcedirectory=hostapd \
	              --buildsystem=makefile \
		      --parallel
	dh_auto_clean --sourcedirectory=src --buildsystem=makefile

override_dh_strip:
	dh_strip --dbg-package=hostapd-dbg

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

override_dh_systemd_enable:
	dh_systemd_enable --no-enable --name=hostapd hostapd.service

override_dh_systemd_start:
	dh_systemd_start --no-start --name=hostapd

override_dh_auto_install:
	$(info Skip dh_auto_install ...)

override_dh_clean:
	# make sure to remove the staging directory for the udeb
	dh_clean hostapd/.config

override_dh_install:
	mkdir -p debian/tmp/var/lib/hostapd/acl
	dh_install
	# install ifupdown hook scripts
	install --mode=755 -D debian/ifupdown/hostapd.sh \
		debian/hostapd/etc/hostapd/ifupdown.sh

override_dh_fixperms:
	dh_fixperms
	chmod 600 debian/hostapd/etc/hostapd.conf

override_dh_installchangelogs:
	dh_installchangelogs --package=hostapd hostapd/ChangeLog

override_dh_gencontrol:
	dh_gencontrol -phostapd -- '-v1:$(VERSION)'
	dh_gencontrol --remaining-packages

override_dh_builddeb:
	dh_builddeb -- -Zxz -z6
### end dh overrides

%:
	dh ${@} --parallel --with systemd

get-orig-source:
	chmod +x $(CURDIR)/debian/get-orig-source
	$(CURDIR)/debian/get-orig-source $(CURDIR)
