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

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

VERS = $(shell sed -ne '1s/.*(\(.*\)).*/\1/p' debian/changelog)

# KSRC is the location of the kernel source. This is the default value,
# when make-kpkg is used it will supply to real value
KSRC            = /usr/src/linux

# KDREV is the package-revision, as given to make-kpkg by the user.
# Just put a simply default value in here which we use when we test
# the packagebuilding without make-kpkg
KDREV           = "Custom.1.00"

# Separate the epoch from the normal revision number in KDREV
# for use with dh_gencontrol
KDREV_EPOCH = $(shell echo $(KDREV) | sed -ne '1s/\([^:]*:\)\?\(.*\)/\1/p')
KDREV_REV   = $(shell echo $(KDREV) | sed -ne '1s/\([^:]*:\)\?\(.*\)/\2/p')

# Now we need to get the kernel-version somehow
KVERS=`sed -n -e '/UTS_RELEASE/s/^[^"]*"\([^"]*\)".*$$/\1/p' $(KSRC)/include/linux/version.h`

SED_SCRIPT=s!\$$KVERS!$(KVERS)!g; \
       s!\$$KSRC!$(KSRC)!; \
       s!\$$KEMAIL!$(KEMAIL)!; \
       s!\$$KMAINT!$(KMAINT)!; \
       s!\$$KDREV!$(KDREV)!; \
       s!\$$DEBDATE!$(shell date +"%a, %d %b %Y %H:%M:%S %z")!

debian/control: debian/control.in
	sed -e "$(SED_SCRIPT)" debian/control.in > $@

.PHONY: debian/control

SHELL=/bin/bash

.PHONY: configure_mod
configure_mod:
	sed 's/#KVERS#/$(KVERS)/g' debian/control.template | sed 's/#KDREV#/$(KDREV)/g' > $(CURDIR)/debian/control
	sed 's/#KVERS#/$(KVERS)/g' debian/dvb-driver.config.template > $(CURDIR)/debian/dvb-driver-$(KVERS).config
	sed 's/#KVERS#/$(KVERS)/g' debian/templates.template > $(CURDIR)/debian/templates
	sed 's/#KVERS#/$(KVERS)/g' debian/prerm.template > $(CURDIR)/debian/prerm
	sed 's/#KVERS#/$(KVERS)/g' debian/postinst.01.template > $(CURDIR)/debian/postinst.01
	sed 's/#KVERS#/$(KVERS)/g' debian/postinst.04.template > $(CURDIR)/debian/postinst.04
	cat driver/makedev.napi > debian/postinst.02
	cat debian/postinst.0? > debian/postinst
	dh_testdir
	dh_testroot
	touch configure_mod-stamp


.PHONY: build_mod
build_mod:
	dh_testdir
	dh_testroot
	cd driver ; $(MAKE) #-f Makefile.debian

	touch build_mod-stamp


.PHONY: install_mod
install_mod:
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs -p dvb-driver-$(KVERS) usr/lib/dvd lib/modules/$(KVERS)/misc lib/modules/$(KVERS)-dvb etc/modutils etc/devfs/symlinks.d etc/devfs/conf.d lib/dvb
	
	cp -a driver/*.o debian/dvb-driver-$(KVERS)/lib/modules/$(KVERS)/misc
	
	cp driver/av7110/*.o debian/dvb-driver-$(KVERS)/lib/modules/$(KVERS)/misc
	cp -a driver/av7110/Root driver/av7110/Dpram debian/dvb-driver-$(KVERS)/lib/modules/$(KVERS)-dvb/
	
	cp -a driver/b2c2/skystar2.o debian/dvb-driver-$(KVERS)/lib/modules/$(KVERS)/misc
	
	cp driver/frontends/*.o debian/dvb-driver-$(KVERS)/lib/modules/$(KVERS)/misc
	
	cp driver/modules.conf debian/dvb-driver-$(KVERS)/etc/modutils/dvb-$(KVERS)
	
	cp debian/dvb.symlinks.d debian/dvb-driver-$(KVERS)/etc/devfs/symlinks.d/dvb-driver-$(KVERS)
	cp debian/dvb.conf.d debian/dvb-driver-$(KVERS)/etc/devfs/conf.d/dvb-driver-$(KVERS)
	

.PHONY: binary_modules_mod
binary_modules_mod:
	dh_testdir
	dh_testroot
	cd $(CURDIR)
	dh_installdebconf	-n
	dh_installmodules
	dh_installchangelogs 
	dh_installdocs debian/README
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(KDREV_EPOCH)$(VERS)+$(KDREV_REV)
	dh_md5sums
	dh_builddeb --destdir=$(KSRC)/..


.PHONY: clean_mod
clean_mod:
	dh_testdir
	dh_testroot
	rm -f build_mod-stamp configure_mod-stamp

	# Add here commands to clean up after the build process.
	-cd driver ; $(MAKE) clean #-f Makefile.debian clean
	rm -rf dvb_firm.h fdump debian/dvb-driver-*config debian/postinst

	-dh_clean

# The kdist_configure target is called by make-kpkg modules_config. It
# should configure the module so it is ready for compilation (mostly
# useful for calling configure)
.PHONY: kdist_config
kdist_config: configure_mod


# the kdist_image target is called by make-kpkg modules_image. It is
# responsible for compiling the module and creating the package. It
# should also clean up after making the module. Please note we use a
# seperate binary-modules target to make testing the package building
# easier
.PHONY: kdist_image
kdist_image: configure_mod build_mod install_mod binary_modules_mod clean_mod

.PHONY: kdist
kdist: kdist_image

# the kdist_clean target is called by make-kpkg modules_clean. It is
# responsible for cleaning up any changes that have been made by the
# other kdist_commands (except for the .deb files created).
.PHONY: kdist_clean
kdist_clean: clean_mod
