#!/usr/bin/make -f

deb_source := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
deb_version := $(shell dpkg-parsechangelog | sed -n 's/^Version: //p')
upstream_version := $(shell echo '$(deb_version)' | sed 's/.*://; s/-[^-]*$$//')
git_rev := $(shell echo '$(upstream_version)' | sed 's/.*+//')

DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS = $(shell dpkg-architecture -qDEB_HOST_ARCH_OS)
DEB_HOST_ARCH_CPU = $(shell dpkg-architecture -qDEB_HOST_ARCH_CPU)
DEB_HOST_GNU_CPU = $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
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 = $(shell dpkg-architecture -qDEB_BUILD_ARCH)

QEMU_CPU = $(shell dpkg-architecture -qDEB_HOST_GNU_CPU)
ifeq ($(QEMU_CPU),powerpc)
  QEMU_CPU = ppc
endif
ifneq (,$(filter $(DEB_HOST_ARCH),armel armhf))
  QEMU_CPU = armv4l
endif

ifneq ($(DEB_BUILD_GNU_TYPE), $(DEB_HOST_GNU_TYPE))
  conf_arch += --cc=$(DEB_HOST_GNU_TYPE)-gcc --cpu=$(QEMU_CPU)
endif

ifeq ($(DEB_HOST_ARCH_OS),linux)
       conf_audio_drv += --audio-drv-list=alsa,oss,sdl,pa
endif
ifeq ($(DEB_HOST_ARCH_OS),kfreebsd)
       conf_audio_drv += --audio-drv-list=oss,sdl,pa
endif
# KVM on PowerPC needs at least 2.6.33 kernel headers
ifeq ($(DEB_HOST_ARCH_CPU),powerpc)
       conf_arch += --disable-kvm
endif

ifneq (,$(filter $(DEB_HOST_ARCH),armel armhf))
       conf_arch += --extra-cflags=-marm
endif

# binfmt support
ALL_BINFMT_TARGETS=alpha arm armeb cris i386 m68k microblaze mips mipsel ppc ppc64 ppc64abi32 sh4 sh4eb sparc sparc32plus sparc64 x86_64
ifeq ($(DEB_HOST_ARCH),amd64)
BINFMT_TARGETS=$(filter-out i386 x86_64, $(ALL_BINFMT_TARGETS))
endif
ifneq (,$(findstring $(DEB_HOST_ARCH), arm armel armhf))
BINFMT_TARGETS=$(filter-out arm, $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),i386)
BINFMT_TARGETS=$(filter-out i386, $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),powerpc)
BINFMT_TARGETS=$(filter-out ppc, $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),ppc64)
BINFMT_TARGETS=$(filter-out ppc ppc64 ppc64abi32 , $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),sparc)
BINFMT_TARGETS=$(filter-out sparc sparc32plus sparc64, $(ALL_BINFMT_TARGETS))
endif
ifeq ($(DEB_HOST_ARCH),sparc64)
BINFMT_TARGETS=$(filter-out sparc sparc32plus sparc64, $(ALL_BINFMT_TARGETS))
endif
ifeq ($(BINFMT_TARGETS),)
BINFMT_TARGETS=$(ALL_BINFMT_TARGETS)
endif

# namespace to use for data files
NAMESPACE := linaro

# basedir in which to install BIOSes and keymaps below prefix
DATASUBDIR := share/qemu-$(NAMESPACE)
DATADIR := /usr/$(DATASUBDIR)
# XXX doesn't work because it's #defined in the source should send an upstream
# patch to allow ./configuring that
#CFLAGS += -DSHARE_SUFFIX="$(DATASUBDIR)"

# Workaround for var tracking blowing up the memory requirements more than
# tenfold on armel; we'll keep -g enabled but disable this particular feature
ifneq (,$(findstring $(DEB_HOST_ARCH), armel armhf))
CFLAGS += -fno-var-tracking
endif

# XXX for some reason, --remote doesn't work with gitorious so this needs to be
# run from a local checkout
# --remote=git://gitorious.org/qemu-maemo/qemu-maemo.git
get-orig-source:
	git archive \
	    --format=tar \
	    --prefix=$(deb_source)-$(upstream_version)/ \
	    $(git_rev) \
	    | gzip >$(deb_source)_$(upstream_version).orig.tar.gz

