#!/usr/bin/make -f

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

DEB_HOST_ARCH  := $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_BUILD_ARCH := $(shell dpkg-architecture -qDEB_BUILD_ARCH)

VERSION := $(shell dpkg-parsechangelog | perl -ne '/^Version:\s+(\S+)-[^-]+$$/ && print $$1;')

include debian/rules.defs

setup: $(STAMPS_DIR)/setup-base
$(STAMPS_DIR)/setup-base: debian/control $(BUILD_DIR) $(STAMPS_DIR)
	dh_testdir
	$(MAKE) -f debian/rules.gen setup-$(DEB_HOST_ARCH)
	touch $@

build: debian/control $(STAMPS_DIR)/build-base
$(STAMPS_DIR)/build-base: $(STAMPS_DIR)/setup-base
	dh_testdir
	$(MAKE) -f debian/rules.gen build-$(DEB_HOST_ARCH)
	touch $@

$(BUILD_DIR) $(STAMPS_DIR):
	@[ -d $@ ] || mkdir $@

maintainerclean:
	rm -f debian/control* debian/rules.gen debian/xen-hypervisor-* debian/xen-utils-*
	rm -rf $(filter-out .svn debian, $(wildcard * .[^.]*))

clean: debian/control
	dh_testdir
	rm -rf $(BUILD_DIR) $(STAMPS_DIR)
	dh_clean

binary-indep:
	dh_testdir
	$(MAKE) -f debian/rules.gen binary-indep

binary-arch:
	dh_testdir
	$(MAKE) -f debian/rules.gen binary-arch-$(DEB_HOST_ARCH)

binary:	binary-indep binary-arch

DIR_ORIG = ../orig/xen-$(MAJOR)-$(VERSION)

orig: $(DIR_ORIG)
	rsync --delete --exclude debian --exclude .svn --link-dest=$(DIR_ORIG)/ -a $(DIR_ORIG)/ .

# This is to make dpatch-edit-patch work
unpatch: clean                                 

CONTROL_FILES += debian/changelog $(wildcard debian/templates/control.*) 
CONTROL_FILES += $(wildcard debian/arch/defines) $(wildcard debian/arch/*/defines)
GENCONTROL = $(__MODULES_DIR)gencontrol.py
debian/control debian/rules.gen: $(CONTROL_FILES)
	if [ -f debian/control ] && [ -f debian/control.md5sum ] && [ -f debian/rules.gen ]; then \
		if md5sum $^ | diff - debian/control.md5sum > /dev/null; then true; else \
			$(MAKE) -f debian/rules debian/control-real; \
		fi \
	else \
		$(MAKE) -f debian/rules debian/control-real; \
	fi


debian/control-real: $(CONTROL_FILES)
	debian/bin/gencontrol.py $(KERNELVERSION) /usr/src/linux-support-$(KERNELVERSION)
	md5sum $^ > debian/control.md5sum
	@echo
	@echo This target is made to fail intentionally, to make sure
	@echo that it is NEVER run during the automated build. Please
	@echo ignore the following error, the debian/control file has
	@echo been generated SUCCESSFULLY.
	@echo
	exit 1

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