#!/usr/bin/make -f

# alsa-driver's debian/rules.
# Substantially rewritten by Steve Kowalik <stevenk@debian.org>
# Substantially rewritten by Thomas Hood <jdthood@yahoo.co.uk>

export DH_VERBOSE=1

ifeq (,$(filter noudeb, $(DEB_BUILD_PROFILES)))
  with_udeb = yes
endif

SHELL := /bin/bash
# Bwahaha, let's play "Fool dpatch"
# PACKAGE := ALSA
# No, no. Switched to quilt ;)

SRC_DIR := debian/alsa-source/usr/src/modules/alsa-driver
INSTALL_UAG := -o root -g audio

#include /usr/share/quilt/quilt.make

clean:
	dh_testdir
	dh_testroot
	debconf-updatepo
	-$(RM) build-stamp
	-$(RM) include/isapnp.h
	-$(RM) -r debian/patched
	-$(RM) debian/files
	-$(RM) debian/OSS-module-list_cleaned
	-$(RM) debian/ALSA-module-list_cleaned
	-$(RM) debian/ALSA-card-list_cleaned
	-$(RM) debian/ALSA-card-list_cleaned_nobrackets
	-$(RM) debian/ALSA-card-list_cleaned_nodescriptions
	-$(RM) debian/ALSA-card-list_needingpostinstall
	-$(RM) debian/ALSA-card-list_modem
	-$(RM) debian/ALSA-card-list_tv
	-$(RM) debian/ALSA-card-list_usb
	-$(RM) debian/ALSA-card-list_pcps
	dh_prep

configure: configure-stamp
configure-stamp:
	dh_testdir
	touch configure-stamp

build: configure build-stamp
build-stamp:
	touch build-stamp

binary: binary-indep binary-arch