override_dh_auto_configure:
	# system build
	mkdir -p $(CURDIR)/system-build
	cd $(CURDIR)/system-build && \
		../configure \
		    --with-pkgversion="Debian $(deb_version)" \
		    --extra-cflags="$(CFLAGS)" \
		    --extra-ldflags="$(LDFLAGS)" \
		    --prefix=/usr \
		    --datadir=$(DATADIR) \
		    --interp-prefix=/etc/qemu-binfmt/%M \
		    --disable-blobs \
		    --disable-strip \
		    --disable-linux-user \
		    --disable-bsd-user \
		    --disable-darwin-user \
		    $(conf_arch) $(conf_audio_drv)

ifeq ($(DEB_HOST_ARCH_OS),linux)
	# user build
	mkdir -p $(CURDIR)/user-build
	cd $(CURDIR)/user-build && \
		../configure \
		    --with-pkgversion="Debian $(deb_version)" \
		    --extra-cflags="$(CFLAGS)" \
		    --extra-ldflags="$(LDFLAGS)" \
		    --prefix=/usr \
		    --datadir=$(DATADIR) \
		    --interp-prefix=/etc/qemu-binfmt/%M \
		    --disable-blobs \
		    --disable-strip \
		    --disable-system \
		    $(conf_arch)

	# static user build
	mkdir -p $(CURDIR)/user-static-build
	cd $(CURDIR)/user-static-build && \
		../configure \
		    --with-pkgversion="Debian $(deb_version)" \
		    --extra-cflags="$(CFLAGS)" \
		    --extra-ldflags="$(LDFLAGS)" \
		    --prefix=/usr \
		    --datadir=$(DATADIR) \
		    --interp-prefix=/etc/qemu-binfmt/%M \
		    --disable-blobs \
		    --disable-strip \
		    --disable-system \
		    --static \
		    $(conf_arch)
endif

override_dh_auto_build:
	# system build
	dh_auto_build -B system-build
ifeq ($(DEB_HOST_ARCH_OS),linux)
	# user build
	dh_auto_build -B user-build

	# static user build
	dh_auto_build -B user-static-build
endif

override_dh_auto_install:
ifeq ($(DEB_HOST_ARCH_OS),linux)
	dh_auto_install -B user-static-build --destdir=$(CURDIR)/debian/tmp
	for script in prerm postinst; do \
		sed -e 's/^BINFMT_TARGETS=.*/BINFMT_TARGETS=\"$(BINFMT_TARGETS)\"/g' \
                $(CURDIR)/debian/qemu-user-static.$$script.in \
		> $(CURDIR)/debian/qemu-user-static.$$script; \
	done
	for target in $(CURDIR)/debian/tmp/usr/bin/qemu-*; do \
		install -m 755 $$target $(CURDIR)/debian/tmp/usr/bin/$$(basename $$target)-static; \
	done
	dh_auto_install -B user-build --destdir=$(CURDIR)/debian/tmp
	for target in $(BINFMT_TARGETS) ; do \
		install -m 644 $(CURDIR)/debian/binfmts/qemu-$$target \
			$(CURDIR)/debian/qemu-user-static/usr/share/binfmts ; \
	done
	f=debian/qemu-user-static.sysctl; [ -f debian/qemu-user-static.sysctl.$(DEB_BUILD_ARCH) ] && f=debian/qemu-user-static.sysctl.$(DEB_BUILD_ARCH); cp $$f debian/qemu-user-static/etc/sysctl.d/30-qemu-user-static.conf
endif
	dh_auto_install -B system-build --destdir=$(CURDIR)/debian/tmp
	mv $(CURDIR)/debian/tmp/usr/share/man/man1/qemu.1 \
	   $(CURDIR)/debian/tmp/usr/share/man/man1/qemu-system.1

override_dh_auto_clean:
	rm -rf user-static-build user-build system-build

override_dh_auto_test:
	# tests are broken ATM

%:
	dh $@

.PHONY: get-orig-source
