#!/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_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)

# libltdl needs to conform to policy
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
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

%:
	dh $@ --without=autoreconf

override_dh_auto_clean:
	if [ -f debian/.version~ ]; then mv debian/.version~ .version; fi
	[ ! -f Makefile ] || dh_auto_clean

execute_before_dh_auto_configure:
	dh_assistant restore-file-on-clean \
		aclocal.m4 configure libtoolize.in maint.mk GNUmakefile INSTALL Makefile.in \
		build-aux/announce-gen build-aux/compile build-aux/depcomp \
		build-aux/do-release-commit-and-tag build-aux/gendocs.sh \
		build-aux/gnu-web-doc-update build-aux/gnupload build-aux/install-sh \
		build-aux/ltmain.in build-aux/ltmain.sh build-aux/mdate-sh build-aux/missing \
		build-aux/test-driver build-aux/texinfo.tex build-aux/update-copyright \
		build-aux/useless-if-before-free build-aux/vc-list-files \
		doc/gendocs_template doc/libtool.1 doc/libtool.info doc/libtool.info-1 \
		doc/libtool.info-2 doc/libtoolize.1 doc/stamp-vti doc/version.texi \
		gnulib-tests/Makefile.in gnulib-tests/Makefile.am gnulib-tests/init.sh \
		gnulib-tests/test-init.sh gnulib-tests/test-update-copyright.sh \
		gnulib-tests/test-vc-list-files-cvs.sh gnulib-tests/test-vc-list-files-git.sh \
		libltdl/Makefile.in libltdl/aclocal.m4 libltdl/configure \
		m4/00gnulib.m4 m4/gnulib-common.m4 m4/gnulib-comp.m4 m4/zzgnulib.m4 \
		tests/testsuite
	mv -f .version debian/.version~

	# dot files may have been lost in git
	echo ${SERIAL} > .serial || true
	echo ${VERSION} > .version || true
	echo ${VERSION} > .tarball-version || true

	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

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

override_dh_auto_configure:
	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

	cd doc && makeinfo libtool.texi
	cd doc && makeinfo --html --no-split libtool.texi

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:
	# Create symlinks to the one in autotools-dev
	rm -f debian/libtool/usr/share/libtool/build-aux/config.guess
	ln -s ../../misc/config.guess debian/libtool/usr/share/libtool/build-aux
	rm -f debian/libtool/usr/share/libtool/build-aux/config.sub
	ln -s ../../misc/config.sub debian/libtool/usr/share/libtool/build-aux

	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 )

execute_before_dh_gencontrol:
	echo 'automake=automake (>= ${AM_VERSION})' > debian/libltdl-dev.substvars

override_dh_installdocs:
	dh_installdocs -plibtool -plibtool-doc -plibtool-bin -plibltdl7
	dh_installdocs -plibltdl-dev --link-doc libltdl7
