#!/usr/bin/make -f

# alsa-driver's debian/rules.
# Substanially rewritten by Steve Kowalik <stevenk@debian.org>

SHELL := /bin/bash
# Bwahaha, let's play "Fool dpatch"
PACKAGE := ALSA

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

include /usr/share/dpatch/dpatch.make

clean: unpatch
	dh_testdir
	dh_testroot
	$(RM) debian/substvars debian/*.debhelper debian/*.substvars
	$(RM) -r debian/alsa-{base,source,headers}
	$(RM) debian/files debian/snddevices
	find . -type f -a \( -name \#\*\# -o -name \*\~ -o -name DEADJOE \
		-o -name \*.orig -o -name \*.rej -o -name \*.bak -o -name \
		.\*.orig -o -name .\*.rej -o -name .SUMS -o -name TAGS \
		-o -name .\#\* -o -name core -o \( -path \*/.deps/\* -a -name \
		\*.P \) \) -exec rm -f {} \;
	-$(RM) build-stamp
	-$(RM) include/isapnp.h
	-$(RM) debian/*substvars
	-$(RM) -r debian/patched
	dh_clean -k

build: build-stamp
build-stamp: patch
	touch build-stamp

binary: binary-indep binary-arch

binary-indep: DH_OPTIONS=-i
binary-indep: patch build-stamp build-source
	dh_testdir
	dh_testroot
	dh_installdirs
	install -d -o root -g root -m 755 debian/alsa-headers/usr/include/sound
	for f in $(shell cat debian/alsa-headers.headers); do \
		install -p -o root -g root -m 644 alsa-kernel/include/$$f debian/alsa-headers/usr/include/sound/; \
	done
	for i in alsa-base alsa-source ; do \
		mv debian/$$i.templates debian/$$i.templates.old; \
		cat debian/$$i.templates.old debian/alsa-common.templates > debian/$$i.templates; \
	done
	install $(INSTALL_UAG) -m 755 debian/alsa-base.apm \
		debian/alsa-base/etc/apm/event.d/alsa
	install $(INSTALL_UAG) -m 755 snddevices \
		debian/alsa-base/usr/share/alsa-base/snddevices
	install $(INSTALL_UAG) -m 644 debian/modules-snippet.conf \
		debian/alsa-base/usr/share/alsa-base/modules-snippet.conf
	install $(INSTALL_UAG) -m 644 debian/alsa-base.conf \
		debian/alsa-base/usr/share/alsa-base/alsa-base.conf
	install $(INSTALL_UAG) -m 644 debian/alsa-source.conf \
		debian/alsa-source/usr/share/alsa-source/alsa-source.conf
	install $(INSTALL_UAG) -m 644 debian/devfs.conf \
		debian/alsa-base/etc/devfs/conf.d/alsa
#	install $(INSTALL_UAG) -m 644 debian/alsa-path \
#		debian/alsa-base/etc/modutils/alsa-path
	install $(INSTALL_UAG) -m 755 utils/alsaconf \
		debian/alsaconf/usr/sbin
	install -d -m 755 debian/alsa-base/usr/share/lintian/overrides
	install -d -m 755 debian/alsa-base/usr/share/linda/overrides
	install -m 644 debian/alsa-base.lintian debian/alsa-base/usr/share/lintian/overrides/alsa-base
	install -m 644 debian/alsa-base.linda debian/alsa-base/usr/share/linda/overrides/alsa-base
	dh_installdebconf
# Hahaha! Now we can undo our great evilness!
	for i in alsa-base alsa-source ; do \
		if [ -f debian/$$i.templates.old ]; then \
			mv debian/$$i.templates.old debian/$$i.templates; \
		fi; \
	done
	dh_installdocs
	mv debian/alsa-source/usr/share/doc/alsa-source/patch-stamp debian/alsa-source/usr/share/doc/alsa-source/PATCHES
	dh_installexamples 
	DH_OPTIONS= dh_installinit --no-start -r --init-script=alsa
	dh_installman
	dh_installchangelogs 
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Nothing to do 
binary-arch:

build-source:
	mkdir -p $(SRC_DIR)
	find . \( -path './debian' -o -name 'CVS' -o -name '.cvsignore'  -o -name '*-stamp' \) -prune -o -print | cpio -admp $(SRC_DIR)
	-$(MAKE) -C $(SRC_DIR) clean
	chown -R root:src debian/alsa-source/usr/src
	# alsa-source/debian/*
	install -d $(SRC_DIR)/debian
	install -m755 debian/alsa-source.rules $(SRC_DIR)/debian/rules
	install -m644 debian/alsa-source.control $(SRC_DIR)/debian/control
	install -m644 debian/changelog $(SRC_DIR)/debian/changelog
	install -m644 debian/compat $(SRC_DIR)/debian/compat
	install -m644 debian/alsa-modules.dirs $(SRC_DIR)/debian/dirs
	# 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.
	for i in post{inst,rm} ; do \
		major_ver="$(shell head -1 debian/changelog | cut -d\( -f2 | cut -d\) -f1 | cut -d. -f1-2)"; \
		cat debian/alsa-modules.$$i | sed -e "s/_MAJORVERSION_/$$major_ver/g" > $(SRC_DIR)/debian/$$i; \
	done
	# 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 patch unpatch

