#!/usr/bin/make -f

# Hacks to build openssl in CI
# * The `RELEASE` env variable is set to the distro release (eg. kali-dev)
#   by the CI pipeline, however for the openssl config script this variable
#   has a different meaning. It's supposed to be set to a kernel release,
#   in case the caller wants to prevent auto-detection via `uname -r`.
#   Workaround: unset it.
# * The openssl config script detects the architecture via `uname -m`,
#   however for i386 builds it returns x86_64. That's not what we want.
#   Workaround: set the `MACHINE` env variable.
ifeq ($(CI),true)
ifneq ($(RELEASE),)
  RELEASE :=
endif
ifeq ($(BUILD_ARCH),i386)
  MACHINE := i686
  export MACHINE
endif
endif

# 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.

%:
	dh $@

override_dh_auto_build:
	# Build openssl from git and build heartleech against it
	git clone https://github.com/openssl/openssl.git -b OpenSSL_1_0_1g
	cd openssl; \
	sed -ie 's/march=armv7-a"/march=armv7-a+fp"/g' ./config; \
	./config; \
	make depend; \
	make; \
	gcc $$CFLAGS ../heartleech.c $$LDFLAGS libssl.a libcrypto.a -ldl -lpthread -o heartleech -I./include

override_dh_auto_install:
	# Copy the newly compiled binary in the package
	mkdir -p debian/heartleech/usr/bin
	cp openssl/heartleech debian/heartleech/usr/bin/

override_dh_auto_clean:
	rm -rf openssl
