#!/usr/bin/make -f
# $Id: rules,v 1.13 2003/04/23 18:43:28 nold Exp $

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

# This is the debhelper compatability version to use.
export DH_COMPAT=4

export DEB_GLAME_PREFIX=/usr
export DEB_GLAME_INFODIR=/usr/share/info

export DEB_HOST_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
export DEB_BUILD_GNU_TYPE ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

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

configure: configure-stamp
configure-stamp:
	dh_testdir

	# Make sure we use the latest autotools helpers.
	if test -f config.sub; then \
		mv config.sub config.sub.debsave; \
	fi
	if test -f config.guess; then \
		mv config.guess config.guess.debsave; \
	fi
	ln -sf /usr/share/misc/config.sub .
	ln -sf /usr/share/misc/config.guess .

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS="-g -O0" ./configure $(confflags) \
		--prefix=$(DEB_GLAME_PREFIX) \
		--infodir=$(DEB_GLAME_INFODIR)
else
	CFLAGS="-g -O2" ./configure $(confflags) \
		--prefix=$(DEB_GLAME_PREFIX) \
		--infodir=$(DEB_GLAME_INFODIR)
endif

	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir

	$(MAKE)
	/usr/bin/docbook-to-man debian/cglame.sgml > debian/cglame.1
	/usr/bin/docbook-to-man debian/glame.sgml > debian/glame.1
	/usr/bin/docbook-to-man debian/glame-convert.sgml > debian/glame-convert.1

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	rm -f debian/cglame.1 debian/glame.1 debian/glame-convert.1

	-$(MAKE) distclean

	if test -L config.sub -a -e config.sub.debsave; then \
		rm config.sub && mv config.sub.debsave config.sub; \
	fi
	if test -L config.guess -a -e config.guess.debsave; then \
		rm config.guess && mv config.guess.debsave config.guess; \
	fi
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(MAKE) install DESTDIR=`pwd`/debian/glame/
	# Kludge. The .sh suffix makes for a rather undesirable name of
	# the man page. Furthermore, it's really not needed.
	mv `pwd`/debian/glame/usr/bin/glame-convert.sh `pwd`/debian/glame/usr/bin/glame-convert
	# Clean up unwanted static lib + lib metadata. Waaaay
	# easier than teaching libtool about it. :/
	-find `pwd`/debian/glame/usr/lib -name "*.a" -exec rm {} \;
	-find `pwd`/debian/glame/usr/lib -name "*.la" -exec rm {} \;
	# ltdl insists on installing that one.
	-rmdir `pwd`/debian/glame/usr/include/

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installdocs
	dh_installmenu
	dh_installman
	dh_installinfo
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps -Xdebian/glame/usr/lib/glame/ -- \
		-dRecommends `find debian/glame/usr/lib/glame/ -name "*.so" -type f` -dDepends
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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