#!/usr/bin/make -f

include debian/rules.defs

SHELL := /bin/bash

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS:=$(shell dpkg-buildflags --get CXXFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

%:
	if [ -e debian/control ]; then \
		dh $@; \
	else \
		$(MAKE) -f debian/rules debian/control; \
	fi

override_dh_installinit:
	dh_installinit --name sasc-ng

override_dh_installlogrotate:
	dh_installlogrotate --name sasc-ng

override_dh_auto_configure:
	# done per binary package in build-% rule
	
override_dh_auto_clean:
	cd contrib/sasc-ng; if [ -r config.mak ]; then make clean module_clean; fi
	rm -f contrib/sasc-ng/dvbloopback.o
	rm -f contrib/sasc-ng/config.mak
	rm -f contrib/sasc-ng/dvbloopback.ko
	rm -rf contrib/sasc-ng/sc/PLUGINS/src/sc-src/.pc
	rm -f contrib/sasc-ng/sc/PLUGINS/src/sc-src/.dependencies 
	rm -f .dependencies 
	rm -rf contrib/sasc-ng/sc/locale
	
# build_(kernel)_(flavor), e.g. build_3.7-trunk-amd64_sse2
# build_kernel needs to happen before build_sasc because build_sasc need
# dvbdev.h which is provided by build_kernel!

define kernel_short_version
$(shell echo $1|grep -o '[[:digit:]]\+\.[[:digit:]]\+')
endef

build_sasc_%:
	VERSION="$*"; \
	FLAVOR=$${VERSION#*_}; \
	KERNEL=$${VERSION/_$$FLAVOR/}; \
	BUILD_DIR=/lib/modules/$${KERNEL}/build; \
	sed -e "s|#DVB_DIR#|$(CURDIR)/debian/tmp/include/$$KERNEL/|" \
            < debian/config.mak-$$FLAVOR > contrib/sasc-ng/config.mak; \
	echo "Building sasc-ng $$VERSION - $$FLAVOR"; \
	cd contrib/sasc-ng; \
	make module CC=gcc-4.6 CXX=g++-4.6 BUILD_DIR=$${BUILD_DIR}; \
	make CC=gcc-4.6 CXX=g++-4.6 CPPFLAGS="$(CPPFLAGS)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" LDFLAGS="$(LDFLAGS)"; \
	mkdir -p $(CURDIR)/debian/tmp/$$KERNEL-$$FLAVOR/bin; \
	mkdir -p $(CURDIR)/debian/tmp/$$KERNEL-$$FLAVOR/modules; \
	mkdir -p $(CURDIR)/debian/tmp/$$KERNEL-$$FLAVOR/lib; \
	install -m0755 sasc-ng $(CURDIR)/debian/tmp/$$KERNEL-$$FLAVOR/bin/; \
	install -m0644 dvbloopback.ko $(CURDIR)/debian/tmp/$$KERNEL-$$FLAVOR/modules; \
	install -m0644 sc/PLUGINS/lib/* $(CURDIR)/debian/tmp/$$KERNEL-$$FLAVOR/lib/; \
	make module_clean; \
	make clean

# build_kernel_(kernel), e.g. build_kernel_3.7-trunk-amd64
build_kernel_%:
	VERSION="$*"; \
	KERNEL=$${VERSION#*_}; \
	SHORT_KERNEL=$$( echo $${KERNEL} | grep -o '[[:digit:]]\+\.[[:digit:]]\+' ); \
	KERNEL_TARBALL=/usr/src/linux-source-$${SHORT_KERNEL}.tar.xz; \
	BUILD_DIR=/lib/modules/$${KERNEL}/build; \
	echo "Building patched dvb-core module for kernel $${KERNEL}"; \
	tmpdir=$(CURDIR)/debian/tmp-$${SHORT_KERNEL}; \
	mkdir -p $$tmpdir; \
	tar xJf $${KERNEL_TARBALL} -C $${tmpdir} --strip-components 4 linux-source-$${SHORT_KERNEL}/drivers/media/dvb-core/; \
	patch -p4 -d $${tmpdir} < $(CURDIR)/debian/linux-3.7.1-dvb-mutex.patch; \
	make -C $${BUILD_DIR} M=$${tmpdir} modules; \
	mkdir -p $(CURDIR)/debian/tmp/$$KERNEL/module-updates; \
	mkdir -p $(CURDIR)/debian/tmp/include/$$KERNEL/; \
	cp $${tmpdir}/dvb-core.ko $(CURDIR)/debian/tmp/$$KERNEL/module-updates/; \
	cp $${tmpdir}/dvbdev.h $(CURDIR)/debian/tmp/include/$$KERNEL/; \
	rm -rf $${tmpdir}

.PHONY: build_kernel
build_kernel: $(foreach kernel,$(KERNELS),build_kernel_$(kernel))

.PHONY: build_sasc
build_sasc: $(foreach kernel,$(KERNELS),$(foreach flav,$(FFDECSA_FLAVORS),build_sasc_$(kernel)_$(flav))) 

override_dh_auto_build:
	# Do nothing

override_dh_auto_install: build_kernel build_sasc

dh_strip_%:
	VERSION="$*"; \
	FLAVOR=$${VERSION#*_}; \
	KERNEL=$${VERSION/_$$FLAVOR/}; \
	dh_strip -p sasc-ng-$$KERNEL-$$FLAVOR --dbg-package=sasc-ng-$$KERNEL-$$FLAVOR-dbg

.PHONY: override_dh_strip
override_dh_strip: $(foreach kernel,$(KERNELS),$(foreach flav,$(FFDECSA_FLAVORS),dh_strip_$(kernel)_$(flav))) 

CONTROL_FILES    = debian/control.source debian/control.flavor debian/rules debian/rules.defs debian/sasc-ng.install.in
comma            = ,

debian/control: $(CONTROL_FILES)
ifeq ($(wildcard debian/control.md5sum),)
	$(MAKE) -f debian/rules debian/control-real
else
	md5sum --check debian/control.md5sum --status || \
		$(MAKE) -f debian/rules debian/control-real
endif

LINUX_HEADERS    =$(foreach f,$(KERNELS),linux-headers-$(f) (=$(KERNEL_VERSION_$(f)))$(comma))
LINUX_SOURCES    =$(foreach f,$(KERNELS),linux-source-$(call kernel_short_version,$(f)) (=$(KERNEL_VERSION_$(f)))$(comma))

# call with $1 = kernel version, $2 = full kernel version, $3 = flavor
define append-flavor-entries
	sed \
		-e 's/#KERNEL#/$1/g' \
		-e 's/#KERNEL_VERSION#/$2/g' \
		-e 's/#FLAVOR#/$3/g' \
		debian/control.flavor >> debian/control.tmp

endef

# call with $1 = kernel version, $2 = flavor
define create-flavor-install
	 sed -e 's/#KERNEL#/$1/g' \
	     -e 's/#FLAVOR#/$2/g' \
		debian/sasc-ng.install.in > debian/sasc-ng-$1-$2.install

endef


debian/control-real: $(CONTROL_FILES)
	sed \
		-e 's/#LINUX_HEADERS#/$(LINUX_HEADERS)/g' \
		-e 's/#LINUX_SOURCES#/$(LINUX_SOURCES)/g' \
		debian/control.source > debian/control.tmp
	$(foreach kernel,$(KERNELS),$(foreach flav,$(FFDECSA_FLAVORS),$(call append-flavor-entries,$(kernel),$(KERNEL_VERSION_$(kernel)),$(flav))))
	mv debian/control.tmp debian/control
	$(foreach kernel,$(KERNELS),$(foreach flav,$(FFDECSA_FLAVORS),$(call create-flavor-install,$(kernel),$(flav))))
	md5sum debian/control $^ > 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

