#!/usr/bin/make -f
# debian/rules for the obsolete Debian GNU Libtool 1.4 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_GNU_TYPE)
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
ifeq (ia64, $(DEB_HOST_ARCH))
	make_check = no
endif

# Tests fail
ifeq (hppa, $(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

	./configure --prefix=/usr --disable-ltdl-install $(confflags) \
		CFLAGS="$(CFLAGS)"
	$(MAKE)

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

#	These go into the libtool1.4-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 ./depdemo.tar.gz depdemo

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/libtool1.4

	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

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

	cd libltdl && \
		aclocal -I .. && \
		autoheader2.50 && \
		automake -a -c && \
		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 mdemo; do \
		cd $$sub; \
		aclocal -I ..; \
		automake --foreign -a -c; \
		autoconf2.50; \
		cd ..; \
	done

	rm -f ltmain.sh
	autoconf2.50

#	Make sure we include ltmain.sh in the package.
	./configure --prefix=/usr --disable-ltdl-install $(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 libltdl mdemo \
		   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
	rm -rf debian/tmp/usr/share/libtool/libltdl
	rm -f debian/tmp/usr/share/aclocal/ltdl.m4

	dh_installdocs -i
	dh_installinfo -plibtool1.4-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

	dh_installdocs -plibtool1.4
	dh_installman -plibtool1.4 debian/libtool.1 debian/libtoolize.1
	dh_installchangelogs -plibtool1.4 ChangeLog
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -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
