#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.

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

INSTALL=/usr/bin/install
psource:=batmand-gateway-source
dkmssource:=batmand-gateway-dkms
sname:=batmand-gateway

version:= $(shell dpkg-parsechangelog | grep ^Version: | cut -d' ' -f2 \
            | cut -d- -f1)

### KERNEL SETUP
### Setup the stuff needed for making kernel module packages
### taken from /usr/share/kernel-package/sample.module.rules

PACKAGE=batmand-gateway-modules
MA_DIR=/usr/share/modass
-include $(MA_DIR)/include/generic.make
-include $(MA_DIR)/include/common-rules.make

kdist_config: prep-deb-files

# the kdist_clean target is called by make-kpkg modules_clean and from
# kdist* rules. It is responsible for cleaning up any changes that have
# been made by the other kdist_commands (except for the .deb files created)
#kdist_clean: clean
kdist_clean:
	$(MAKE) $(MFLAGS) -f debian/rules clean
	$(MAKE) -C $(KSRC) M=$(CURDIR) clean

### end  KERNEL SETUP

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

binary-modules: binary-modules-stamp
	dh_testdir

# the binary-modules rule is invoked by module-assistant while processing the
# kdist* targets. It is called by module-assistant or make-kpkg and *not*
# during a normal build
binary-modules-stamp:
	dh_testroot
	dh_prep
	dh_installdirs lib/modules/$(KVERS)/kernel/net

	# Build the module
	$(MAKE) -C $(KSRC) M=$(CURDIR)

	# Install the module
	cp batgat.$ko debian/$(PKGNAME)/lib/modules/$(KVERS)/kernel/net

	dh_installdocs
	dh_installchangelogs
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(DEB_DESTDIR)
	dh_prep
	touch $@

build: build-stamp

build-stamp:
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) CFLAGS="$(CFLAGS)"
	#docbook-to-man debian/batmand.sgml > batmand.1

	touch $@

clean:
	dh_testdir
	dh_testroot

	# Add here commands to clean up after the build process.
	# clean can be run from module assistant or the original package rules file.
	if [ ! -r mod_batman.c ]; then \
		$(MAKE) clean; \
	fi

	dh_clean 

install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

	# Add here commands to install the package into debian/batmand.
	$(MAKE) INSTALL_PREFIX=$(CURDIR)/debian/batmand install
	$(INSTALL) -m 0644 -D  $(CURDIR)/debian/batmand-default $(CURDIR)/debian/batmand/etc/default/batmand

	# Create the directories to install the source into
	dh_installdirs -p$(psource)  usr/src/modules/$(sname)/debian
	dh_installdirs -p$(dkmssource)  usr/src/$(sname)-$(version)

	# Copy only the driver source to the proper location
	cp linux/modules/*.c linux/modules/*.h linux/modules/Makefile*  \
		debian/$(psource)/usr/src/modules/$(sname)
	cp linux/modules/*.c linux/modules/*.h linux/modules/Makefile*  \
		debian/$(dkmssource)/usr/src/$(sname)-$(version)
	mv debian/$(psource)/usr/src/modules/$(sname)/Makefile.kbuild \
	   debian/$(psource)/usr/src/modules/$(sname)/Makefile
	mv debian/$(dkmssource)/usr/src/$(sname)-$(version)/Makefile.kbuild \
	   debian/$(dkmssource)/usr/src/$(sname)-$(version)/Makefile
	# Copy the needed debian/ pieces to the proper location
	cp debian/*modules.in* \
		debian/$(psource)/usr/src/modules/$(sname)/debian
	cp debian/changelog debian/copyright debian/rules\
		debian/compat debian/control debian/$(psource)/usr/src/modules/$(sname)/debian/

	mkdir -p debian/$(psource)/usr/share/doc/$(psource)
	cp debian/copyright debian/$(psource)/usr/share/doc/$(psource)
	mkdir -p debian/$(dkmssource)/usr/share/doc/$(dkmssource)
	cp debian/copyright debian/$(dkmssource)/usr/share/doc/$(dkmssource)

	mkdir -p debian/$(psource)$(MA_DIR)/packages/
	cd debian/$(psource)$(MA_DIR)/packages/ && ln -sf default.sh $(psource)
	cd debian/$(psource)/usr/src && tar c modules | bzip2 -9 > $(sname).tar.bz2 && rm -rf modules

	dh_dkms -V
	dh_install


# Build architecture-independent files here.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs  -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_installdeb -i
	dh_gencontrol -i
	dh_md5sums -i
	dh_builddeb -i

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir -s
	dh_testroot -s
	dh_installchangelogs -s CHANGELOG
	dh_installdocs -s
	dh_installexamples -s
	dh_lintian -s
#	dh_install -s
	dh_installinit -s -u"start 20 2 3 4 5 . stop 20 1 ."
	dh_installman -s
	dh_link -s
	dh_strip -s --dbg-package=batmand-dbg
	dh_compress -s
	dh_fixperms -s
	dh_installdeb -s
	dh_shlibdeps -s
	dh_gencontrol -s
	dh_md5sums -s
	dh_builddeb -s

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install binary-modules kdist kdist_clean
