#!/usr/bin/make -f

# Written by Steve Kowalik <stevenk@debian.org> for the New Alsa-Source.
# Loosely based on the rules file from pcmcia-cs and the old alsa-source.

KSRC ?= /usr/src/linux
KDREV ?= unknown
KVERS ?= unknown

# See if we can work out the compiler used
ifeq ($(origin CC),default)
ifneq "$(wildcard $(KSRC)/include/linux/compile.h)" ""
CC = gcc-$(shell grep LINUX_COMPILER $(KSRC)/include/linux/compile.h | sed 's/.* \([0-9]\+\.[0-9]\+\).*/\1/')
else
CC = gcc
endif
endif

# Special case gcc 2.7.2
ifeq ($(CC),gcc-2.7)
CC = gcc272
endif

# If they didn't set $(KVERS), see if we can do it for them.
ifeq ($(KVERS),unknown)
ifneq "$(wildcard $(KSRC)/include/linux/version.h)" ""
KVERS = $(shell head -1 $(KSRC)/include/linux/version.h | sed 's/.*"\(.*\)"$$/\1/')
endif
endif

# Clear root command if already root
ifeq ($(shell id -u),0)
ROOT_CMD=
endif

CONFIGURE_OPT = CC=$(CC) --prefix=/usr --with-kernel=$(KSRC) \
		 --with-moddir=/lib/modules/$(KVERS)/alsa \
		 --with-sequencer=yes

# See SF bug #550435
ifneq (,$(findstring 2.2.,$(KVERS)))
CONFIGURE_OPT += --disable-verbose-printk
endif

# Read in config file, generated by debconf.
ifeq ($(HOME)/.alsa-source.conf,$(wildcard $(HOME)/.alsa-source.conf))
include $(HOME)/.alsa-source.conf
else
ifeq (/etc/alsa/alsa-source.conf,$(wildcard /etc/alsa/alsa-source.conf))
include /etc/alsa/alsa-source.conf
endif
endif

ifeq ($(ALSA_NOPNP),"y")
CONFIGURE_OPT += --with-isapnp=no
else
CONFIGURE_OPT += --with-isapnp=yes
endif

ifeq ($(ALSA_DEBUG),"y")
CONFIGURE_OPT += --with-debug=detect
endif

ifneq ($(ALSA_CARDS),"")
CONFIGURE_OPT += --with-cards=$(ALSA_CARDS)
endif

ifneq ($(CONCURRENCY_LEVEL),)
MAKE_OPT = -j $(CONCURRENCY_LEVEL)
endif

VERSION = $(shell dpkg-parsechangelog | grep ^Vers | cut -d\  -f2)
ifneq ($(KDREV),unknown)
ifeq ($(findstring :,$(KDREV)),:)
VERSION := $(subst :,:$(VERSION)+,$(KDREV))
else
VERSION := $(VERSION)+$(KDREV)
endif
endif

echo-vars:
	@echo "I've been configured using:"
	@echo " - Kernel source of $(KSRC)"
	@echo " - Kernel version of $(KVERS)"
	@echo " - Kernel revision of $(KDREV)"
	@echo " - C compiler of $(CC)"
	@echo " - Make options of $(MAKE_OPT)"
	@echo " - Version of $(VERSION)"

configure: configure-stamp
configure-stamp:
	@if [ ! -x /usr/bin/$(CC) ]; then echo "You don't have the compiler that your kernel was built with installed"; exit 1; fi
	./configure $(CONFIGURE_OPT)
	touch configure-stamp

build: build-stamp
build-stamp: configure-stamp
	$(MAKE) $(MAKE_OPT) compile

install: install-stamp
install-stamp: build-stamp
	$(MAKE) $(MAKE_OPT) DESTDIR=$(CURDIR)/debian/$(shell grep ^Package debian/control | cut -d\  -f2) install-modules

control-munge:
	for i in control postinst postrm ; do \
		cp debian/$$i debian/$$i.old; \
		cat debian/$$i | sed -e 's/_KVERS_/$(KVERS)/g' > debian/$$i.bak; \
		mv debian/$$i.bak debian/$$i; \
	done
	touch control-munge

clean-control-munge:
	for i in control postinst postrm ; do \
		if [ -f debian/$$i.old ]; then \
			mv debian/$$i.old debian/$$i; \
		fi; \
	done
	-$(RM) control-munge

clean: clean-control-munge
	-make mrproper
	$(RM) configure-stamp
	$(RM) build-stamp

binary_modules: binary-modules
binary-modules: configure-stamp build-stamp control-munge install-stamp
	dh_testdir
	dh_testroot
	dh_installdirs
	dh_link usr/share/doc/alsa-base usr/share/doc/alsa-modules-$(KVERS)
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- -v"$(VERSION)"
	dh_md5sums 
ifeq "$(origin KPKG_DEST_DIR)" "undefined"
ifeq "$(origin KMAINT)" "undefined"
	dh_builddeb --destdir=$(CURDIR)/..
else
	dh_builddeb --destdir=$(KSRC)/..
endif
else
	dh_builddeb --destdir=$(KPKG_DEST_DIR)
endif

binary:
	@echo "Binary target not supported. Use binary-modules or make-kpkg."
	exit 1

# Targets that kernel-package uses.
kdist_configure: configure-stamp
kdist_config: configure-stamp
kdist_image:
	$(ROOT_CMD) $(MAKE) -f debian/rules binary-modules
	$(ROOT_CMD) $(MAKE) -f debian/rules clean
kdist_clean: clean
kdist:
	$(ROOT_CMD) $(MAKE) -f debian/rules binary-modules

.PHONY: configure build clean binary-modules binary_modules binary kdist_configure kdist_config kdist_image kdist_clean kdist

