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

ifdef KDREV
FULLVER := $(shell dpkg-parsechangelog |grep ^Version | cut -f2 -d' ')+$(KDREV)
DEBDEP := (= $(KDREV))
else
FULLVER := $(shell dpkg-parsechangelog |grep ^Version | cut -f2 -d' ')
DEBDEP :=
endif

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

# this is the modules package target
ifdef KSRC
MOD_DESTDIR := --destdir=$(KSRC)/..
endif

# I like the Kernel module to be built anyways. -KK
## ifeq ($(shell ls knoppix 2>/dev/null|| true ),knoppix)
## Good morning Mr. Knopper, using your settings
TARGETDIR=lib/modules/`uname -r`/kernel/drivers/block
KVERS := module
KSRC := /usr/src/linux
MOD_DESTDIR := --destdir=$(CURDIR)/..
binary: forknoppix
## else
## binary: binary-indep binary-arch
## endif

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

forknoppix:
	debian/rules clean ; debian/rules binary-indep ; debian/rules binary-arch ; debian/rules clean ; debian/rules kdist_image

kdist_clean: clean

kdist_configure:
	dh_testdir
	cat debian/{control.head,control.mod} | $(gencontrol)
	cp debian/templates.template debian/cloop-$(KVERS).templates
	cp debian/cloop.postinst.template debian/cloop-$(KVERS).postinst
	cp debian/cloop.config.template debian/cloop-$(KVERS).config

configure: configure-stamp
configure-stamp:
	dh_testdir
	cat debian/{control.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) zcode create_compressed_fs extract_compressed_fs APPSONLY=yes
	touch build-stamp

build-mod: kdist_configure
	dh_testdir
	$(MAKE) zcode cloop.o KERNEL_DIR=$(KSRC)
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	-$(MAKE) clean APPSONLY=yes
	-dh_clean
	rm -rf debian/cloop-?.?.?* debian/cloop 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 extract_compressed_fs $(CURDIR)/debian/cloop-utils/usr/bin/
	dh_installman debian/create_compressed_fs.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 cloop-$(KVERS) $(TARGETDIR)
	install -m 644 cloop.o $(CURDIR)/debian/cloop-$(KVERS)/$(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
	-cd zlib-1.1.4; make clean
	echo $$UID
	tar -cf - {Makefile,*.[ch],debian/{control*,copyright,rules,dirs,postrm,README.Debian,templates.template,cloop.postinst.template,cloop.config.template,changelog,genchanges.sh},CHANGELOG,README,zlib-1.1.4} | 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: clean build-mod install-mod build-deb

kdist_image: kdist_targets clean

kdist: kdist_targets
	# Build a .changes file.
	KVERS="$(KVERS)" KSRC="$(KSRC)" KMAINT="$(KMAINT)" \
				 REV="$(KDREV)" KEMAIL="$(KEMAIL)" sh -v debian/genchanges.sh
	# Only now can we clean up.
	debian/rules clean

build-deb:
	dh_testdir -a
	dh_testroot -a
	dh_installdebconf -a
	dh_installdocs -a README debian/README.Debian
	dh_installexamples -a
	dh_installmenu -a
#	dh_installlogrotate -a
#	dh_installemacsen -a
#	dh_installpam -a
#	dh_installmime -a
#	dh_installinit -a
	dh_installcron -a
#	dh_installman -a
	dh_installinfo -a
	dh_installchangelogs CHANGELOG -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
#	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_gencontrol -a -- -v$(FULLVER)
	dh_md5sums -a
	dh_builddeb -a $(MOD_DESTDIR)

build-indep: configure-stamp install-src
	dh_testdir -i
	dh_testroot -i
#	dh_installdebconf -i
	dh_installdocs -i README debian/README.Debian
	dh_installexamples -i
	dh_installmenu -i
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_installinit -i
	dh_installcron -i
#	dh_installman -i
	dh_installinfo -i
	dh_installchangelogs CHANGELOG -i
	dh_strip -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
#	dh_makeshlibs -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