binary-indep: DH_OPTIONS=-i
binary-indep: build-stamp build-source
	dh_testdir
	dh_testroot
	dh_installdirs
	### Start: Workaround bug #518242. Let udev do the job
	#install $(INSTALL_UAG) -m755 debian/alsa-base.modprobe-post-install \
	#	debian/alsa-base/lib/alsa/modprobe-post-install
	### END: Workaround bug #518242. Let udev do the job
	install $(INSTALL_UAG) -m755 debian/alsa-base.init \
		debian/alsa-base/sbin/alsa
	install $(INSTALL_UAG) -m755 debian/alsa-base.apm \
		debian/alsa-base/etc/apm/scripts.d/alsa
	install $(INSTALL_UAG) -m644 debian/alsa-base.defaultfile \
		debian/alsa-base/usr/share/alsa-base/alsa.default
	install $(INSTALL_UAG) -m644 debian/alsa-source.conf \
		debian/alsa-source/usr/share/alsa-source/alsa-source.conf
	install -m644 debian/apport-hook.py \
		debian/alsa-base/usr/share/apport/package-hooks/source_alsa-driver.py
	install -m755 utils/alsa-info.sh \
		debian/alsa-base/usr/share/alsa-base/alsa-info.sh
	### Generate lists
	cat debian/OSS-module-list \
		| sed -e 's/^[[:space:]]*//' -e 's/#.*//' -e '/^$$/d' \
		> debian/OSS-module-list_cleaned
	cat debian/ALSA-module-list \
		| sed -e 's/^[[:space:]]*//' -e 's/#.*//' -e '/^$$/d' \
		> debian/ALSA-module-list_cleaned
	cat debian/ALSA-card-list \
		| sed -e 's/^[[:space:]]*//' -e 's/#.*//' -e 's/,//' -e '/^$$/d' \
		> debian/ALSA-card-list_cleaned
	cat debian/ALSA-card-list_cleaned \
		| sed -e 's/ \[[^]]*\]//' \
		> debian/ALSA-card-list_cleaned_nobrackets
	cat debian/ALSA-card-list_cleaned \
		| sed -e 's/ .*$$//' \
		> debian/ALSA-card-list_cleaned_nodescriptions
	cat debian/ALSA-card-list_cleaned \
		| sed -e '/MODEM/d' \
		      -e '/TV/d' \
		      -e '/UNREAL/d' \
		      -e '/NOPOSTINSTALL/d' \
		> debian/ALSA-card-list_needingpostinstall
	cat debian/ALSA-card-list_cleaned \
		| grep -e 'MODEM' \
		> debian/ALSA-card-list_modem
	cat debian/ALSA-card-list_cleaned \
		| grep -e 'TV' \
		> debian/ALSA-card-list_tv
	cat debian/ALSA-card-list_cleaned \
		| grep -e 'USB' \
		> debian/ALSA-card-list_usb
	### Generate alsa-source.templates
	cp -p debian/alsa-source.templates debian/alsa-source.templates_BAK
	cat debian/alsa-source.templates_BAK \
		| sed "s%_CARDS_WITH_DESCRIPTIONS_%$$(echo $$(sed -e '$$!s/$$/, /' debian/ALSA-card-list_cleaned_nobrackets))%" \
		| sed "s%_CARDS_WITHOUT_DESCRIPTIONS_%$$(echo $$(sed -e '$$!s/$$/, /' debian/ALSA-card-list_cleaned_nodescriptions))%" \
		> debian/alsa-source.templates
	### Install blacklists and related
	install $(INSTALL_UAG) -m644 debian/OSS-module-list_cleaned \
		debian/linux-sound-base/usr/share/linux-sound-base/OSS-module-list
	install $(INSTALL_UAG) -m644 debian/ALSA-module-list_cleaned \
		debian/linux-sound-base/usr/share/linux-sound-base/ALSA-module-list
	cat debian/OSS-module-list_cleaned \
		| sed -e 's/[[:space:]].*//' -e 's/^/blacklist /' \
		> debian/linux-sound-base/lib/linux-sound-base/noOSS.modprobe.conf
	cat debian/ALSA-module-list_cleaned \
		| sed -e 's/[[:space:]].*//' -e 's/^/blacklist /' \
		> debian/linux-sound-base/lib/linux-sound-base/noALSA.modprobe.conf
	echo '# Uncomment these entries in order to blacklist unwanted modem drivers' \
		> debian/alsa-base/etc/modprobe.d/blacklist-modem.conf
	cat debian/ALSA-card-list_modem \
		| sed -e 's/[[:space:]].*//' -e 's/^/# blacklist snd-/' \
		>> debian/alsa-base/etc/modprobe.d/blacklist-modem.conf
	echo '# Comment this entry in order to load snd-pcsp driver' \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	echo 'blacklist snd-pcsp' \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	#echo '# Comment this entry in order to load pcspkr driver' \
	#	>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	#echo 'blacklist pcspkr' \
	#	>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	### Install module options
	install $(INSTALL_UAG) -m644 debian/alsa-base.modprobe \
		debian/alsa-base/etc/modprobe.d/alsa-base.conf
	### Start: Workaround bug #518242. Let udev do the job
	#echo "# Cause a script to be run after card driver module initialization" \
	#	>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	#cat debian/ALSA-card-list_needingpostinstall \
	#	| sed -e 's/[[:space:]].*//' -e 's%\(.*\)%install snd-\1 /sbin/modprobe --ignore-install snd-\1 $$CMDLINE_OPTS \&\& /lib/alsa/modprobe-post-install snd-\1%' \
	#	>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	### End: Workaround bug #518242. Let udev do the job
	### The default index of these drivers is -2 as of ALSA 1.0.9
	echo "# Prevent abnormal drivers from grabbing index 0" \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	cat debian/ALSA-card-list_tv \
		| sed -e 's/[[:space:]].*//' -e 's%\(.*\)%options \1 index=-2%' \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	cat debian/ALSA-card-list_modem debian/ALSA-card-list_usb \
		| sed -e 's/[[:space:]].*//' -e 's%\(.*\)%options snd-\1 index=-2%' \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	echo "# Ubuntu #62691, enable MPU for snd-cmipci" \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	echo "options snd-cmipci mpu_port=0x330 fm_port=0x388" \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	### This is for kernels >= 2.6.26
	echo "# Keep snd-pcsp from being loaded as first soundcard" \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	echo "options snd-pcsp index=-2" \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	echo "# Keep snd-usb-audio from beeing loaded as first soundcard" \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
	echo "options snd-usb-audio index=-2" \
		>> debian/alsa-base/etc/modprobe.d/alsa-base.conf
