#!/usr/bin/make -f
# -*- makefile -*-

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

DEB_SOURCE := $(shell dpkg-parsechangelog | grep Source: | sed -e 's/Source: //')
DEB_VERSION := $(shell dpkg-parsechangelog | grep Version: | sed -e 's/Version: //')
DEB_UPSTREAM_VERSION := $(shell echo $(DEB_VERSION) | sed -e 's/-[^-]*$$//')
SVN_URL = http://scholarzhang.googlecode.com/svn/trunk/west-chamber

# package version
pver:=$(shell dpkg-parsechangelog | grep Version: | sed -e 's/Version: //' | sed -e 's/-[^-]*$$//')

# The short upstream name, used for the module source directory
dkmssname := west-chamber-$(pver)

# Name of the DKMS package
dkmssource:=west-chamber-dkms

# some default definitions, important!
# Name of the source package
psource:=west-chamber-source

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

# The name of the common userspace package
cname:=west-chamber-common

### 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=west-chamber-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 get-xtables-headers
	./autogen.sh
	dh_auto_configure

# 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
	dh_testdir
	dh_clean
	[ ! -f Makefile ] || $(MAKE) $(MFLAGS) clean
#
### end  KERNEL SETUP

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	./autogen.sh
	dh_auto_configure

	touch configure-stamp


build-arch: configure-stamp  build-arch-stamp
build-arch-stamp: 
	dh_testdir

	# Add here command to compile/build the package.
	$(MAKE) -C extensions user-all-local

	touch $@

#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:
	dh_testroot
	dh_prep
	dh_installdirs lib/modules/$(KVERS)/extra/xtables-addons

	# Build the module
	$(MAKE) -C $(KSRC) M=$(CURDIR)/extensions XA_ABSTOPSRCDIR=$(CURDIR) KERNEL_DIR=$(KSRC) KVERS=$(KVERS) modules

	# Install the module
	cp extensions/xt_CUI.ko extensions/xt_ZHANG.ko extensions/xt_gfw.ko debian/$(PKGNAME)/lib/modules/$(KVERS)/extra/xtables-addons

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

build-indep:  configure-stamp build-indep-stamp
build-indep-stamp: 
	dh_testdir

	# Add here command to compile/build the arch indep package.
	# It's ok not to do anything here, if you don't need to build
	#  anything for this package.
	#docbook-to-man debian/west-chamber.sgml > west-chamber.1

	touch $@

build: build-arch build-indep

clean: 
	dh_testdir
	#dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp

	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) clean
	[ ! -f Makefile ] || $(MAKE) distclean

	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs

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

	# Copy only the driver source to the proper location
	cp extensions/*.c extensions/Makefile.am \
	   extensions/?build \
	   debian/$(psource)/usr/src/modules/$(sname)/extensions
	cp Makefile.am Makefile.extra Makefile.iptrules.in Makefile.mans.in \
	   autogen.sh configure.ac mconfig west-chamber.8.in \
	   debian/$(psource)/usr/src/modules/$(sname)
	# Copy the needed debian/ pieces to the proper location
	cp debian/*modules.in* \
		debian/$(psource)/usr/src/modules/$(sname)/debian
	cp debian/rules debian/changelog debian/copyright debian/clean \
		debian/compat debian/control \
	        debian/$(psource)/usr/src/modules/$(sname)/debian/
	cd debian/$(psource)/usr/src && tar c modules | bzip2 -9 > $(sname).tar.bz2 && rm -rf modules

	# DKMS
	# DKMS: Create the directories to install the source into
	dh_installdirs -p$(dkmssource) usr/src/$(dkmssname)
	# DKMS: Copy only the driver source to the proper location
	cp extensions/*.c debian/$(dkmssource)/usr/src/$(dkmssname)/
	cp extensions/Kbuild extensions/Makefile extensions/Makefile.in debian/$(dkmssource)/usr/src/$(dkmssname)/
	find debian/$(dkmssource)/usr/src -name ".gitignore" -delete
	cp Makefile.extra Makefile.iptrules \
	   mconfig \
	   debian/$(dkmssource)/usr/src/$(dkmssname)
	#sed 's/compat_xtables.o//' extensions/Kbuild > debian/$(dkmssource)/usr/src/$(dkmssname)/Kbuild
	echo 'EXTRA_CFLAGS += -I$${XTABLESDIR} -I.' >> debian/$(dkmssource)/usr/src/$(dkmssname)/Kbuild
	# DKMS: Copy the dkms configuration file
	cp $(CURDIR)/debian/$(dkmssource).conf \
	   debian/$(dkmssource)/usr/src/$(dkmssname)/dkms.conf

	# Add here commands to install the package into debian/west-chamber.
	dh_install

get-xtables-headers:
	tar --extract --to-stdout --file=/usr/src/xtables-addons.tar.bz2 \
		modules/xtables-addons/compat_xtables.c > \
		$(CURDIR)/extensions/compat_xtables.c
	tar --extract --to-stdout --file=/usr/src/xtables-addons.tar.bz2 \
		modules/xtables-addons/compat_xtables.h > \
		$(CURDIR)/extensions/compat_xtables.h
	tar --extract --to-stdout --file=/usr/src/xtables-addons.tar.bz2 \
		modules/xtables-addons/compat_nfinetaddr.h > \
		$(CURDIR)/extensions/compat_nfinetaddr.h
	tar --extract --to-stdout --file=/usr/src/xtables-addons.tar.bz2 \
		modules/xtables-addons/compat_skbuff.h > \
		$(CURDIR)/extensions/compat_skbuff.h
	tar --extract --to-stdout --file=/usr/src/xtables-addons.tar.bz2 \
		modules/xtables-addons/compat_xtnu.h > \
		$(CURDIR)/extensions/compat_xtnu.h

# 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 ChangeLog -i
	dh_installdocs -i
	dh_installexamples -i
	dh_install -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
	dh_installdeb -i
	dh_shlibdeps -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_installdocs -s
	dh_installexamples -s
	dh_installmenu -s
	dh_installcron -s
	dh_installman -s
	dh_installinfo -s
	dh_installchangelogs ChangeLog -s
	dh_strip -s
	dh_link -s
	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

get-orig-source:
	set -e; if echo $(DEB_VERSION) | grep -c "svn"; \
	then \
		svn_version=`echo $(DEB_VERSION) | sed -e 's/^.*svn\([0-9]*\)*\.\(.*\)-.*$$/\2/g'`; \
	else \
		svn_version=$(DEB_UPSTREAM_VERSION); \
	fi; \
	tmpdir=`mktemp -d -t`; \
	cd $$tmpdir; \
	echo "checkout upstream repository ..."; \
	svn checkout -r $$svn_version $(SVN_URL) west-chamber; echo "getting specific upstream revision/tag: $$svn_version"; \
	tar --exclude=.svn -cjvf $(CURDIR)/$(DEB_SOURCE)_$(DEB_UPSTREAM_VERSION).orig.tar.bz2 `ls | head -n 1`; \
	cd $(CURDIR); \
	rm -rf $$tmpdir

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