#!/usr/bin/make -f

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

VERSION = $(shell dpkg-parsechangelog | grep ^Version: | sed "s/.* //" | sed "s/-[^-]*$$//")
DATE = $(shell date -d "$$(dpkg-parsechangelog | grep ^Date: | sed -n '/^Date: /s///p')" "+%d %b %Y")

GENFILES =			\
	avg-pkg-build-time.1	\
	sbuild.1		\
	sbuild-setup.7		\
	update-sourcedeps.1	\
	Sbuild/Log.pm

%: %.in
	sed -e 's/\$$Revision: [0-9.]* \$$/$(VERSION)/g' \
	    -e 's/@VERSION@/$(VERSION)/g' \
	    -e 's/@DATE@/$(DATE)/g' <$< >$@

INSTALL=/usr/bin/install
DESTDIR=$(CURDIR)/debian/sbuild

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

configure: configure-stamp
configure-stamp:
	dh_testdir
	touch configure-stamp

build: configure-stamp build-stamp
build-stamp: $(GENFILES)
	dh_testdir
	touch build-stamp

clean:
	dh_testdir
	dh_testroot

	rm -f $(GENFILES)
	rm -f build-stamp configure-stamp

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	$(INSTALL) -m 755 sbuild update-sourcedeps avg-pkg-build-time \
		$(DESTDIR)/usr/bin/
	$(INSTALL) -m 644 Sbuild.pm $(DESTDIR)/usr/share/perl5/
	$(INSTALL) -m 644 Sbuild/Chroot.pm $(DESTDIR)/usr/share/perl5/Sbuild/Chroot.pm
	$(INSTALL) -m 644 Sbuild/Conf.pm $(DESTDIR)/usr/share/perl5/Sbuild/Conf.pm
	$(INSTALL) -m 644 Sbuild/Log.pm $(DESTDIR)/usr/share/perl5/Sbuild/Log.pm
	$(INSTALL) -m 644 Sbuild/Utility.pm $(DESTDIR)/usr/share/perl5/Sbuild/Utility.pm
	ln -s avg-pkg-build-time $(DESTDIR)/usr/bin/avg-pkg-build-space
	$(INSTALL) -m 644 sbuild.conf $(DESTDIR)/usr/share/sbuild/
	$(INSTALL) -m 644 sbuild.conf.local $(DESTDIR)/etc/sbuild/sbuild.conf

	cp -a configs example.sbuildrc $(DESTDIR)/usr/share/doc/sbuild/examples/.
	find bin -maxdepth 1 -type f -exec cp {} $(DESTDIR)/usr/share/sbuild/ \;
	find chroot -maxdepth 1 -type f -exec cp {} $(DESTDIR)/usr/share/sbuild/ \;
	mv $(DESTDIR)/usr/share/sbuild/README-buildd.chroot \
		$(DESTDIR)/usr/share/doc/sbuild/

# Build architecture-independent files here.
binary-indep: build install
	dh_testdir
	dh_testroot
	dh_installdocs debian/cvslogs.old
	dh_installman
	ln -s avg-pkg-build-time.1.gz debian/sbuild/usr/share/man/man1/avg-pkg-build-space.1.gz
	dh_installchangelogs
	dh_compress -Xbuildd.chroot -XREADME-buildd.chroot
	dh_fixperms
	dh_installdeb
	dh_perl
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-dependent files here.
binary-arch: build
# We have nothing to do by default.

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