#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# Ubuntu's dpkg-buildpackage sets env LDFLAGS=-Wl,-Bsymbolic-functions which
# breaks the kernel build
comma := ,
LDFLAGS := $(filter-out -Wl$(comma)-Bsymbolic-functions,$(LDFLAGS))
LDFLAGS := $(filter-out -Wl$(comma)-z$(comma)relro,$(LDFLAGS))

config_target:=oldconfig
kernel_version:=3.13.0
kernel_dir:=linux-source-$(kernel_version)
debian:=$(CURDIR)/debian
tmp:=$(debian)/user-mode-linux
tmpmodules:=$(debian)/uml-modules
DEB_HOST_ARCH?=$(shell dpkg-architecture -qDEB_HOST_ARCH)
#SUBARCH?=$(shell uname -m)

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
KBUILDVARS := CFLAGS_KERNEL=-O1
endif

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
NUMJOBS = -j$(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
endif

# development only targets
#
copy-config:
	cp config.$(DEB_HOST_ARCH) $(kernel_dir)/.config

oldconfig: unpack copy-config
	make -C $(kernel_dir) oldconfig ARCH=um
	cp config.$(DEB_HOST_ARCH) config.$(DEB_HOST_ARCH).oldconfig.old
	cp $(kernel_dir)/.config config.$(DEB_HOST_ARCH)

menuconfig: unpack copy-config
	make -C $(kernel_dir)  menuconfig ARCH=um
	cp config.$(DEB_HOST_ARCH) config.$(DEB_HOST_ARCH).menuconfig.old
	cp $(kernel_dir)/.config config.$(DEB_HOST_ARCH)

# 
# end development only targets


unpack: unpack-stamp
unpack-stamp:
	tar -x --bzip2 -f /usr/src/linux-source-$(kernel_version).tar.bz2
	touch $@

patch: patch-stamp
patch-stamp: unpack-stamp
	(cd $(kernel_dir) && QUILT_PATCHES=$(debian)/patches quilt push -a)
	touch $@

$(kernel_dir)/.config: config.$(DEB_HOST_ARCH) unpack
	cp $< $@

configure: configure-stamp
configure-stamp: patch-stamp $(kernel_dir)/.config
	dh_testdir

	$(MAKE) -C $(kernel_dir) $(config_target) ARCH=um

	touch $@

build: build-stamp
build-stamp: configure
	dh_testdir

	$(MAKE) $(NUMJOBS) -C $(kernel_dir) $(KBUILDVARS) linux modules ARCH=um
	docbook-to-man linux.sgml > linux.uml.1

	touch $@

clean: clean-unpatched
clean-unpatched: unpatch
	dh_testdir
	dh_testroot
	rm -f unpack-stamp build-stamp configure-stamp

	rm -rf $(kernel_dir) linux.uml.1 $(tmpmodules) changelog

	dh_clean

unpatch:
	(cd $(kernel_dir) && QUILT_PATCHES=$(debian)/patches quilt pop -a || /bin/true)
	rm -rf patch-stamp $(kernel_dir)/.pc

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs usr/bin usr/lib/uml usr/share/doc/user-mode-linux
	install -D -m 0644 debian/user-mode-linux.lintian debian/user-mode-linux/usr/share/lintian/overrides/user-mode-linux

	install $(kernel_dir)/linux $(tmp)/usr/bin/linux.uml
	$(MAKE) -C $(kernel_dir) modules_install ARCH=um \
		INSTALL_MOD_PATH=$(tmpmodules)
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	find $(tmpmodules) -name '*.ko' \
		| xargs --no-run-if-empty strip --strip-debug
endif
	-rm $(tmpmodules)/lib/modules/*/build
	-rm $(tmpmodules)/lib/modules/*/source
	mv $(tmpmodules)/lib/modules \
		$(tmp)/usr/lib/uml/modules
	rm -rf $(tmpmodules)

	install -m 644 $(kernel_dir)/.config \
		$(tmp)/usr/share/doc/user-mode-linux/config


# 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_installdebconf	
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
#	dh_installinit
	dh_installcron
	dh_installman linux.uml.1
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs $(CURDIR)/debian/changelog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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