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

debian/build/config.status: configure
	dh_testdir

	mkdir debian/build; \
	cd debian/build; \
	CFLAGS="$(CFLAGS)" ../../configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --sysconfdir=/etc --libexecdir=/usr/lib --localstatedir=/var --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info --enable-dchroot --enable-dchroot-dsa

build: build-stamp

build-stamp: debian/build/config.status
	dh_testdir

	cd debian/build; \
	$(MAKE) all

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -rf debian/build
	rm -rf debian/install
	rm -f build-stamp

ifneq "$(wildcard /usr/share/misc/config.sub)" ""
	cp -f /usr/share/misc/config.sub scripts/config.sub
endif
ifneq "$(wildcard /usr/share/misc/config.guess)" ""
	cp -f /usr/share/misc/config.guess scripts/config.guess
endif

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd debian/build; \
	$(MAKE) check

	cd debian/build; \
	$(MAKE) install DESTDIR=$(CURDIR)/debian/install
	dh_install

	mkdir -p $(CURDIR)/debian/dchroot/usr/share/lintian/overrides
	mkdir -p $(CURDIR)/debian/dchroot-dsa/usr/share/lintian/overrides
	mkdir -p $(CURDIR)/debian/schroot/usr/share/lintian/overrides
	cp $(CURDIR)/debian/dchroot.lintian-overrides $(CURDIR)/debian/dchroot/usr/share/lintian/overrides/dchroot
	cp $(CURDIR)/debian/dchroot-dsa.lintian-overrides $(CURDIR)/debian/dchroot-dsa/usr/share/lintian/overrides/dchroot-dsa
	cp $(CURDIR)/debian/schroot.lintian-overrides $(CURDIR)/debian/schroot/usr/share/lintian/overrides/schroot

# 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_installchangelogs ChangeLog
	dh_installdocs
	rm -rf $(CURDIR)/debian/dchroot/usr/share/doc/dchroot
	ln -sf schroot $(CURDIR)/debian/dchroot/usr/share/doc/dchroot
	rm -rf $(CURDIR)/debian/dchroot-dsa/usr/share/doc/dchroot-dsa
	ln -sf schroot $(CURDIR)/debian/dchroot-dsa/usr/share/doc/dchroot-dsa
	dh_installexamples
	dh_installman
	dh_installinit --no-start --update-rcd-params='start 75 S .'
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	chmod 4755 $(CURDIR)/debian/dchroot/usr/bin/dchroot
	chmod 4755 $(CURDIR)/debian/dchroot-dsa/usr/bin/dchroot-dsa
	chmod 4755 $(CURDIR)/debian/schroot/usr/bin/schroot
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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