#!/usr/bin/make -f

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

PKG_NAME=xen-qemu-dm-4.0
XEN_TARGET=xen-4.0
XEN_FOLDER=/usr/lib/$(XEN_TARGET)

BUILD_DIR=debian/build
STAMPS_DIR=debian/stamps

$(STAMPS_DIR)/source:
	@mkdir -p $(STAMPS_DIR)
	# Do a fresh copy of all the code in debian/build/source
	rm -rf $(BUILD_DIR)
	mkdir -p $(BUILD_DIR)
	cp -al $(filter-out debian .git .svk .svn, $(wildcard .[^.]* *)) $(BUILD_DIR)
	# This applies the Bastian Blank patch to the Makefile to be able to
	# build without the Xen mercurial.
	# Sorry for this, I know this is unconventional, but it helps
	# doing easier backports to Lenny without the use of the quilt
	# format. If I am mistaking here, let me know how I should do.
	cd $(BUILD_DIR) \
		&& patch -p1 <../mypatches/add-rules.patch \
		&& patch -p1 <../mypatches/adds-audio-card-config.patch \
		&& patch -p1 <../mypatches/alsaaudio-spelling-mistake.patch \
		&& patch -p1 <../mypatches/remove-blktap.patch \
		&& patch -p1 <../mypatches/fix_buffer_underflow_CVE-2012-0029.patch \
	        && patch -p1 <../mypatches/xsa17-qemu-xen-traditional-all.patch \
		&& patch -p1 <../mypatches/xsa19-qemu-all.patch \
		&& patch -p1 <../mypatches/CVE-2012-6075-1 \
		&& patch -p1 <../mypatches/CVE-2012-6075-2
	touch $@

# The xen-setup thing
# Note that normally, --audio-drv-list supports to have all of these
# at the same time: oss alsa sdl esd pa
# and then, you can use an environment variable to select the driver
# like this: QEMU_AUDIO_DRV=pa
# There's also a list of emulated sound hardware that can be set in
# the soundhw= parameter of your guest, soundhw=ac97 being the best
# choice in terms of performances.
# --audio-card-list="ac97 es1370 sb16 cs4231a adlib gus"
$(STAMPS_DIR)/setup: $(STAMPS_DIR)/source
	cd $(BUILD_DIR); ./xen-setup \
		--audio-drv-list="pa oss alsa sdl esd" \
		--audio-card-list="ac97 es1370 sb16 cs4231a adlib gus" \
		--enable-mixemu
	touch $@

$(STAMPS_DIR)/build: $(STAMPS_DIR)/setup
	dh_testdir
	$(MAKE) -C $(BUILD_DIR) BUILD_DOCS=yes
	# As we don't want to conflict with the "normal" qemu-img utility, I
	# did this:
	mv $(BUILD_DIR)/qemu-img.1 $(BUILD_DIR)/qemu-img-xen.1
	# Remove blktap as support has been removed from the hypervisor
	#cd $(BUILD_DIR) && patch -p0 <../qemu-img-xen.1.diff
	touch $@

clean:
	dh_testdir
	dh_testroot
	dh_clean
	rm -rf $(BUILD_DIR) $(STAMPS_DIR)

$(STAMPS_DIR)/install: $(STAMPS_DIR)/build
	dh_testdir
	dh_testroot
	dh_prep
	# dh_installdirs
	# dh_link
	install -D -m 0755 $(BUILD_DIR)/i386-dm/qemu-dm debian/$(PKG_NAME)/usr/lib/$(XEN_TARGET)/bin/qemu-dm
	# Not shipping this, as it's in the qemu package
	#install -D -m 0755 $(BUILD_DIR)/qemu-img-xen debian/$(PKG_NAME)/usr/bin/qemu-img-xen
	install -D -m 0644 debian/docbase debian/$(PKG_NAME)/usr/share/doc-base/xen-qemu-dm-4.0-doc

	# $(MAKE) install DESTDIR=debian/$(PKG_NAME)$(XEN_FOLDER)

binary-arch: $(STAMPS_DIR)/install
	dh_testdir
	dh_testroot
	dh_installchangelogs Changelog
	dh_installdocs
	dh_link
	dh_shlibdeps
	dh_strip
	dh_fixperms
	dh_compress
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

binary-indep: $(STAMPS_DIR)/install

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