#!/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.
#
# This version is for a hypothetical package that can build a kernel modules
# architecture-dependant package via make-kpkg, as well as an
# architecture-independent module source package, and other packages
# either dep/indep for things like common files or userspace components
# needed for the kernel modules.

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

# some default definitions, important!
# 
# Name of the source package
psource:=batman-adv-source
dkmssource:=batman-adv-dkms

# The short upstream name, used for the module source directory
sname:=batman-adv

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

# prefix of the target package name
PACKAGE=batman-adv-modules
# modifieable for experiments or debugging m-a
MA_DIR=/usr/share/modass
# load generic variable handling
-include $(MA_DIR)/include/generic.make
# load default rules, including kdist, kdist_image, ...
-include $(MA_DIR)/include/common-rules.make
  
# module assistant calculates all needed things for us and sets
# following variables:
# KSRC (kernel source directory), KVERS (kernel version string), KDREV
# (revision of the Debian kernel-image package), CC (the correct
# compiler), VERSION (the final package version string), PKGNAME (full
# package name with KVERS included), DEB_DESTDIR (path to store DEBs)
  
# The kdist_config target is called by make-kpkg modules_config and
# by kdist* rules by dependency. It should configure the module so it is
# ready for compilation (mostly useful for calling configure).
# prep-deb-files from module-assistant creates the neccessary debian/ files 
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)


# 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 batman-adv.$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 $@
	


binary-modules: binary-modules-stamp
	dh_testdir

	# Add here command to compile/build the package.
	#$(MAKE)


build:

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. 
	
	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	dh_installdocs

###########################
# setup batman-adv-source #
###########################
	# 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 *.c *.h Makefile* README  \
		debian/$(psource)/usr/src/modules/$(sname)
	cp *.c *.h Makefile* README  \
		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

	sed -e 's/[@]VERSION[@]/$(version)/' debian/dkms.conf.in \
	    > debian/$(dkmssource)/usr/src/$(sname)-$(version)/dkms.conf

	dh_install

# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir -i
	dh_testroot -i
	dh_installchangelogs  -i CHANGELOG
	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:

binary: binary-indep

get-orig-source:
	$(CURDIR)/debian/get-orig-source.sh

.PHONY: build clean binary-indep binary-arch binary install binary-modules kdist kdist_configure kdist_image kdist_clean get-orig-source
