#!/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.

DEB_HOST_ARCH	?= $(shell dpkg-architecture -qDEB_HOST_ARCH)
THIS_PACKAGE	:= $(shell dpkg-parsechangelog | grep ^Source:  | cut -d ' ' -f 2)
THIS_VERSION	:= $(shell dpkg-parsechangelog | grep ^Version: | cut -d ' ' -f 2)

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

include debian/rules.defs
UPSTREAM_VERSION	?= $(firstword $(subst +, ,$(THIS_VERSION)))
KERNEL_MAJMIN		?= $(word 2,$(subst +, ,$(THIS_VERSION)))
NVIDIA_LEGACY		?=
legacy			 = $(if $(NVIDIA_LEGACY),-legacy-$(NVIDIA_LEGACY)xx)
DESC_NAME		?= NVIDIA
SECTION			?= non-free/kernel
PRIORITY		?= optional
MODULE			?= $(SOURCE)
SOURCE_PKG		?= nvidia-kernel$(legacy)-source
SOURCE			?= $(SOURCE_PKG:-source=)
SOURCE_MIN_REV		?=
SOURCE_MAX_SUFFIX	?= .~
META_MINDEP_VERSION	?= $(UPSTREAM_VERSION)
KERNEL_ABI		?= ???
KERNEL_VERSION		?= $(KERNEL_MAJMIN)-$(KERNEL_ABI)

SOURCE_VERSION		:= $(call get_package_version,$(SOURCE_PKG))
SOURCE_REVISION		 = $(subst X$(UPSTREAM_VERSION)-,,X$(SOURCE_VERSION))
THIS_REVISION		?= $(subst X$(UPSTREAM_VERSION)+$(KERNEL_MAJMIN)+,,X$(THIS_VERSION))
MERGED_VERSION		 = $(UPSTREAM_VERSION)+$(THIS_REVISION)+$(SOURCE_REVISION)

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))


build: build-arch build-indep
build-arch: build-stamp
build-indep:
build-stamp: debian/control
	tar jxf /usr/src/$(SOURCE).tar.bz2
	sed -i \
	    -e '/^Source:/s/.*/Source: $(THIS_PACKAGE)/' \
	    -e '/^Recommends:/s/linux-image-#KVERS#//g' \
	    -e '/^Depends:/ s/$$/, linux-image-#KVERS#,/' \
	    modules/$(SOURCE)/debian/control.template
	mv modules/$(SOURCE)/debian/changelog modules/$(SOURCE)/debian/changelog.$(SOURCE_PKG).Debian
	echo debian/changelog.$(SOURCE_PKG).Debian >> modules/$(SOURCE)/debian/docs
	cp debian/changelog modules/$(SOURCE)/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
	cd modules/$(SOURCE) && \
		KSRC=/lib/modules/$*/build \
			KDREV=$(SOURCE_REVISION)+$(call get_package_version,linux-headers-$*) \
			debian/rules configure
	sed -i 's/^$(THIS_VERSION)/$(UPSTREAM_VERSION)+$(THIS_REVISION)/' modules/$(SOURCE)/debian/VERSION
	cd modules/$(SOURCE) && \
		KSRC=/lib/modules/$*/build \
			KDREV=$(SOURCE_REVISION)+$(call get_package_version,linux-headers-$*) \
			KPKG_DEST_DIR=../.. \
			debian/rules binary_modules
	cd modules/$(SOURCE) && \
		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}_*.deb ; \
	    mv $$deb .. ; \
	    dpkg-distaddfile $$deb "$(SECTION)" "$(PRIORITY)" ; \
	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) debian/changelog
ifeq ($(wildcard debian/control.md5sum),)
	$(MAKE) -f debian/rules debian/control-real
else
	grep -q '#UPSTREAM_VERSION=$(UPSTREAM_VERSION)#' debian/control.md5sum && \
	md5sum --check debian/control.md5sum --status || \
		$(MAKE) -f debian/rules debian/control-real
	touch $@
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/#MODULE#/$(MODULE)/g' \
		-e 's/#META_MINDEP_VERSION#/$(META_MINDEP_VERSION)/g' \
		-e 's/#DESC_NAME#/$(DESC_NAME)/g' \
		debian/control.flavor >> debian/control.tmp

endef

debian/control-real: $(CONTROL_FILES)
	sed \
		-e 's/#THIS_PACKAGE#/$(THIS_PACKAGE)/g' \
		-e 's%#SECTION#%$(SECTION)%g' \
		-e 's/#PRIORITY#/$(PRIORITY)/g' \
		-e 's/#SOURCE_PKG#/$(SOURCE_PKG)/g' \
		-e 's/#UPSTREAM_VERSION#/$(UPSTREAM_VERSION)/g' \
		-e 's/#SOURCE_MIN_REV#/$(SOURCE_MIN_REV)/g' \
		-e 's/#SOURCE_MAX_SUFFIX#/$(SOURCE_MAX_SUFFIX)/g' \
		-e 's/#LINUX_HEADERS#/$(strip $(LINUX_HEADERS))/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 "#UPSTREAM_VERSION=$(UPSTREAM_VERSION)#" >> 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
