#!/usr/bin/make -f

# output every command that modifies files on the build system.
#export DH_VERBOSE = 1
#export DEB_BUILD_MAINT_OPTIONS = hardening=+all
export OPENSSL_DIR=$(CURDIR)/local/openssl

# The sed below is due to gcc-11 changing the handling of hardfloat and softfloat
# for ARM devices.  Unlike prior versions of gcc, with gcc-11+, you have to pass
# -march=armv7-a+fp to show that you want/have support for hardware floating point.

# Unlike the Debian openssl package, this is a vanilla upstream openssl tarball, so we need to
# hardcode the arch into the config line, otherwise the CI gets the wrong one.
include /usr/share/dpkg/architecture.mk
ifeq ($(DEB_HOST_ARCH),arm64)
	export BUILDARCH=linux-aarch64
endif
ifeq ($(DEB_HOST_ARCH),amd64)
	export BUILDARCH=linux-x86_64
endif
ifeq ($(DEB_HOST_ARCH),armhf)
	export BUILDARCH=linux-armv4
endif
ifeq ($(DEB_HOST_ARCH),armel)
	export BUILDARCH=linux-armv4
endif
ifeq ($(DEB_HOST_ARCH),i386)
	export BUILDARCH=linux-x86
endif

%:
	dh $@ --with python3

override_dh_auto_build:
	echo "Downloading OpenSSL"
	wget https://github.com/openssl/openssl/archive/OpenSSL_1_1_1i.tar.gz -O $(CURDIR)/local/openssl.tar.gz
	cd $(CURDIR)/local && tar -xzf openssl.tar.gz
	mv $(CURDIR)/local/openssl-OpenSSL_1_1_1i $(OPENSSL_DIR)
	rm $(CURDIR)/local/openssl.tar.gz
	cd $(OPENSSL_DIR) && sed -ie 's/march=armv7-a"/march=armv7-a+fp"/g' ./config
	cd $(OPENSSL_DIR) && ./Configure $(BUILDARCH) -v --prefix=$(OPENSSL_DIR)/local enable-ssl2 enable-ssl3 enable-ssl3-method enable-des enable-rc4 enable-weak-ssl-ciphers no-shared
	cd $(OPENSSL_DIR) && make
	cd $(OPENSSL_DIR) && make install_sw

	echo "Compiling hostapd"
	cd $(CURDIR)/local/hostapd-eaphammer/hostapd && cp defconfig .config
	cd $(CURDIR)/local/hostapd-eaphammer/hostapd && make hostapd-eaphammer_lib V=1

override_dh_install:
	dh_install -Xstub

override_dh_python3:
	dh_python3 --no-ext-rename
