#!/usr/bin/make -f
include /usr/share/dpkg/default.mk
DEB_VERSION_EPOCH	= $(call dpkg_late_eval,DEB_VERSION_EPOCH,echo '$(DEB_VERSION)' | sed -e 's/^\([0-9]*:\).*/\1/' -e 's/://')

export DEB_BUILD_MAINT_OPTIONS	= hardening=+all
export DH_VERBOSE		= 1

binutils_source_name	= binutils-source
binutils_source_version	!= dpkg-query -f'$${source:Upstream-Version}' -W $(binutils_source_name)
binutils_source_tar	!= dpkg-query -L $(binutils_source_name) | grep 'binutils-[^/]*\.tar.*'
binutils_source_dir	= src
build_using		!= dpkg-query -f '$${source:Package} (= $${source:Version})' -W $(binutils_source_name)
target			= m68hc11
enable_targets		= m68hc11,m68hc12
pkg_version		= $(DEB_VERSION_EPOCH):$(binutils_source_version)+$(DEB_VERSION_UPSTREAM)

%:
	mkdir --parent "$(binutils_source_dir)"
	dh $@ --sourcedirectory="$(binutils_source_dir)" --builddirectory

execute_before_dh_update_autotools_config:
	tar \
		--extract \
		--directory="$(binutils_source_dir)" \
		--strip-components=1 \
		--file="$(binutils_source_tar)"
	cd "$(binutils_source_dir)"; QUILT_PATCHES=../debian/patches quilt push -a
	rm --force --recursive $(binutils_source_dir)/zlib

override_dh_autoreconf:
	if ! echo $(DEB_BUILD_OPTIONS) | grep -q terse; \
	then \
		AUTORECONF_OPTS='-v'; \
	fi; \
	AUTORECONF_DIRS=`cd $(binutils_source_dir) && find . ! -path './.pc/*' \( -name configure.ac -o -name configure.in \)`; \
	dh_autoreconf autoreconf2.69 -- \
		--force \
		--include=`pwd`/$(binutils_source_dir)/config \
		$$AUTORECONF_OPTS \
		$$AUTORECONF_DIRS

override_dh_auto_configure:
	DH_COMPAT=10 dh_auto_configure -- \
		--target=$(target) \
		--libdir="\$${prefix}/lib/$(target)" \
		--with-bugurl="https://www.debian.org/Bugs/" \
		--with-system-zlib \
		--with-pkgversion="$(pkg_version)" \
		--enable-deterministic-archives \
		--enable-targets=$(enable_targets)

override_dh_auto_install:
	dh_auto_install --destdir=debian/tmp

override_dh_gencontrol:
	dh_gencontrol -- -v$(pkg_version) -VBuilt-Using="$(build_using)"
