#!/usr/bin/make -f
# based on the sample debian/rules file from pcmcia-cs package by Brian Mays.

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

build:
#There's nothing to do here

build-modules: build-stamp
build-stamp:
	$(checkdir)
	make LINUX_LOCATION=$(KSRC) 
	touch build-stamp

clean:
	test -f debian/rules
	test `id -u` = "0"
	-rm -f build-stamp debian/KVERS debian/MODVERS
	-rm -rf debian/files debian/tmp
	-cp -f debian/control.source debian/control
	-make distclean
	rm -f debian/module.*
	rm -f debian/mod.postrm debian/mod.preinst
	rm -f modules/*.o

clean-modules:
	test -f debian/rules
	test `id -u` = "0"
	-rm -rf debian/tmp build-stamp debian/KVERS debian/MODVERS
	-make distclean

kdist_clean: clean clean-modules

binary: binary-indep binary-arch

binary-indep:

binary-arch: binary-source
	test `id -u` = "0"
# snarfed this cool setup from the pcmcia package (again), thanks Brian ;-)
	set -e; KPATH=$(KPATH); \
	if [ "$$KPATH" ]; then \
	  for k in `IFS=':'; echo $$KPATH`; do \
	    test ! -d $$k || \
	    $(MAKE) $(MFLAGS) -f debian/rules KSRC="$$k" binary-modules clean-modules; \
	  done; \
	else \
	  test ! -d $(KSRC) || $(MAKE) $(MFLAGS) -f debian/rules binary-modules clean-modules; \
	fi
#	echo "**** previous line modif. by A. Martin ********"; \

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

kdist: kdist_image
	KSRC="$(KSRC)" KMAINT="$(KMAINT)" KEMAIL="$(KEMAIL)" \
		sh -v debian/genchanges.sh

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/share/doc/ftape-source
	install -d -o root -g root -m 755 debian/tmp/usr/share/doc/ftape-source/examples
	install -p -o root -g root -m 644 debian/copyright \
		debian/tmp/usr/share/doc/ftape-source
	install -p -o root -g root -m 644 README BUGS README.PCI \
		RELEASE-NOTES TODO debian/tmp/usr/share/doc/ftape-source
	install -p -o root -g root -m 644 ChangeLog \
		debian/tmp/usr/share/doc/ftape-source/changelog
	install -p -o root -g root -m 644 debian/changelog \
		debian/tmp/usr/share/doc/ftape-source/changelog.Debian
	install -p -o root -g root -m 644 debian/README.Debian \
		debian/tmp/usr/share/doc/ftape-source/README.Debian
	install -p -o root -g root -m 755 modules/insert \
		debian/tmp/usr/share/doc/ftape-source/examples
	install -p -o root -g root -m 644 modules/isapnp.conf \
		debian/tmp/usr/share/doc/ftape-source/examples
	install -p -o root -g root -m 755 modules/remove \
		debian/tmp/usr/share/doc/ftape-source/examples
	gzip -9v debian/tmp/usr/share/doc/ftape-source/RELEASE-NOTES
	gzip -9v debian/tmp/usr/share/doc/ftape-source/changelog.Debian
	gzip -9v debian/tmp/usr/share/doc/ftape-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
	install -p -o root -g root -m 755 debian/prerm debian/tmp/DEBIAN
	install -p -o root -g root -m 755 debian/postinst 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 -isp
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp ..

binary-modules: build-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 LINUX_LOCATION=$(KSRC) DEST=`pwd`/debian/tmp install

# Documentation
	install -d -o root -g root -m 755 debian/tmp/usr/share/doc

# Maintainer scripts and some other changes
	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
	install -p -o root -g root -m 755 debian/module.prerm \
		debian/tmp/DEBIAN/prerm
	install -p -o root -g root -m 755 debian/module.templates \
		debian/tmp/DEBIAN/templates
	install -p -o root -g root -m 755 debian/module.config \
		debian/tmp/DEBIAN/config

# 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/share/doc` -v`cat debian/MODVERS` -isp
	chown -R root.root debian/tmp
	chmod -R go=rX debian/tmp
	dpkg --build debian/tmp $(MODDIR)

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


