#!/usr/bin/make -f
# Sample debian/rules that uses debhelper. 
# GNU copyright 1997 by Joey Hess.
#
# This version is for a hypothetical package that builds an
# architecture-dependant package, as well as an architecture-independent
# package.

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

SHELL=/bin/bash

# This is the debhelper compatability version to use.
export DH_COMPAT=3

PACKAGE=cloop-module
MA_DIR ?= /usr/share/modass
-include $(MA_DIR)/include/generic.mk
-include $(MA_DIR)/include/common-rules.mk

k = $(shell echo $(KVERS) | grep -q ^2.6 && echo k)

#defining some values
TARGETDIR=lib/modules/$(KVERS)/kernel/drivers/block

# I like the Kernel module to be built anyways. -KK
# And I do not! -EB
#ifeq ($(shell ls knoppix 2>/dev/null|| echo $$LOGNAME),knoppix)
## Good morning Mr. Knopper, using your settings
#TARGETDIR=lib/modules/`uname -r`/kernel/drivers/block
#KSRC := /usr/src/linux
#DEB_DESTDIR := $(CURDIR)/..
#PKGNAME = cloop-module
#HEAD := knopper
#binary: forknoppix
#else
HEAD := debian
binary: binary-indep binary-arch
#endif

gencontrol= sed 's/\#KVERS\#/$(KVERS)/g' > $(CURDIR)/debian/control

# first build the module with Knoppix parameters, then continue with
# default rules
forknoppix: clean kdist_image
	$(MAKE) $(MFLAGS) -f debian/rules clean binary-indep binary-arch

configure: configure-stamp
configure-stamp:
	dh_testdir
	cat debian/{control.head.$(HEAD),control.utils,control.src} | $(gencontrol)
	# Add here commands to configure the package.

	touch configure-stamp

build: configure-stamp build-stamp
build-stamp:
	dh_testdir
	$(MAKE) utils
	touch build-stamp

# for m-a
kdist_clean: clean
kdist_config: kdist_configure
kdist_configure: prep-deb-files
	test $(PKGNAME) = cloop-module || cp debian/cloop-module.templates debian/$(PKGNAME).templates
	cat debian/{control.head.$(HEAD),control.mod} | $(gencontrol)
binary-modules: kdist_configure
	CC=$(CC) $(MAKE) cloop.$ko KERNEL_DIR=$(KSRC)
ifneq ($k,k)
	cd 0.68 && CC=$(CC) $(MAKE) cloop.o KERNEL_DIR=$(KSRC)
	cd 1.02 && CC=$(CC) $(MAKE) cloop.o KERNEL_DIR=$(KSRC)
endif
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -p $(PKGNAME) $(TARGETDIR)
	ln -sf cloop.$ko $(CURDIR)/debian/$(PKGNAME)/$(TARGETDIR)/cloop-2.01.$ko
	ln -sf cloop.$ko $(CURDIR)/debian/$(PKGNAME)/$(TARGETDIR)/cloop-2.00.$ko
ifneq ($k,k)
	install -m 644 0.68/cloop.o $(CURDIR)/debian/$(PKGNAME)/$(TARGETDIR)/cloop-0.68.o
	install -m 644 1.02/cloop.o $(CURDIR)/debian/$(PKGNAME)/$(TARGETDIR)/cloop-1.02.o
endif
	install -m 644 cloop.$ko $(CURDIR)/debian/$(PKGNAME)/$(TARGETDIR)
	rm -f cloop.o compressed_loop.o
	dh_testdir -a
	dh_testroot -a
	dh_installdebconf -a
	dh_installdocs -a README debian/README.Debian
	dh_installchangelogs CHANGELOG -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_gencontrol -a -- -v$(VERSION)
	dh_md5sums -a
	dh_builddeb -a --destdir=$(DEB_DESTDIR)

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	-$(MAKE) clean
	-$(MAKE) clean APPSONLY=yes
	-dh_clean
	rm -rf debian/cloop-?.?.?* debian/cloop debian/cloop-module debian/cloop-module-?.?.* debian/*.files knoppix

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -p cloop-utils usr/bin
	install -m 755 create_compressed_fs  $(CURDIR)/debian/cloop-utils/usr/bin/
	install -m 755 advancecomp-1.9_create_compressed_fs/advfs  $(CURDIR)/debian/cloop-utils/usr/bin/
#	install -m 755 compressloop          $(CURDIR)/debian/cloop-utils/usr/bin/
	install -m 755 extract_compressed_fs $(CURDIR)/debian/cloop-utils/usr/bin/
	dh_installman -p cloop-utils $(CURDIR)/debian/create_compressed_fs.1
	cd $(CURDIR)/debian/cloop-utils/usr/share/man/man1 && ln -sf create_compressed_fs.1 extract_compressed_fs.1 && ln -sf create_compressed_fs.1 advfs.1
#	dh_installman $(CURDIR)/debian/compressloop.1
	cd debian/cloop-utils/usr/share/man/man1 && ln -s create_compressed_fs.1.gz extract_compressed_fs.1.gz

install-src: configure-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -p cloop-src usr/src/modules/cloop
	echo $$UID
	tar -cf - {1.02,0.68,Makefile,*.[ch],debian/{po,*template*,control*,copyright,rules,dirs,*_KVERS_*,README.Debian,changelog},CHANGELOG,README} | tar -x -C debian/cloop-src/usr/src/modules/cloop
	dh_fixperms -i
	cd debian/cloop-src/usr/src ; tar -cf - modules | gzip -9 > cloop.tar.gz ; rm -rf modules

binary-arch: build install build-deb

binary-indep: install-src build-indep

#kdist_image: kdist_targets
#	debian/rules kdist_clean

#kdist: kdist_targets genchanges
#	debian/rules kdist_clean

build-deb:
	dh_testdir -a
	dh_testroot -a
	dh_installdebconf -a
	dh_installdocs -a README debian/README.Debian
	dh_installexamples -a
	dh_installchangelogs CHANGELOG -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

build-indep: configure-stamp install-src
	dh_testdir -i
	dh_testroot -i
	dh_installdocs -i README debian/README.Debian
	dh_installexamples -i
	dh_installchangelogs CHANGELOG -i
	dh_strip -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_shlibdeps -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

.PHONY: build clean binary-indep binary-arch binary kdist kdist_image kdist_configure kdist_clean kdist
.NOTPARALLEL:
