#!/usr/bin/make -f
# -*- makefile -*-

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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif

# This target makes sure that timestamps of files are in the
# right order after dpkg-deb has unpacked the package. It prevents
# the upstream Makefile from trying to regenerate the files (which
# would be problematic because we don't build-depend on the appropriate
# tools).
autogen-check: configure bibclean.h
	dh_testdir
	touch configure
	touch bibclean.h

config.status: autogen-check
	CFLAGS="$(CFLAGS)" ./configure \
		--with-pager=/usr/bin/pager --with-fhs-rcfiles \
		--host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) \
                --prefix=/usr --mandir=\$${prefix}/share/man

build: build-arch build-indep

build-stamp: config.status
	$(MAKE)
	$(MAKE) -C ./doc bibclean.dvi
	touch build-stamp

clean: autogen-check
	[ -z "`find -uid 0`" ] || dh_testroot
	rm -f build-stamp
	[ ! -f Makefile ] || $(MAKE) -o config.status -o Makefile.in distclean
	rm -f Makefile
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	$(MAKE) install-exe prefix=$(CURDIR)/debian/bibclean/usr
	mv debian/bibclean/usr/share/bibcleanrc debian/bibclean/etc/
	rm debian/bibclean/usr/bin/bibclean-*

# 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
	echo misc:Depends= > debian/bibclean.substvars
	dh_testdir
	dh_testroot
	dh_installdirs $(LINTIAN)
	dh_installdocs doc/bibclean.dvi
	dh_installman bibclean.man
	dh_installchangelogs ChangeLog
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb
	rm -f debian/bibclean.substvars

binary: binary-indep binary-arch


# build targets as suggested by explanation to lintian warning
build-arch: build-stamp
build-indep: build-stamp

.PHONY: build clean binary-indep binary-arch binary install autogen-check

# The following two rules will should not fire when the Debian source
# package is unpacked, because the two files in question should already
# exist in that case. However, they *will* fire for a clean checkout
# of my cvs repository, e.g. when cvs-buildpackage runs the clean
# target before creating a source package. 
configure:
	dh_testdir
	autoconf2.13

# Special magic to reproduce the same layout as the bibclean.h
# distributed by upstream:
bibclean.h:
	dh_testdir
	make -f Makefile.in AWK=awk NROFF='nroff -rLL=74n' SED=sed bibclean.h
	perl -pi -e's/"  /"/' bibclean.h
