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

SHELL=/bin/bash

configure: configure-stamp
configure-stamp:
	dh_testdir
	cd debian; cp control.big control
	# Add here commands to configure the package.
	touch configure-stamp

docs-stamp:
	cd doc/dvbapi ; yes "" | make
	touch docs-stamp

mpegtools-stamp:
	cd apps/mpegtools/; make
	touch docs-stamp

szap-stamp:
	cd libdvb ; make
	cd apps/szap/; make
	touch docs-stamp

tuxzap-stamp: szap-stamp
	cd apps/tuxzap/; ./autogen.sh --prefix=/usr  --with-dvb-path=$(CURDIR)/libdvb ; make
	touch tuxzap-stamp


build: configure-stamp build-stamp
build-stamp: docs-stamp mpegtools-stamp szap-stamp tuxzap-stamp
	dh_testdir
	# Add here commands to compile the package.
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f *-stamp

	# Add here commands to clean up after the build process.
	cd debian; cp control.big control
	-$(MAKE) clean
	-cd driver ; make clean
	-cd apps/tuxzap ; make distclean
	rm -rf apps/tuxzap/{config.guess,config.sub,config.cache,install-sh,missing,mkinstalldirs} libdvb/merge_dvb libdvb/conv driver/*.o driver/dvb_firm.h driver/fdump apps/tuxzap/src/Makefile.in apps/tuxzap/aclocal.m4 apps/tuxzap/config.h.in apps/tuxzap/Makefile.in apps/tuxzap/configure
	-cd doc/dvbapi ; make clean
	-cd apps/szap ; make clean
	find apps/tuxzap/depcomp debian/ -type l | xargs rm -f

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	# apparently bug in debhelper
	DH_COMPAT=1; dh_installdirs

	# install stuff into debian/dvb-dev.
	cp -a libdvb/include debian/dvb-dev/usr
	cp -a ost/include debian/dvb-dev/usr
	cp -a libdvb/*.a debian/dvb-dev/usr/lib

	# install stuff into debian/dvb-zapping.
	cd apps/tuxzap/src; make install prefix=../../../debian/dvb-zapping/usr
	cp -a apps/szap/szap debian/dvb-zapping/usr/bin

	# install stuff into debian/dvb-mpegtools.
	cp -a $$(find apps/mpegtools/ -type l) apps/mpegtools/mpegtools  apps/mpegtools/mplex debian/dvb-mpegtools/usr/bin
	(cd debian/dvb-mpegtools/usr/bin; for x in analyze cut_mpg es_demux mpegtools mplex pes2aud pes2aud_es pes2ts pes2ts2 pes2vid pes2vid_es pes_demux pesplot pes_repack ps2vid remux split_mpg streamtype ts2es ts2pes ts2ps ts_demux ts_es_demux tspids ; do ln -s mplex $x || true; done)


	# create the -src package.
#	cd driver && make dvb_firm.h-build
	cp -a driver debian/dvb-driver-source/usr/src/modules/dvb-driver/
#	rm debian/dvb-driver-source/usr/src/modules/dvb-driver/dvb_firm.h
	cat debian/patch-* | (cd debian/dvb-driver-source/usr/src/modules/dvb-driver/ ; patch -p1)
#	rm debian/dvb-driver-source/usr/src/modules/dvb-driver/driver/Root debian/dvb-driver-source/usr/src/modules/dvb-driver/driver/Dpram
	cp -a debian/dvb-driver.debian/* debian/dvb-driver-source/usr/src/modules/dvb-driver/debian/
	ln -s /usr/include/ost debian/dvb-driver-source/usr/src/modules/dvb-driver
	cp -a debian/copyright* debian/rules debian/dvb-driver-source/usr/src/modules/dvb-driver/debian/
	dh_installdebconf -pdvb-driver debian/copyright*
	chmod +x debian/dvb-driver-source/usr/src/modules/dvb-driver/debian/rules
#	sed -e 's/^dvb /dvb-driver-source /' < debian/changelog > debian/dvb-driver-source/usr/src/modules/dvb-driver/debian/changelog
	cp debian/changelog debian/dvb-driver-source/usr/src/modules/dvb-driver/debian/changelog
	cd  debian/dvb-driver-source/usr/src/ ; tar -c modules/dvb-driver | gzip -9 > dvb-driver.tgz
	rm -rf debian/dvb-driver-source/usr/src/modules/dvb-driver
	
	dh_movefiles

	find debian | grep -i CVS | xargs rm -rf
	find debian/dvb-dev debian/dvb-driver-source/usr/src/modules/ -type f |grep -v rules| xargs chmod -x

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -a
	dh_testroot -a
#	dh_installdebconf -a
	dh_installdocs -a
	dh_installdocs -p dvb-mpegtools apps/mpegtools/README
	# use a different name
	cp apps/tuxzap/README debian/tmp/README.tuxzap
	dh_installdocs -p dvb-zapping debian/tmp/README.tuxzap
	# install the README from dvb-driver.debian
	cp driver/gnufirm/README debian/tmp/README.gnufirm
	dh_installdocs -p dvb-driver-source debian/dvb-driver.debian/README.Debian debian/tmp/README.gnufirm
	cat doc/README.* > debian/dvb-driver.debian/README
	dh_installexamples -p dvb-zapping apps/dvb* apps/lir* apps/szap/{vdr*,channels.conf*}
	dh_installmenu -a
	dh_installcron -a
	dh_installinfo -a
	dh_installman -p dvb-zapping debian/tuxzap.1
	for x in ntuxplayer ntuxzap rtuxzap szap tuxplayer tuxview ; do ln -s tuxzap.1 debian/dvb-zapping/usr/share/man/man1/$$x.1; done
	dh_installman -p dvb-mpegtools debian/mpegtools.1
	for x in mplex `find apps/mpegtools/ -type l | cut -f3 -d"/"` ; do ln -s mpegtools.1 debian/dvb-mpegtools/usr/share/man/man1/$$x.1; done
	dh_installchangelogs  -a
	dh_strip -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_fixperms -p dvb-dev
#	dh_makeshlibs -a
	dh_installdeb -a
#	dh_perl -a
	dh_shlibdeps -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

#ifneq ($(KSRC), )
#binary: kdist_image
#else
binary: binary-indep binary-arch
#binary: binary-arch
#endif
.PHONY: build clean binary-indep binary-arch binary install configure

.PHONY: configure_mod
configure_mod:
	-cd debian; cp -s dvb-driver.debian/* .
	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
	-rm driver/dvb_firm.o
	mv driver/tuner.o driver/dvb_tuner.o
	cp -a driver/*.o debian/dvb-driver-$(KVERS)/lib/modules/$(KVERS)/misc
	cp driver/modules.conf debian/dvb-driver-$(KVERS)/etc/modutils/dvb-$(KVERS)
	-rm debian/dvb-driver-$(KVERS)/lib/modules/$(KVERS)/misc/dvb_firm.o
	cp -a driver/Root driver/Dpram debian/dvb-driver-$(KVERS)/lib/modules/$(KVERS)-dvb/

.PHONY: binary_modules_mod
binary_modules_mod:
	dh_testdir
	dh_testroot
	cd $(CURDIR)
	dh_installdebconf	-n
#	dh_installexamples
#	dh_installmanpages
#	dh_installinit -n
	dh_installmodules
	dh_installchangelogs 
	dh_installdocs debian/README
	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
	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) -f Makefile.debian clean
	rm -rf dvb_firm.h fdump debian/dvb-driver-*config debian/postinst

	-dh_clean

#	-rm $(CURDIR)/debian/control
#	-rm $(CURDIR)/debian/dirs
#	-rm $(CURDIR)/debian/override
#	-$(MAKE) 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