ifeq ($(with_udeb),yes)
	cp debian/alsa-base/etc/modprobe.d/* debian/alsa-base-udeb/etc/modprobe.d/
endif
	### Install debconf templates
	dh_installdebconf
	cp -p debian/alsa-source.templates_BAK debian/alsa-source.templates && rm -f debian/alsa-source.templates_BAK
	dh_lintian
	install -p -m 0644 -o root -g root debian/alsa-base.bug.presubj \
		debian/alsa-base/usr/share/bug/alsa-base/presubj
	install -p -m 0644 -o root -g root debian/alsa-source.bug.presubj \
		debian/alsa-source/usr/share/bug/alsa-source/presubj
	install -p -m 0644 -o root -g root debian/alsa-base.bug.control \
		debian/alsa-base/usr/share/bug/alsa-base/control
	install -p -m 0644 -o root -g root debian/alsa-source.bug.control \
		debian/alsa-source/usr/share/bug/alsa-source/control
	install -p -m 0755 -o root -g root debian/alsa-base.bug.script \
		debian/alsa-base/usr/share/bug/alsa-base/script
	install -p -m 0755 -o root -g root debian/alsa-source.bug.script \
		debian/alsa-source/usr/share/bug/alsa-source/script
	dh_installdocs
	dh_installman
	dh_installchangelogs -i
	dh_installchangelogs -palsa-source -palsa-base debian/changelog.ALSA
	dh_installdocs -p alsa-base debian/changelog-old.Debian
	cp debian/PATCHES.Debian \
		debian/alsa-source/usr/share/doc/alsa-source/
	mkdir debian/alsa-base/usr/share/doc/alsa-base/driver
	mv debian/alsa-base/usr/share/doc/alsa-base/Documentation/*.txt \
		debian/alsa-base/usr/share/doc/alsa-base/driver
	mv debian/alsa-base/usr/share/doc/alsa-base/Documentation/*.html \
		debian/alsa-base/usr/share/doc/alsa-base/driver
	rm -rf debian/alsa-base/usr/share/doc/alsa-base/Documentation
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Nothing to do 
binary-arch:

build-source:
	mkdir -p $(SRC_DIR)/debian/bug
	find . \( -path './debian' -o -name 'CVS' -o -name '.cvsignore'  -o -name '*-stamp' \) -prune -o -print | cpio -admp $(SRC_DIR)
	#Can't use upstream's makefiles because we don't run configure
	#-$(MAKE) -C $(SRC_DIR) clean
	chown -R root:src debian/alsa-source/usr/src
	# alsa-source/debian/*
	install -m755 debian/alsa-source.rules $(SRC_DIR)/debian/rules
	install -m644 debian/alsa-source.control $(SRC_DIR)/debian/control
	install -m644 debian/copyright $(SRC_DIR)/debian/copyright
	install -m644 debian/changelog $(SRC_DIR)/debian/changelog
	install -m644 debian/changelog.ALSA $(SRC_DIR)/debian/changelog.ALSA
	install -m644 debian/compat $(SRC_DIR)/debian/compat
	# And fix perms on the directory ...
	find debian/alsa-source -type d -exec chmod 775 {} \;
	find debian/alsa-source/usr/src/modules -type d -exec chmod 2775 {} \;
	# As much as I hate automatically generated stuff, automatically generate
	# files.
	major_ver="$(shell head -n 1 debian/changelog | cut -d\( -f2 | cut -d\) -f1 | cut -d. -f1-2)"; \
	for i in preinst postinst prerm postrm ; do \
		if [ -f debian/alsa-modules.$$i ] ; then \
			cat debian/alsa-modules.$$i | sed -e "s/_MAJORVERSION_/$$major_ver/g" > $(SRC_DIR)/debian/$$i; \
		fi \
	done
	install -m644 debian/alsa-modules.bug.control $(SRC_DIR)/debian/bug/control
	# Build the tarball, debian/* mess over.
	cd debian/alsa-source/usr/src && tar cf alsa-driver.tar modules
	$(RM) -r debian/alsa-source/usr/src/modules
	bzip2 -9 debian/alsa-source/usr/src/alsa-driver.tar

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

