#!/usr/bin/make -f
# based on the sample debian/rules file for debhelper by Joey Hess.

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

build: build-stamp
build-stamp:
	$(checkdir)
	-mkdir modules
	-mkdir include/linux/modules
	make LINUX_LOCATION=$(KSRC) 
	touch build-stamp

clean:
	test -f debian/rules
	test `id -u` = "0"
	-rm -f build-stamp debian/VERSION
	-rm -rf debian/files debian/tmp
	-make clean

binary: binary-indep binary-arch

binary-indep: binary-source 

binary-arch:

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

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 and install them
	install -d debian/tmp/usr/src/modules/ftape
	find . \( -name \*.o -path ./debian/tmp \) -prune -o -print | \
		cpio -admp debian/tmp/usr/src/modules/ftape
	-rm -f debian/tmp/usr/src/modules/ftape/modules/*.o 
	( cd debian/tmp/usr/src/modules/ftape; $(MAKE) -f debian/rules clean )

# Documentation
	install -d -o root -g root -m 755 debian/tmp/usr/doc/ftape-source
	install -p -o root -g root -m 644 debian/copyright \
		debian/tmp/usr/doc/ftape-source
	install -p -o root -g root -m 644 README BUGS INSTALL README.PCI \
		RELEASE-NOTES TODO debian/tmp/usr/doc/ftape-source
	install -p -o root -g root -m 644 ChangeLog \
		debian/tmp/usr/doc/ftape-source/changelog
	install -p -o root -g root -m 644 debian/changelog \
		debian/tmp/usr/doc/ftape-source/changelog.Debian
	gzip -9v debian/tmp/usr/doc/ftape-source/{RELEASE-NOTES,changelog.Debian,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 ... packaging !
	find debian/tmp -type f | grep -v "./DEBIAN" | xargs md5sum | \
		sed -e 's#debian/tmp/##' > debian/tmp/DEBIAN/md5sums

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

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 modules
	make DEST=`pwd`/debian/tmp install

# Documentation
	install -d -o root -g root -m 755 debian/tmp/usr/doc
	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/control \
		debian/tmp/DEBIAN
	install -p -o root -g root -m 755 debian/module.preinst \
		debian/tmp/DEBIAN/preinst
	install -p -o root -g root -m 755 debian/module.postinst \
		debian/tmp/DEBIAN/postinst
	install -p -o root -g root -m 755 debian/module.postrm \
		debian/tmp/DEBIAN/postrm

# And now ... 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: build clean binary-indep binary-arch binary


