#!/usr/bin/make -f
# Sample debian.rules file - for GNU Hello (1.3).
# Copyright 1994,1995 by Ian Jackson.
# I hereby give you perpetual unlimited permission to copy,
# modify and relicense this file, provided that you do not remove
# my name from the file itself.  (I assert my moral right of
# paternity under the Copyright, Designs and Patents Act 1988.)
# This file may have to be extensively modified
#
# Modified to be a prototype for debmake by Christoph Lameter <clameter@debian.org>
include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/pkg-info.mk

export DEB_BUILD_MAINT_OPTIONS = hardening=+all,-zerocallusedregs

# Suppress spurious -Wfree-nonheap-object warning in mem.c
export CFLAGS += -Wno-free-nonheap-object

SHELL=/bin/bash

package=openssl

# For generating the manpages
export VERSION=$(DEB_VERSION_UPSTREAM)

ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
	export CROSS_COMPILE ?= $(DEB_HOST_GNU_TYPE)-
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
	MAKEFLAGS += -j$(NUMJOBS)
endif

CONFARGS  = --prefix=/usr --openssldir=/usr/lib/ssl --libdir=lib/$(DEB_HOST_MULTIARCH) no-idea no-mdc2 no-rc5 no-ssl3 no-ssl3-method no-capieng enable-ktls enable-ec_nistp_64_gcc_128 \
            enable-tfo
#OPT_alpha = ev4 ev5
ARCHOPTS  = OPT_$(DEB_HOST_ARCH)
OPTS      = $($(ARCHOPTS))

ifeq (,$(findstring terse,$(DEB_BUILD_OPTIONS)))
	TESTSUITE_FLAGS = HARNESS_VERBOSE=yes
else
	TESTSUITE_FLAGS =
endif

ifeq ($(filter amd64 i386,$(DEB_HOST_ARCH_CPU)),)
	CONFARGS += no-padlockeng
endif

%:
	dh $@ --without autoreconf

override_dh_auto_configure:
	test -z "$(OPTS)" || for opt in $(OPTS); \
	do \
		set -xe; \
		mkdir build_$$opt; \
		cd build_$$opt ; \
		../Configure shared $(CONFARGS) debian-$(DEB_HOST_ARCH)-$$opt; \
		GCC_VERSION=$$(gcc -dumpversion | cut -d. -f1); \
		if [ $$GCC_VERSION -lt 11 ]; then \
			sed -i 's/-fzero-call-used-regs=[^ "]*//g' configdata.pm; \
			perl configdata.pm; \
		else \
			perl configdata.pm -d; \
		fi; \
		cd .. ;\
	done
	# Debian Perl policy 5.1 (Script Magic)
	mkdir build_shared; cd build_shared; HASHBANGPERL=/usr/bin/perl ../Configure shared $(CONFARGS) debian-$(DEB_HOST_ARCH) ;\
	GCC_VERSION=$$(gcc -dumpversion | cut -d. -f1); \
	if [ $$GCC_VERSION -lt 11 ]; then \
		sed -i 's/-fzero-call-used-regs=[^ "]*//g' configdata.pm; \
		perl configdata.pm; \
	else \
		perl configdata.pm -d; \
	fi

override_dh_auto_build-indep:

override_dh_auto_build-arch:
	test -z "$(OPTS)" || for opt in $(OPTS); \
	do \
		set -xe; \
		$(MAKE) -C build_$$opt build_libs; \
	done
	$(MAKE) -C build_shared build_libs

override_dh_auto_test-indep:

override_dh_auto_test-arch:
ifneq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	@echo Skipping tests
else
	test -z "$(OPTS)" || for opt in $(OPTS); \
	do \
		set -xe; \
		$(MAKE) -C build_$$opt test $(TESTSUITE_FLAGS); \
	done
	$(MAKE) -C build_shared test $(TESTSUITE_FLAGS)
endif

override_dh_auto_clean:
	rm -rf build_shared
	test -z "$(OPTS)" || for opt in $(OPTS); \
	do \
		set -xe; \
		rm -rf build_$$opt; \
	done
	dh_auto_clean

