#!/usr/bin/make -f

include  /usr/share/dpkg/pkg-info.mk

# at least LOCALTIME_{MIN,MAX} are affected by this, see #791362
export TZ=UTC

# these are used by executible dh_install files
export fullversion := $(shell /bin/bash debian/config.debian --full-version)
export archtriplet := $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
export version     := $(shell /bin/bash debian/config.debian --version)

nextversion := $(shell /bin/bash debian/config.debian --next-version)
srcdir      := $(shell pwd)
tmp	    = debian/tmp
lib         = usr/lib/$(archtriplet)/perl/$(version)
baselib     = usr/lib/$(archtriplet)/perl-base
share       = usr/share/perl/$(version)
patchlevel  = patchlevel-debian.h

# we put the generated config.sh files here
configdata  = $(tmp)/usr/lib/$(archtriplet)/perl/debian-config-data-$(fullversion)

# If nonempty, this will determine $Config{debian_abi} and Provides: entries
# (otherwise, the Provides: entries will be generated by debian/mkprovides)
perlabi =

DEB_BUILD_GNU_TYPE      ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
DEB_HOST_GNU_TYPE       ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)

# the static flavour is for perl-base;
# shared is for libperl5.xx and debug goes into perl-debug
flavours    = static shared debug

# Invoking PERL_TO_USE is expected to find its standard library
# without additional command line arguments, so include lib/ here
# in its definition.
ifeq ($(DEB_HOST_GNU_TYPE), $(DEB_BUILD_GNU_TYPE))
	PERL_TO_USE = $(srcdir)/build-static/perl -I $(srcdir)/build-static/lib
else
	PERL_TO_USE = $(shell which perl)
	export TIME_HIRES_DONT_RUN_PROBES=1
endif

# this gets prepended to the patch names in patchlevel.h
patchprefix = DEBPKG:

# control file substitutions
subst_upstream = -VUpstream-Version=$(DEB_VERSION_UPSTREAM)
subst_next_upstream = -VNext-Upstream-Version=$(nextversion)

ifeq (,$(perlabi))
subst_perlapi  = -Vperlapi:Provides="`$(PERL_TO_USE) debian/mkprovides`"
else
subst_perlapi  = -Vperlapi:Provides="perlapi-$(perlabi)"
endif

# for cpan/Compress-Raw-Zlib
export BUILD_ZLIB=False
export ZLIB_INCLUDE=/usr/include
export ZLIB_LIB=/usr/lib

# for cpan/Compress-Raw-Bzip2
export BUILD_BZIP2=0
export BZIP2_INCLUDE=/usr/include
export BZIP2_LIB=/usr/lib

# disable tests declared upstream as not useful for packaging
export PERL_BUILD_PACKAGING=1

# Debian Bug #896827: riscv64 currently has excessively slow build hosts
DEB_BUILD_ARCH          ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)
ifeq (riscv64,$(DEB_BUILD_ARCH))
export PERL_TEST_TIME_OUT_FACTOR=3
endif

# not really quite this simple :)
%:
	dh $@

verify-configure-stamp: update-configure-stamp
	if ! diff -u Configure.orig Configure || ! diff -u config_h.SH.orig config_h.SH; then \
	    echo "Unexpected modifications after rebuilding Configure, aborting" 1>&2; \
	    echo '(See "A special note on patching Configure" in README.source for details)' 1>&2; \
	    exit 1; \
	fi
	touch $@

# manual target documented in README.source
update-configure: update-configure-stamp
	# sanity check
	test -f Configure

update-configure-stamp:
	if [ -e regen-configure/bin ]; then \
	    echo "regen-configure/bin is present but should have been repacked away, aborting" 1>&2; \
	    echo '(see README.source and https://bugs.debian.org/992200)' 1>&2; \
	    exit 1; \
	fi
	mv Configure Configure.orig
	mv config_h.SH config_h.SH.orig
	ln -s regen-configure/U
	ln -s regen-configure/.package
	metaconfig -X .metaconf-exclusions.txt -m -L regen-configure/dist
	./Porting/config_h.pl # reorders config_h.SH
	chmod +x config_h.SH Configure # placate t/porting/exec-bit.t
	touch $@

perl-config-%-stamp: verify-configure-stamp
	$(SHELL) debian/config.debian --$*
	touch $@

perl-build-%-stamp: perl-config-%-stamp $(patchlevel)
	ln -s ../$(patchlevel) build-$*/$(pathlevel)
	if [ "$*" = "shared" ]; then \
	  ln -s libperl.so.$(fullversion) build-$*/libperl.so.$(version); \
	  ln -s libperl.so.$(version) build-$*/libperl.so; \
	  dh_auto_build --builddirectory=build-$* -- SHRPLDFLAGS='$$(LDDLFLAGS) -Wl,-soname,libperl.so.$(version)'; \
	elif [ "$*" = "debug" ]; then \
	  dh_auto_build --builddirectory=build-$* -- perl; \
	else \
	  dh_auto_build --builddirectory=build-$*; \
	fi
	touch $@

perl-test-%-stamp: perl-build-%-stamp
	dh_auto_test --builddirectory=build-$*
	touch $@

$(patchlevel): debian/patches/series
	$(checkdir)
	test -f $<	# maintainer sanity check
	debian/gen-patchlevel -p $(patchprefix) -v $(DEB_VERSION) $< > $@
	# patchlevel_date in perlbug is extracted from patchlevel.h mtime, let's set
	# it to highest mtime of Debian patches for build reproducibility
	patchdate=`debian/gen-patchlevel -d`; touch --date="@$$patchdate" patchlevel.h

override_dh_auto_configure-indep: perl-config-shared-stamp

override_dh_auto_configure-arch: $(addsuffix -stamp,$(addprefix perl-config-,$(flavours)))

override_dh_auto_build-indep: perl-build-shared-stamp

override_dh_auto_build-arch: $(addsuffix -stamp,$(addprefix perl-build-,$(flavours)))

override_dh_auto_test-indep: perl-test-shared-stamp

override_dh_auto_test-arch: perl-test-static-stamp perl-test-shared-stamp

perl-install-shared-stamp:
	dh_auto_install --builddirectory=build-shared --destdir=debian/tmp
	touch $@

override_dh_auto_install-indep: perl-install-shared-stamp
	# move to full version; symlinks to match @INC will be created after dh_install
	mv $(tmp)/$(share) $(tmp)/usr/share/perl/$(fullversion)

override_dh_auto_install-arch: perl-install-shared-stamp
	# store probed configuration to support cross builds
	for flavour in $(flavours); do \
	  install -D -m 644 build-$$flavour/config.sh $(configdata)/config.sh.$$flavour; \
	  $(PERL_TO_USE) -pi -e 's@\Q$(CURDIR)\E@/dummy/build/dir@g' $(configdata)/config.sh.$$flavour; \
	  gzip -n $(configdata)/config.sh.$$flavour; \
	done

ifeq (,$(perlabi))
else
	# support \$Config{debian_abi} (see the Debian Perl policy)
	  sed -i -e "/version =>/i\    debian_abi => '$(perlabi)'," \
	         $(tmp)/$(lib)/Config.pm
endif

	# avoid exposing the point release in libperl names, to
	# avoid unnecessary breakage
	# see #759989
	  for file in Config.pm Config_heavy.pl; do \
	    $(PERL_TO_USE) -i -p \
	      -e 's/^(libperl='\''libperl\.so)\.$(fullversion)'\''/$$1.$(version)'\''/' \
	      $(tmp)/$(lib)/$$file; \
	  done

	# remove dpkg-buildflags effects from %Config
	# see #657853
	  for file in Config.pm Config_heavy.pl; do \
	    if which dpkg-buildflags >/dev/null 2>&1; then \
	        for flag in $(shell dpkg-buildflags --get CPPFLAGS) \
	                 $(shell dpkg-buildflags --get CFLAGS) \
	                 ; do \
	                case "$$flag" in -fstack-protector) ;; \
	                     *) export flag; $(PERL_TO_USE) -i -pe "/^(cc|cpp)flags/ and \
	                          s/(['\s])\Q\$$ENV{flag}\E(['\s])/\1\2/ and s/  +/ /" \
	                    $(tmp)/$(lib)/$$file ;; \
	            esac; done; \
	        for flag in $(shell dpkg-buildflags --get LDFLAGS); do \
	                export flag; $(PERL_TO_USE) -i -pe "/^ld(dl)?flags/ and \
	                          s/(['\s])\Q\$$ENV{flag}\E(['\s])/\1\2/ and s/  +/ /" \
	                  $(tmp)/$(lib)/$$file ; \
	        done; \
	    fi; \
	  done

	# convert required header files
	-cd /usr/include; \
		$(srcdir)/debian/fake-config $(srcdir)/$(tmp)/$(lib) $(PERL_TO_USE) \
		&$(srcdir)/build-static/utils/h2ph -a -d $(srcdir)/$(tmp)/$(lib) \
		`cat $(srcdir)/debian/headers`

	# fix up generated headers
	$(PERL_TO_USE) debian/fixheaders $(tmp)/$(lib)

	# remove references to the build directory in some files
	find $(tmp) \
	  -path '*/CORE/config.h' -or -path '*/Config_heavy.pl' | \
	    xargs -tr $(PERL_TO_USE) -pi -e 's@\Q$(CURDIR)\E@/dummy/build/dir@g'

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	# Verify that the headers are usable
	for ph in `< debian/headers sed -e 's/\.h$$/.ph/'`; do \
	        if [ ! -f $(tmp)/$(lib)/$$ph ]; then \
			echo "$$ph: missing"; else \
			echo $$ph | $(PERL_TO_USE) debian/check-require $(tmp) \
				|| exit 1; \
		fi; \
	done
endif
	# remove some cruft
	rm -f $(tmp)/$(lib)/.packlist

	# relocate perl library
	mv $(tmp)/$(lib)/CORE/libperl.so.$(fullversion) $(tmp)/usr/lib/$(archtriplet)

	# replaced by dh_link symlinks
	rm -f $(tmp)/$(lib)/CORE/libperl.so $(tmp)/$(lib)/CORE/libperl.so.$(version)

	# move to full version; symlinks to match @INC will be created after dh_install
	mv $(tmp)/$(lib) $(tmp)/usr/lib/$(archtriplet)/perl/$(fullversion)

execute_after_dh_install-indep:
	ln -s $(fullversion) debian/perl-modules-$(version)/$(share)

	# POD files are in perl-doc
	rm -f debian/perl-modules-$(version)/usr/share/perl/$(fullversion)/pod/*

	# except perldiag.pod which is needed by the diagnostics module
	mv debian/perl-doc/usr/share/perl/$(fullversion)/pod/perldiag.pod \
	   debian/perl-modules-$(version)/usr/share/perl/$(fullversion)/pod

execute_after_dh_install-arch:
	ln -s $(fullversion) debian/libperl$(version)/$(lib)

	# switch man extensions to 1p and 3pm for vendor module installs
	for instdir in debian/libperl$(version)/$(lib) debian/perl-base/$(baselib); do \
	  for file in Config.pm Config_heavy.pl; do \
	    $(PERL_TO_USE) -i -p \
	      -e 's/^(man1ext=).*/$$1'\''1p'\''/;' \
	      -e 's/^(man3ext=).*/$$1'\''3pm'\''/;' \
	      $$instdir/$$file; \
	  done; \
	done

	# put version+arch -specific binaries in libperl5.xx
	for prefix in perl cpan; do \
	  mv debian/libperl$(version)/usr/bin/$$prefix \
	     debian/libperl$(version)/usr/bin/$${prefix}$(version)-$(archtriplet); \
	done
	sed -i "1 s,/usr/bin/perl,/usr/bin/perl$(version)-$(archtriplet)," \
	   debian/libperl$(version)/usr/bin/cpan$(version)-$(archtriplet)

	# perl itself goes in perl-base
	ln debian/perl-base/usr/bin/perl debian/perl-base/usr/bin/perl$(fullversion)
	rm debian/perl/usr/bin/perl debian/perl/usr/bin/perl$(fullversion)

	# debugging version has a different name
	mv debian/perl-debug/usr/bin/perl debian/perl-debug/usr/bin/debugperl

	# put a perldoc stub in the perl package
	mv debian/perl/usr/bin/perl.perldoc debian/perl/usr/bin/perldoc

	# rip pod out of -base modules
	$(PERL_TO_USE) debian/stripdoc debian/perl-base

ifeq (,$(findstring nocheck,$(DEB_BUILD_OPTIONS)))
	# Verify that perl-base stays self contained

	( cd $(srcdir)/debian/perl-base && find . -name '*.p[hlm]' ) | \
	  $(PERL_TO_USE) $(srcdir)/debian/check-require $(srcdir)/debian/perl-base
endif

execute_after_dh_installman-arch:
	# put version+arch -specific manpages in libperl5.xx
	for prefix in perl cpan; do \
	  mv debian/libperl$(version)/usr/share/man/man1/$$prefix.1 \
	     debian/libperl$(version)/usr/share/man/man1/$${prefix}$(version)-$(archtriplet).1; \
	done

override_dh_strip:
	dh_strip --no-package=perl-debug --dbg-package=perl-debug

# subst_perlapi uses build-static/perl, not present in arch-indep builds
override_dh_gencontrol-indep:
	dh_gencontrol -- $(subst_upstream) $(subst_next_upstream)

override_dh_gencontrol-arch:
	dh_gencontrol -- $(subst_upstream) $(subst_next_upstream) $(subst_perlapi)

override_dh_installdocs-arch:
	dh_installdocs --package=perl-debug --link-doc=perl
	dh_installdocs --package=perl-base --doc-main-package=perl
	dh_installdocs --remaining-packages
