#!/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.make

#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

kdist_clean: clean

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)

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 APPSONLY=yes
	touch build-stamp

build-mod: kdist_configure
	CC=$(CC) $(MAKE) cloop.o KERNEL_DIR=$(KSRC)

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

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 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
#	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-mod: build-mod
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -p $(PKGNAME) $(TARGETDIR)
	install -m 644 cloop.o $(CURDIR)/debian/$(PKGNAME)/$(TARGETDIR)
	rm cloop.o compressed_loop.o

install-src: configure-stamp
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -p cloop-src usr/src/modules/cloop
	echo $$UID
	tar -cf - {Makefile,*.[ch],debian/{po,*template*,control*,copyright,rules,dirs,*.in,*_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_targets: kdist_clean build-mod install-mod build-moddeb

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-moddeb:
	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)

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:
