#! /usr/bin/make -f

# enable all hardening options (see #763372)
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

CONFIGURE_ARGS = -- --disable-dependency-tracking		\
	--disable-symbol-hiding --enable-versioned-symbols	\
	--enable-threaded-resolver --with-lber-lib=lber		\
	--with-gssapi=/usr --with-nghttp2	\
	--includedir=/usr/include/$(DEB_HOST_MULTIARCH)		\
	--with-zsh-functions-dir=/usr/share/zsh/vendor-completions

# disable libssh2 on Ubuntu (see #888449)
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
	CONFIGURE_ARGS += --with-libssh --without-libssh2
else
	CONFIGURE_ARGS += --without-libssh --with-libssh2
endif


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

export DEB_CFLAGS_MAINT_APPEND = -D_DEB_HOST_ARCH=\"$(DEB_HOST_MULTIARCH)\"
export DEB_CXXFLAGS_MAINT_APPEND = -D_DEB_HOST_ARCH=\"$(DEB_HOST_MULTIARCH)\"

%:
	dh $@

override_dh_auto_configure:
	mkdir -p debian/build debian/build-gnutls debian/build-nss
	# pop the last patch (nss)
	quilt pop
	# pop the second last patch (gnutls)
	quilt pop
	# get the source without nss and gnutls patches
	tar -cf - --exclude=debian/build* --exclude=.pc . \
		| tar -xf - -C debian/build
	# push the second last patch which must be gnutls
	quilt push
	# get the source with gnutls patch applied
	tar -cf - --exclude=debian/build* --exclude=.pc . \
		| tar -xf - -C debian/build-gnutls
	# push the last patch which must be nss
	quilt push
	# get the source with nss patch applied
	tar -cf - --exclude=debian/build* --exclude=.pc . \
		| tar -xf - -C debian/build-nss
	# run buildconf and make sure to copy the patched ltmain.sh
	for flavour in build build-gnutls build-nss; do \
		(cd debian/$$flavour && ./buildconf && cp ../../ltmain.sh .) \
	done
	cd debian/build && dh_auto_configure ${CONFIGURE_ARGS} --with-openssl \
		--with-ca-path=/etc/ssl/certs \
		--with-ca-bundle=/etc/ssl/certs/ca-certificates.crt
	cd debian/build-gnutls && dh_auto_configure ${CONFIGURE_ARGS} --with-gnutls \
		--with-ca-path=/etc/ssl/certs
	cd debian/build-nss && dh_auto_configure ${CONFIGURE_ARGS} --with-nss --with-nss-deprecated

override_dh_auto_build:
	cd debian/build && dh_auto_build
	cd debian/build-gnutls && dh_auto_build
	cd debian/build-nss && dh_auto_build

# Test parameters that are passed to runtests.pl.
# -n disables valgrind usage
TESTS_GENERAL_PARAMETERS := -n

# These tests fail if a IPv6-only builder is used:
# https://bugs.debian.org/1032343
# https://github.com/curl/curl/issues/10682
TESTS_FAILS_ON_IPV6_ONLY_MACHINES += $(addprefix ~, 300 301 303 304 306 309 310 325 364 400 401 403 406 407 408 409 410 414 417 560 678 1112 1272 1561 1562 1630 1631 1632 2034 2037 2041 3000 3001)

TESTS_GENERAL_PARAMETERS += $(TESTS_FAILS_ON_IPV6_ONLY_MACHINES)

override_dh_auto_test:
ifeq ($(filter nocheck,$(DEB_BUILD_PROFILES)),)
# OpenSSL tests.
	cd debian/build && VERBOSE=1 \
		TFLAGS="$(TESTS_GENERAL_PARAMETERS)" \
		$(MAKE) test-nonflaky

# GnuTLS tests.
	cd debian/build-gnutls && VERBOSE=1 \
		TFLAGS="$(TESTS_GENERAL_PARAMETERS)" \
		$(MAKE) test-nonflaky

# NSS tests.
	cd debian/build-nss && VERBOSE=1 \
		TFLAGS="$(TESTS_GENERAL_PARAMETERS)" \
		$(MAKE) test-nonflaky
endif

override_dh_install:
	${MAKE} -C debian/build					\
		DESTDIR=$(shell pwd)/debian/tmp install
	${MAKE} -C debian/build-gnutls				\
		DESTDIR=$(shell pwd)/debian/tmp-gnutls install
	${MAKE} -C debian/build-nss				\
		DESTDIR=$(shell pwd)/debian/tmp-nss install
	find debian/tmp* -name '*.la' -delete
	dh_install -plibcurl3-gnutls -plibcurl4-gnutls-dev	\
		--sourcedir=debian/tmp-gnutls
	dh_install -plibcurl3-nss -plibcurl4-nss-dev		\
		--sourcedir=debian/tmp-nss
	dh_install -pcurl -plibcurl4 -plibcurl4-openssl-dev -plibcurl4-doc \
		--sourcedir=debian/tmp
# Modify curl-config to make it architecture-independent:
# 1. In --static-libs output, replace the output of krb5-config (which
#    currently includes architecture-specific paths) with a call at
#    runtime to krb5-config.  Of course, this will only work correctly
#    if the installed libkrb5-dev matches the architecture of the
#    program you're linking, or if libkrb5-dev is made
#    multiarch-compatible at some point in the future.  For dynamic
#    linking this has no impact.
# 2. In --configure output, replace the architecture-specific paths
#    used for --libdir and --libexecdir with a literal backquoted call
#    to dpkg-architecture.  This is functionally equivalent to the way
#    debhelper actually invokes configure, and indicates to the user
#    (who runs curl-config --configure in order to learn about how the
#    library was compiled) that they are in fact using a multi-arch
#    package.
# 3. Likewise, replace the architecture name used for --build (and
#    build_alias) with a literal backquoted call to dpkg-architecture.
# 4. In --configure output, remove
#    -fdebug-prefix-map=/buildd/specific/random/path=. and
#    -ffile-prefix-map=/buildd/specific/random/path=.
	sed -e "/-lcurl /s|`krb5-config --libs gssapi`|\`krb5-config --libs gssapi\`|" \
	    -e "/--prefix/s|/$(DEB_HOST_MULTIARCH)'|/'\\\\\`dpkg-architecture -qDEB_HOST_MULTIARCH\\\\\`|g" \
	    -e "/--prefix/s|=$(DEB_BUILD_GNU_TYPE)'|='\\\\\`dpkg-architecture -qDEB_BUILD_GNU_TYPE\\\\\`|g" \
	    -e "/-fdebug-prefix-map=/s|\(-fdebug-prefix-map=\)/[^ ]*=.||" \
	    -e "/-ffile-prefix-map=/s|\(-ffile-prefix-map=\)/[^ ]*=.||" \
	    -i `find . -name curl-config`
# When we get here, everything has been properly installed.  Because
# of debhelper-compat 13, we have to remove some files that are
# otherwise flagged as not installed and will cause the build to fail.
	rm -rfv debian/tmp/usr/share/man/* \
		debian/tmp/usr/share/fish/* \
		debian/tmp/usr/share/aclocal/*

override_dh_installchangelogs:
	dh_installchangelogs CHANGES

override_dh_compress:
	dh_compress -X.pdf

override_dh_auto_clean:
	$(RM) -r debian/build* debian/tmp*
	dh_auto_clean
