#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

configure: configure-stamp
configure-stamp:
	dh_testdir
	
	# add stuff for configuration

	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir

	#$(MAKE)

	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp

	# Add here commands to clean up after the build process.
	#-$(MAKE) clean

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	cd $(CURDIR)
	mkdir -p $(CURDIR)/debian/cdfs-src/usr/src/modules/cdfs/debian
	for x in cdfs*bz2 ; do bzcat $$x | tar x -f- -C $(CURDIR)/debian/cdfs-src/usr/src/modules/ ; done
	cd $(CURDIR)/debian/cdfs-src/usr/src/modules && mv cdfs-2.6.* cdfs/2.6 && mv cdfs-2.4* cdfs/2.4
	cat debian/cdfs.include.diff | patch $(CURDIR)/debian/cdfs-src/usr/src/modules/cdfs/2.6/cdfs.h
	cp -a debian/*modules.in* debian/rules debian/compat debian/*KVERS* debian/changelog debian/copyright \
		$(CURDIR)/debian/cdfs-src/usr/src/modules/cdfs/debian/
	#cp patch.cdfs debian/cdfs-src/usr/src/modules/cdfs/
	dh_fixperms
	cd $(CURDIR)/debian/cdfs-src/usr/src && tar -f- -c modules | bzip2 -9 > cdfs.tar.bz2 && rm -rf modules

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do by default.

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
#	dh_installdebconf	
	dh_installdocs
	dh_installexamples
#	dh_installmanpages
#	dh_undocumented
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
#	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb


# Name of package
PACKAGE         = cdfs
# modifieable for experiments or debugging m-a
MA_DIR ?= /usr/share/modass
# load generic variable handling
-include $(MA_DIR)/include/generic.mk
# load default rules
-include $(MA_DIR)/include/common-rules.mk
export CC

kdist_config: prep-deb-files

binary-modules: prep-deb-files
	cd $(CURDIR)
	mkdir -p debian/cdfs-$(KVERS)/lib/modules/$(KVERS)/kernel/fs
ifeq ($(shell echo $(KVERS) | sed -e 's/\(...\).*/\1/'),2.6)
	cd 2.6 && make KDIR=$(KSRC)
#	$(LD) $(LDFLAGS) $(EXTRA_LDFLAGS) -r -o cdfs.ko 2.6/cdfs.o
	cp 2.6/cdfs.ko debian/cdfs-$(KVERS)/lib/modules/$(KVERS)/kernel/fs/
else
	cd 2.4 && ./configure --with-kernel-dir="$(KSRC)" && make CONFIG_CDFS_KERNELDIR="$(KSRC)"
	cp 2.4/cdfs.o debian/cdfs-$(KVERS)/lib/modules/$(KVERS)/kernel/fs/
endif
#	dh_installdebconf	
	dh_installdocs 2.4/cdfs.html 2.4/cdfs.png
#	dh_installexamples
#	dh_installmanpages
#	dh_undocumented
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	# You may want to make some executables suid here.
#	dh_suidregister
#	dh_makeshlibs
	dh_installdeb
#	dh_perl
#	dh_shlibdeps
	dh_gencontrol  -- -v$(VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(DEB_DESTDIR)

.PHONY: clean
kdist_clean:
	-dh_testdir
	-dh_testroot
	rm -f build-stamp configure-stamp
	-dh_clean
	-rm -f $(CURDIR)/debian/control
	-rm -rf $(CURDIR)/debian/cdfs-2.* *.o *.ko */*.o */*.ko
	$(MAKE) clean || true
	-cd 2.6 && $(MAKE) clean
	-cd 2.4 && $(MAKE) clean


binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure
