#!/usr/bin/make -f

package=alsadriver
KSRC=/usr/src/linux
MODDIR=.

config:
	$(checkdir)
	./configure --prefix=/usr
	touch config

build: config
	make
	touch build

clean: 
	test -f debian/rules
	test `id -u` = "0"
	cp -f debian/control.normal debian/control
	-rm -f config build debian/VERSION
	-rm -rf debian/files debian/tmp
	-make clean
	-rm -f config.cache config.log config.status Makefile.conf
	-rm -f utils/alsadriver.spec

binary: binary-indep binary-arch

binary-indep: binary-source binary-common

binary-arch:

kdist_image: build 
	$(MAKE) $(MFLAGS) -f debian/rules MODDIR=$(KSRC)/.. binary-modules

############################ source stuff #################################
binary-source:
# Perform some tests
	test -f debian/rules
	test `id -u` = "0"

# Setup everything first
	-rm -rf debian/tmp debian/substvars
	install -d debian/tmp

# Clean up the sources	
	install -d debian/tmp/usr/src/modules/alsadriver
	find . \( -name \*.o -path ./debian/tmp \) -prune -o -print | \
		cpio -admp debian/tmp/usr/src/modules/alsadriver
	( cd debian/tmp/usr/src/modules/alsadriver; $(MAKE) -f debian/rules clean )

# Documentation
	install -d -o root -g root -m 755 debian/tmp/usr/doc/alsa-source/
	install -p -o root -g root -m 644 README TODO INSTALL \
		debian/tmp/usr/doc/alsa-source
	install -p -o root -g root -m 644 debian/README \
		debian/tmp/usr/doc/alsa-source/README.Debian
	install -p -o root -g root -m 644 CHANGELOG \
		debian/tmp/usr/doc/alsa-source/changelog

# Install control files
	install -d -o root -g root -m 755 debian/tmp/DEBIAN
	install -p -o root -g root -m 755 debian/control debian/tmp/DEBIAN

# And now.. for the final packaging!
	find debian/tmp -type f | grep -v "./DEBIAN" | xargs md5sum | \
		sed -e 's#debian/tmp/##' > debian/tmp/DEBIAN/md5sums

	dpkg-gencontrol -palsa-source
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp ..

############################ common stuff #################################

binary-common:
# Perform some tests
	test -f debian/rules
	test `id -u` = "0"

# Setup everything first
	-rm -rf debian/tmp debian/substvars
	install -d debian/tmp

# Device stuff
	install -d debian/tmp/dev
	ln -s /proc/asound/dev debian/tmp/dev/snd

# Include-files
	install -d -o root -g root -m 755 debian/tmp/usr/include
	install -p -o root -g root -m 755 include/asound.h debian/tmp/usr/include
	install -p -o root -g root -m 755 include/asoundid.h debian/tmp/usr/include

# Install documentation
	install -d -o root -g root -m 755 debian/tmp/usr/doc/alsa-modules/
	install -p -o root -g root -m 644 README README.kernel \
		FAQ TODO WARNING doc/SOUNDCARDS doc/*txt \
		debian/tmp/usr/doc/alsa-modules
	gzip -9 debian/tmp/usr/doc/alsa-modules/coding.txt
	install -p -o root -g root -m 644 CHANGELOG \
		debian/tmp/usr/doc/alsa-modules/changelog
	gzip -9 debian/tmp/usr/doc/alsa-modules/changelog

# Install the manual
	install -d -o root -g root -m 755 debian/tmp/usr/doc/alsa-modules/html
	install -p -o root -g root -m 644 doc/*html \
			debian/tmp/usr/doc/alsa-modules/html
	gzip -9 debian/tmp/usr/doc/alsa-modules/html/*

# Install Debian-specific items
	# Manpages for undocumented stuff
	install -d -o root -g root -m 755 debian/tmp/usr/man/man8
	ln -s ../man7/undocumented.7.gz debian/tmp/usr/man/man8/alsaconfig.8.gz
	# Menu support
	install -d -o root -g root -m 755 debian/tmp/usr/lib/menu
	install -p -o root -g root -m 644 debian/menu debian/tmp/usr/lib/menu/alsa
	# Documentation
	install -d -o root -g root -m 755 debian/tmp/usr/doc/alsa-modules
	install -p -o root -g root -m 644 debian/changelog \
		debian/tmp/usr/doc/alsa-modules/changelog.Debian
	gzip -9	debian/tmp/usr/doc/alsa-modules/changelog.Debian
	install -p -o root -g root -m 644 debian/copyright \
		debian/tmp/usr/doc/alsa-modules
	# Init-stuff
	install -d -o root -g root -m 755 debian/tmp/etc/init.d
	install -p -o root -g root -m 755 debian/init debian/tmp/etc/init.d/alsa

# Install our own extra's
	install -d -o root -g root -m 755 debian/tmp/usr/sbin
	install -p -o root -g root -m 755 debian/alsaconfig debian/tmp/usr/sbin

# Install control files
	install -d -o root -g root -m 755 debian/tmp/DEBIAN
	install -p -o root -g root -m 755 debian/control debian/tmp/DEBIAN
	install -p -o root -g root -m 755 debian/conffiles debian/tmp/DEBIAN
	install -p -o root -g root -m 755 debian/postinst debian/tmp/DEBIAN
	install -p -o root -g root -m 755 debian/preinst debian/tmp/DEBIAN
	install -p -o root -g root -m 755 debian/prerm debian/tmp/DEBIAN

# And now.. for the final packaging!
	find debian/tmp -type f | grep -v "./DEBIAN" | xargs md5sum | \
		sed -e 's#debian/tmp/##' > debian/tmp/DEBIAN/md5sums

	dpkg-gencontrol -palsa-modules
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp ..

############################ modules stuff #################################

binary-modules:
# Perform some tests
	test -f debian/rules
	test `id -u` = "0"

# Setup everything first
	-rm -rf debian/tmp debian/substvars
	install -d debian/tmp

# Install the software
	install -d -o root -g root -m 755 debian/tmp/usr/include/linux
	make INSTROOT=debian/tmp install

# Fix some stuff up
	install -d -o root -g root -m 755 debian/tmp/usr/doc
	rm -rf debian/tmp/usr/include
	sh debian/prep-modules $(KSRC)

# Install control files
	install -d -o root -g root -m 755 debian/tmp/DEBIAN
	install -p -o root -g root -m 755 debian/postinst.mod debian/tmp/DEBIAN

# And now.. for the final packaging!
	find debian/tmp -type f | grep -v "./DEBIAN" | xargs md5sum | \
		sed -e 's#debian/tmp/##' > debian/tmp/DEBIAN/md5sums

	dpkg-gencontrol -p`ls -1 debian/tmp/usr/doc` -v`cat debian/VERSION`
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp $(MODDIR)

.PHONY: binary binary-arch binary-indep binary-modules kdist_image clean

