#!/usr/bin/make -f

#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 )
DEB_BUILD_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS  )
ARCH                := $(shell dpkg-architecture -qDEB_BUILD_ARCH     )

# compiler flags, as described in Policy
CFLAGS += -Wall -Wshadow -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  CFLAGS += -O0
else
  CFLAGS += -O2
endif
export CFLAGS

# some useful variables
TOPDIR      :=$(shell pwd)
PKGNAME     :=$(word 2,$(shell dpkg-parsechangelog | grep ^Source  ) )
FULLVERSION :=$(word 2,$(shell dpkg-parsechangelog | grep ^Version ) )
UPSTREAMVER :=$(shell echo $(FULLVERSION) | sed 's/-[0-9.]\+$$//' | sed 's/^0~//' )
BUILDDIR    :=build/


# not really unpack, but just links everything in build/
unpack: $(BUILDDIR)/COPYING
$(BUILDDIR)/COPYING:
	# unpack
	[ -d "$(BUILDDIR)" ] || mkdir "$(BUILDDIR)"
	/bin/sh debian/lndir.sh . $(BUILDDIR)
	rm -r $(BUILDDIR)/build
	rm -r $(BUILDDIR)/debian


build: build-arch build-indep
build-arch: build-stamp
build-indep:
build-stamp: $(BUILDDIR)/COPYING
	dh_testdir

	# config.sub and config.guess update
	rm -f $(BUILDDIR)/tools/config.sub
	rm -f $(BUILDDIR)/tools/config.guess
	cp /usr/share/misc/config.sub   $(BUILDDIR)/tools/config.sub
	cp /usr/share/misc/config.guess $(BUILDDIR)/tools/config.guess

	# configure
	cd $(BUILDDIR) && \
	./configure --host=$(DEB_HOST_GNU_TYPE) \
	            --build=$(DEB_BUILD_GNU_TYPE) \
	            --prefix=/usr \
	            --mandir=\$${prefix}/share/man \
		    --infodir=\$${prefix}/share/info \
		    --bindir=\$${prefix}/sbin

	# build
	cd $(BUILDDIR) && \
	$(MAKE)

	touch build-stamp

clean:
	dh_testdir
	-rm -rf $(BUILDDIR)
	-rm -f *-stamp
	dh_clean

install: build-stamp
	dh_testdir
	dh_testroot

	dh_clean -k

	dh_installdirs

	cd $(BUILDDIR) && \
	$(MAKE) install prefix=$(TOPDIR)/debian/quotatool/usr


binary-indep:
	# nothing to do

binary-arch: build-stamp install
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_strip
	dh_compress
	dh_fixperms
	dh_link
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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