override_dh_auto_install-indep:

override_dh_auto_install-arch:
	# Install only the renamed shared and static libs — nothing else
	mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)
	# Copy and rename shared libs (versioned + symlinks)
	for f in build_shared/libssl.so* build_shared/libcrypto.so*; do \
		[ -e "$$f" ] || continue; \
		dest=$$(basename $$f | sed 's/\(lib[a-z]*\)\.so/\1-nginx.so/'); \
		cp -a $$f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$$dest; \
	done
	# Rename SONAME inside shared libs for nginx linking
	for libfile in debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libssl-nginx.so* \
				   debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libcrypto-nginx.so*; do \
		[ -L "$$libfile" ] && continue; \
		[ -f "$$libfile" ] || continue; \
		old_soname=$$(readelf -d $$libfile 2>/dev/null | grep SONAME | grep -oP 'lib[a-z-]+\.so[0-9.]*' | head -1); \
		if [ -n "$$old_soname" ]; then \
			new_soname=$$(echo $$old_soname | sed 's/libssl\.so/libssl-nginx.so/; s/libcrypto\.so/libcrypto-nginx.so/'); \
			if [ "$$old_soname" != "$$new_soname" ]; then \
				echo "Renaming SONAME: $$old_soname → $$new_soname in $$libfile"; \
				patchelf --set-soname $$new_soname $$libfile; \
			fi; \
		fi; \
	done
	# Copy and rename static libs
	cp build_shared/libssl.a    debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libssl-nginx.a
	cp build_shared/libcrypto.a debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/libcrypto-nginx.a
	# Copy and rename optimized builds
	for opt in $(OPTS); do \
		set -xe; \
		mkdir -p debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$$opt; \
		for f in build_$$opt/libssl.so* build_$$opt/libcrypto.so*; do \
			[ -e "$$f" ] || continue; \
			dest=$$(basename $$f | sed 's/\(lib[a-z]*\)\.so/\1-nginx.so/'); \
			cp -a $$f debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$$opt/$$dest; \
		done; \
	done
	# Rename SONAME in optimized builds
	for opt in $(OPTS); do \
		for libfile in debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$$opt/libssl-nginx.so* \
					   debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/$$opt/libcrypto-nginx.so*; do \
			[ -L "$$libfile" ] && continue; \
			[ -f "$$libfile" ] || continue; \
			old_soname=$$(readelf -d $$libfile 2>/dev/null | grep SONAME | grep -oP 'lib[a-z-]+\.so[0-9.]*' | head -1); \
			if [ -n "$$old_soname" ]; then \
				new_soname=$$(echo $$old_soname | sed 's/libssl\.so/libssl-nginx.so/; s/libcrypto\.so/libcrypto-nginx.so/'); \
				if [ "$$old_soname" != "$$new_soname" ]; then \
					echo "Renaming SONAME in $$opt: $$old_soname → $$new_soname"; \
					patchelf --set-soname $$new_soname $$libfile; \
				fi; \
			fi; \
		done; \
	done
	# Install headers into openssl-nginx subdirectory
	mkdir -p debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/openssl-nginx
	cp -r include/openssl/* debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/openssl-nginx/
	# Copy generated config headers from build
	cp build_shared/include/openssl/configuration.h debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/openssl-nginx/ 2>/dev/null || true
	cp build_shared/include/openssl/opensslconf.h   debian/tmp/usr/include/$(DEB_HOST_MULTIARCH)/openssl-nginx/ 2>/dev/null || true

override_dh_installman:

override_dh_makeshlibs:
	dh_makeshlibs -a -V -Xengines -Xossl-modules -- -c4

override_dh_shlibdeps:
	dh_shlibdeps -a -Xlibssl-nginx -Xlibcrypto-nginx

override_dh_strip:
	dh_strip -a -Xlibssl-nginx -Xlibcrypto-nginx
