#!/usr/bin/make -f
#
# This package is responsible for generating the pre-built kernel modules
# for the current kernel version.  Most of the work is done by the existing
# build machinery in nvidia-kernel-source.  This package depends on
# nvidia-kernel-source and the relevant kernel headers, unpacks the former
# package, and then builds it for each kernel flavor in turn.  The resulting
# *.deb files are then added to the *.changes file as packages built by this
# source file.
#
# This package should be a minimal shim around nvidia-kernel-source.  Most
# problems apparently found in this package are probably actually problems
# that should be fixed there.
#
# Based on the debian/rules template by Joey Hess
# GNU copyright 1997 to 1999 by Joey Hess.
# Copyright (c) 1999-2001 Herbert Xu <herbert@debian.org>
# Fixes by Steve Kowalik for the New Alsa-Source.
# Customized by Randall Donald for nvidia-kernel-source
# Further revisions by the Debian NVIDIA Maintainers.  See debian/changelog.

include debian/rules.defs
DEB_HOST_ARCH	?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
package		:= $(shell dpkg-parsechangelog | grep ^Source: | cut -d ' ' -f2)
legacy		 = $(if $(NVIDIA_LEGACY),-legacy-$(NVIDIA_LEGACY)xx)

KERNEL_FLAVORS_i386		 = $(KERNEL_FLAVORS_i386_yes)
KERNEL_FLAVORS_amd64		 = $(KERNEL_FLAVORS_amd64_yes)
KERNEL_FLAVORS_both		 = $(filter $(KERNEL_FLAVORS_i386),$(KERNEL_FLAVORS_amd64))
KERNEL_FLAVORS_only_i386	 = $(filter-out $(KERNEL_FLAVORS_both),$(KERNEL_FLAVORS_i386))
KERNEL_FLAVORS_only_amd64	 = $(filter-out $(KERNEL_FLAVORS_both),$(KERNEL_FLAVORS_amd64))

# arg1 = package to query
get_package_version		 = $(shell dpkg-query -s $1 | grep ^Version: | cut -d ' ' -f2)

nks_debian_revision		 = $(subst $(NVIDIA_VERSION)-,,$(call get_package_version,nvidia-kernel$(legacy)-source))

build: build-arch build-indep
build-arch: build-stamp
build-indep:
build-stamp: debian/control
	tar jxf /usr/src/nvidia-kernel$(legacy).tar.bz2
	sed -i \
	    -e '/^Source:/s/.*/Source: $(package)/' \
	    -e '/^Recommends:/s/linux-image-#KVERS#//g' \
	    -e '/^Depends:/ s/$$/, linux-image-#KVERS#,/' \
	    modules/nvidia-kernel$(legacy)/debian/control.template
	mv modules/nvidia-kernel$(legacy)/debian/changelog modules/nvidia-kernel$(legacy)/debian/changelog.nvidia-kernel$(legacy)-source.Debian
	cp debian/changelog modules/nvidia-kernel$(legacy)/debian
	touch $@

clean: debian/control
	rm -rf modules
	rm -f *.deb
	dh_clean

# Use debhelper to build the metapackages and then build the kernel
# packages.  debhelper will build dummy packages based on debian/control for
# the kernel packages, which will then be overwritten by the real packages.
binary: binary-arch binary-indep
binary-indep:
binary-arch-meta: build
	dh_installdirs
	dh_installchangelogs
	dh_installdocs
	dh_install
	dh_compress
	dh_fixperms
	dh_gencontrol
	dh_md5sums
	dh_builddeb

.NOTPARALLEL:
binary-module-%: build binary-arch-meta
	set -e; cd modules/nvidia-kernel$(legacy); \
		KSRC=/lib/modules/$*/build \
			KDREV=$(nks_debian_revision)+$(call get_package_version,linux-headers-$*) \
			KPKG_DEST_DIR=../.. \
			debian/rules binary_modules ; \
		KSRC=/lib/modules/$*/build \
			debian/rules clean
	# Replace dummy deb and entry in debian/files, the package just built
	# has a more detailed version number.
	@set -e -x; for deb in *$**.deb; do \
	    : Register $$deb ; \
	    p=$${deb%%_*} ; \
	    sed -i -e "/^$${p}_/d" debian/files ; \
	    rm ../$${p}_* ; \
	    mv $$deb .. ; \
	    dpkg-distaddfile $$deb "non-free/kernel" optional ; \
	done

binary-arch: build binary-arch-meta $(foreach f,$(KERNEL_FLAVORS_$(DEB_HOST_ARCH)),binary-module-$(KERNEL_VERSION)-$(f))

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

CONTROL_FILES	 = debian/control.source debian/control.flavor debian/rules debian/rules.defs
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,$(KERNEL_FLAVORS_both),linux-headers-$(KERNEL_VERSION)-$(f) [i386 amd64]$(comma))
LINUX_HEADERS	+=$(foreach f,$(KERNEL_FLAVORS_only_i386),linux-headers-$(KERNEL_VERSION)-$(f) [i386]$(comma))
LINUX_HEADERS	+=$(foreach f,$(KERNEL_FLAVORS_only_amd64),linux-headers-$(KERNEL_VERSION)-$(f) [amd64]$(comma))

# call with $1 = kernel version, $2 = flavor, $3 = arch
define append-flavor-entries
	sed \
		-e 's/#KERNEL#/$1/g' \
		-e 's/#FLAVOR#/$2/g' \
		-e 's/#ARCH#/$3/g' \
		-e 's/#NVIDIA_VERSION#/$(NVIDIA_VERSION)/g' \
		-e 's/#LEGACY#/$(legacy)/g' \
		debian/control.flavor >> debian/control.tmp

endef

debian/control-real: $(CONTROL_FILES)
	sed \
		-e 's/#NVIDIA_VERSION#/$(NVIDIA_VERSION)/g' \
		-e 's/#LINUX_HEADERS#/$(LINUX_HEADERS)/g' \
		-e 's/#LEGACY#/$(legacy)/g' \
		debian/control.source > debian/control.tmp
	$(foreach f,$(KERNEL_FLAVORS_both),$(call append-flavor-entries,$(KERNEL_VERSION),$(f),i386 amd64))
	$(foreach f,$(KERNEL_FLAVORS_only_i386),$(call append-flavor-entries,$(KERNEL_VERSION),$(f),i386))
	$(foreach f,$(KERNEL_FLAVORS_only_amd64),$(call append-flavor-entries,$(KERNEL_VERSION),$(f),amd64))
	mv debian/control.tmp debian/control
	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
