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

# Use debhelper v4 features.
export DH_COMPAT=4


# libltdl needs to conform to policy
CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

# Build libltdl the right way (tm)
DEB_HOST_ARCH      ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
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

# Tests fail with "Illegal instruction", need a ia64 guru
ifeq (ia64, $(DEB_HOST_ARCH))
	make_check = no
endif

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


build: build-stamp
build-stamp: configure
	dh_testdir

#	Fix some permissions
	chmod a+x tests/mdemo2-*.test

	./configure --prefix=/usr $(confflags) CFLAGS="$(CFLAGS)"
	$(MAKE)

#	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 -e '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

#	This generated HTML page goes into libtool-doc.
	cd doc && makeinfo libtool.texi
	cd doc && texi2html -monolithic libtool.texi

#	These go into the libtool-doc `examples' directory.
	GZIP=--best tar zcf ./cdemo.tar.gz cdemo
	GZIP=--best tar zcf ./demo.tar.gz demo
	GZIP=--best tar zcf ./mdemo.tar.gz mdemo
	GZIP=--best tar zcf ./mdemo2.tar.gz mdemo2
	GZIP=--best tar zcf ./pdemo.tar.gz pdemo
	GZIP=--best tar zcf ./depdemo.tar.gz depdemo
	GZIP=--best tar zcf ./f77demo.tar.gz f77demo
	GZIP=--best tar zcf ./tagdemo.tar.gz tagdemo

ifeq ($(make_check), yes)
#	Now make sure it works
	$(MAKE) check
endif

	touch build-stamp

clean: configure
	dh_testdir
	dh_testroot
	rm -f build-stamp

	-$(MAKE) -k distclean
	rm -f doc/*.html *.tar.gz
	rm -f config.cache config.log config.status
	rm -f */config.cache */config.log */config.status
	rm -f conftest* */conftest*
	rm -rf autom4te.cache */autom4te.cache
	rm -rf debian/libtool

	dh_clean

# This isn't the rule to *run* the configure script (although it does),
# this is the rule to *create* it along with all the other autotools debris.
#
# Please don't file bugs because of missing build dependencies on the
# commands below.  This rule is intended for the package maintainer only
# and as all Debian libtool packages are shipped with all the gunk
# this rule won't get run unless you either delete your "configure" script
# or run "debian/rules configure" manually.  In either case, you should
# know what you're doing.
configure:
	touch ltmain.sh libltdl/ltmain.sh

	aclocal -I .
	automake -a -c
	cp -f /usr/share/misc/config.{guess,sub} .

	cd libltdl && \
		aclocal -I .. && \
		autoheader2.50 && \
		automake -a -c && \
		cp -f /usr/share/misc/config.{guess,sub} . && \
		autoconf2.50

#	These aren't &&d together because later versions of automake
#	tend to error with an exit code after making Makefile.in
#	because they break the rules a bit.
	for sub in demo cdemo depdemo f77demo mdemo mdemo2 pdemo tagdemo; do \
		cd $$sub; \
		aclocal -I ..; \
		autoheader2.50; \
		automake --foreign -a -c; \
		autoconf2.50; \
		cd ..; \
	done

	rm -f ltmain.sh libltdl/ltmain.sh
	autoconf2.50

#	Make sure we include ltmain.sh in the package.
	./configure --prefix=/usr $(confflags)
	$(MAKE) ltmain.sh
	rm -f acinclude.m4 */acinclude.m4
	
# Make it as clean as svn can make it.  Reverses "debian/rules configure"
# provided you've just checked this out of the subversion repository.
svnclean: clean
	for dir in . cdemo debian demo depdemo depdemo/l* doc f77demo libltdl \
		   mdemo mdemo2 pdemo tagdemo tests; do \
		(cd $$dir && rm -rf `svn propget svn:ignore`); \
	done

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) prefix=`pwd`/debian/tmp/usr install

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i

	dh_movefiles -i

#	These get created during installation
	cd debian/tmp/usr/share/libtool && rm -f config.guess config.sub

	dh_installdocs -i
	dh_installinfo -plibtool-doc
	dh_installexamples -i
	dh_installchangelogs -i ChangeLog
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a

	dh_movefiles -a

#	Remove the extra copy of COPYING.LIB, and just link to shared one.
	cd debian/libtool/usr/share/libtool/libltdl && \
		rm COPYING.LIB && \
		ln -sf ../../common-licenses/LGPL COPYING.LIB

	dh_installdocs -plibtool
	dh_installdocs -plibltdl3
	mkdir -p debian/libltdl3-dev/usr/share/doc
	cd debian/libltdl3-dev/usr/share/doc && ln -sf libltdl3 libltdl3-dev
	dh_installman -plibtool debian/libtool.1 debian/libtoolize.1
	dh_installchangelogs -plibtool ChangeLog
	dh_installchangelogs -plibltdl3 ChangeLog
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a -V 'libltdl3 (>= 1.5.2-2)'
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

binary: binary-indep binary-arch
.PHONY: build clean svnclean binary-indep binary-arch binary install
