#!/usr/bin/make -f
# debian/rules for the Debian GNU Libtool package.
# Copyright © 2003, 2004 Scott James Remnant <scott@netsplit.com>

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

# In case they are dropped
SERIAL=4441
VERSION=2.5.4

include /usr/share/dpkg/architecture.mk
DEB_BUILD_MAINT_OPTIONS:= hardening=+all
DEB_CFLAGS_MAINT_PREPEND := -Wall
DPKG_EXPORT_BUILDFLAGS := 1
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk

# Always use triplet-prefixed tools with gfortran-for-host.
ifeq ($(origin FC),default)
export FC := $(DEB_HOST_GNU_TYPE)-gfortran
endif
export F77 ?= $(FC)

ifneq ($(filter nodoc,$(DEB_BUILD_PROFILES)),)
  # Convenience variable to avoid repeating $(filter nodoc,...)
  DEB_BUILD_PROFILE_nodoc = nodoc
endif


AM_VERSION=$(shell aclocal --version | sed 's/.* \([0-9]\+\.[0-9]\+\)\(\.[0-9]\+\)\?$$/\1/;q')

ifeq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
	confflags += --build=$(DEB_HOST_GNU_TYPE)
	make_check = yes
else
	confflags += --build=$(DEB_BUILD_GNU_TYPE) --host=$(DEB_HOST_GNU_TYPE)
	make_check = no
endif

# Takes too long
#ifeq (m68k, $(DEB_HOST_ARCH))
#	make_check = no
#endif

ifdef DEB_BUILD_PROFILE_nodoc
	export HELP2MAN = /bin/true
	export MAKEINFO = /bin/true
endif

%:
	dh $@

override_dh_auto_clean:
	-mv libtoolize.in~ libtoolize.in
	-mv build-aux/ltmain.in~ build-aux/ltmain.in
	[ ! -f Makefile ] || dh_auto_clean

override_dh_autoreconf:
	dh_autoreconf debian/rules -- override_dh_autoreconf-script
override_dh_autoreconf-script:
	# dot files may have been lost in git
	echo ${SERIAL} > .serial || true
	echo ${VERSION} > .version || true
	echo ${VERSION} > .tarball-version || true

	touch README-release
	./bootstrap --force --no-git --skip-po --gnulib-srcdir=/usr/share/gnulib/ --copy

override_dh_auto_configure:
	sed -i -e "s/\$$scriptversion.*/\$$scriptversion Debian-$(DEB_VERSION)/" libtoolize.in
	sed -i -e "s/\$$scriptversion.*/\$$scriptversion Debian-$(DEB_VERSION)/" build-aux/ltmain.in
	sed -i -e 's/^VERSION.*/VERSION="@VERSION@ Debian-$(DEB_VERSION)"/' build-aux/ltmain.in

	CONFIG_SHELL=/bin/bash /bin/bash ./configure \
		--prefix=/usr \
		--libdir=\$${prefix}/lib/$(DEB_HOST_MULTIARCH) \
		$(confflags) \
		GREP=/bin/grep \
		SED=/bin/sed

execute_after_dh_auto_build:
#	The system libtool script in Debian must be able to support
#	invoking gcc as cc
	echo '# ### BEGIN LIBTOOL TAG CONFIG: BINCC' >> libtool
	sed -n -e '/^# ### BEGIN LIBTOOL CONFIG/,/^# ### END LIBTOOL CONFIG/p' < libtool \
		| grep -B 2 -A 1 -e '^LTCC=' -e '^CC=' \
		| sed '/^--/d;s/gcc/cc/g' >> libtool
	echo '# ### END LIBTOOL TAG CONFIG: BINCC' >> libtool
	echo >> libtool

#	The system libtool script in Debian must be able to support
#	invoking g++ both by the g++ and c++ names.
	sed -n -e '/^# ### BEGIN LIBTOOL TAG CONFIG: CXX$$/,/^# ### END LIBTOOL TAG CONFIG: CXX$$/p' < libtool \
		| sed -e 's/CONFIG: CXX/CONFIG: BINCXX/g' \
		      -e 's/g++/c++/g' >> libtool
	echo >> libtool

#	Add our BINCC and BINCXX tags
	sed -e 's/^\(available_tags\)=\"\(.*\)\"/\1=\"\2 BINCC BINCXX\"/' \
		< libtool > libtool.tags
	mv libtool.tags libtool

#	Don't hardcode gcc path information, and don't use nostdlib
	sed -e 's/^\(predep_objects\)=.*/\1=""/' \
	    -e 's/^\(postdep_objects\)=.*/\1=""/' \
	    -e 's/^\(compiler_lib_search_path\)=.*/\1=""/' \
	    -e 's:^\(sys_lib_search_path_spec\)=.*:\1="/lib/ /usr/lib/ /usr/X11R6/lib/ /usr/local/lib/":' \
	    -e 's/^\(archive_cmds=\".*\) -nostdlib /\1 /' \
	    -e 's/^\(archive_expsym_cmds=\".*\) -nostdlib /\1 /' \
	    < libtool > libtool.gcc-indep
	mv libtool.gcc-indep libtool

#	Make libtool executable again
	chmod 755 libtool

ifndef DEB_BUILD_PROFILE_nodoc
	cd doc && makeinfo libtool.texi
	cd doc && makeinfo --html --no-split libtool.texi
endif

override_dh_auto_test:
ifeq ($(make_check), yes)
	dh_auto_test || touch tests-failed
	-cat test-suite.log
	-cat tests/testsuite.log
	@test ! -f tests-failed
endif

execute_after_dh_auto_install-arch:
	# The file-prefix-map/debug-prefix-map contain the build path,
	# which results in unreproducible builds. Strip these arguments
	# out of the manpage.
	sed -i -e "s, ....-fdebug.-prefix.-map=.*=\. , ,g" \
		-e "s, ....-ffile.-prefix.-map=.*=\. , ,g" \
		debian/tmp/usr/share/man/man1/libtool.1

execute_after_dh_install-indep:
	chmod +x debian/libtool/usr/share/libtool/build-aux/ltmain.sh

	# Sanitize /usr-merge path builds
	( set -e ; for f in \
		debian/libtool/usr/bin/libtoolize ; do sed \
			-e 's%/usr/bin/grep%/bin/grep%g'  \
			-e 's%/usr/bin/sed%/bin/sed%g'  \
			-e 's%/usr/bin/dd%/bin/dd%g'  \
			-e 's%${CURDIR}%/build/libtool-$(DEB_VERSION_UPSTREAM)%g'  \
			$$f > debian/tmpff &&  \
		mv debian/tmpff $$f ; \
		done )

execute_after_dh_install-arch:
	# Sanitize /usr-merge path builds
	(set -e ; for f in \
		debian/libtool-bin/usr/share/man/man1/libtool.1 \
		debian/libtool-bin/usr/bin/libtool ; do sed  \
			-e 's%/usr/bin/grep%/bin/grep%g'  \
			-e 's%/usr/bin/sed%/bin/sed%g'  \
			-e 's%/usr/bin/dd%/bin/dd%g'  \
			-e 's%${CURDIR}%/build/libtool-$(DEB_VERSION_UPSTREAM)%g'  \
			$$f > debian/tmpff && \
		mv debian/tmpff $$f ; \
		done )

override_dh_gencontrol:
	dh_gencontrol -plibltdl-dev -- '-Vautomake=automake (>= $(AM_VERSION))'
	dh_gencontrol --remaining-packages

override_dh_installdocs:
	dh_installdocs -plibltdl-dev --link-doc libltdl7
	dh_installdocs --remaining-packages
