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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)
ifneq ($(DEB_HOST_GNU_TYPE),$(DEB_BUILD_GNU_TYPE))
CROSS= --build $(DEB_BUILD_GNU_TYPE) --host $(DEB_HOST_GNU_TYPE)
else
CROSS= --build $(DEB_BUILD_GNU_TYPE)
endif

# some default definitions, important!
#
# Package version
pver := 1.37

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

# Name of the DKMS package
dkmssource:=xtables-addons-dkms

# Name of the source package
psource:=xtables-addons-source

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

# The name of the common userspace package
cname:=xtables-addons-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=xtables-addons-modules
# modifieable for experiments or debugging m-a
MA_DIR ?= /usr/share/modass

# If we do not enforce $(CC) value here, then the following include
# will set it to gcc-4.1
CC = gcc

# 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:
	dh_testdir
	$(MAKE) -C $(KSRC) M=$(CURDIR) XA_ABSTOPSRCDIR=$(CURDIR) clean
	dh_clean
### end  KERNEL SETUP

configure-stamp: configure
	dh_testdir
	# Add here commands to configure the package.
	cp -f /usr/share/misc/config.sub config.sub
	cp -f /usr/share/misc/config.guess config.guess
	# do not build TEE (see #592237)
	sed -i 's/build_TEE=m/build_TEE=n/' mconfig
	# Fix the kbuild detection, to avoid failing while we
	# do *not* want kbuild
	sed -i '/^k.*+0))/d' ./configure
	./configure $(CROSS) --prefix=/usr \
		--mandir=\$${prefix}/share/man \
		--infodir=\$${prefix}/share/info \
		--libexecdir=/lib \
		--without-kbuild
##CFLAGS="$(CFLAGS)" LDFLAGS="-Wl,-z,defs"
	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
	$(MAKE) -f Makefile.mans all

	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_clean -k

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

	# Install the module
	$(MAKE) -C $(KSRC) M=$(CURDIR) XA_ABSTOPSRCDIR=$(CURDIR) modules_install \
		INSTALL_MOD_PATH=$(CURDIR)/debian/$(PKGNAME) \
		INSTALL_MOD_DIR=extra/$(sname)

	# remove modules.* files if existing. See http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=552301
	find $(CURDIR)/debian/$(PKGNAME)/lib/modules/* -maxdepth 1 -name 'modules\.*' -delete

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

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

	touch $@

build: build-arch build-indep

clean:
	dh_testdir
	#dh_testroot
	# Add here commands to clean up after the build process.
	[ ! -f Makefile ] || $(MAKE) distclean ||:
	rm -f extensions/ipset/.*.d
	rm -f xtables-addons.8 extensions/matches.man extensions/targets.man
	rm -f config.sub config.guess
	rm -f config.log config.status
	rm -f build-arch-stamp build-indep-stamp configure-stamp
	rm -f extensions/ACCOUNT/Makefile extensions/ipset-4/Makefile extensions/ipset-5/Makefile extensions/pknock/Makefile
	rm -f matches.man targets.man .manpages.lst
	sed -i 's/build_TEE=.*/build_TEE=/' mconfig
	find . -name ".*.oo.d" -delete
	dh_clean

install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Install the libraries
	$(MAKE) -C extensions user-install-local DESTDIR=$(CURDIR)/debian/$(cname)
	$(MAKE) install-man DESTDIR=$(CURDIR)/debian/$(cname)
	#$(MAKE) install DESTDIR=$(CURDIR)/debian/$(cname)

	$(MAKE) -C extensions user-clean-local

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

	# Copy only the driver source to the proper location
	cp -r extensions/* debian/$(psource)/usr/src/modules/$(sname)/
	cp mconfig Makefile.extra Makefile.iptrules debian/$(psource)/usr/src/modules/$(sname)/

	# create a correct kbuild file
	cat mconfig extensions/Kbuild >debian/$(psource)/usr/src/modules/$(sname)/Kbuild
	sed -i -e "s/^include/-include/" debian/$(psource)/usr/src/modules/$(sname)/Kbuild

	# Copy the needed debian/ pieces to the proper location
	cp debian/*modules.in* \
		debian/$(psource)/usr/src/modules/$(sname)/debian
	#cp debian/*_KVERS_* debian/rules debian/changelog debian/copyright \
	#	debian/compat debian/$(psource)/usr/src/modules/$(sname)/debian/
	cp debian/rules debian/changelog debian/copyright \
		debian/compat debian/control \
		debian/$(psource)/usr/src/modules/$(sname)/debian/

	rm $(CURDIR)/debian/xtables-addons-common/usr/lib/*.la
	rm $(CURDIR)/debian/xtables-addons-common/usr/lib/*.so

	cd debian/$(psource)/usr/src && tar c modules | bzip2 -9 > $(sname).tar.bz2 && rm -rf modules

	## Now install sources for DKMS
	# Create the directories to install the source into
	dh_installdirs -p$(dkmssource)  usr/src/$(dkmssname)

	# Copy only the driver source to the proper location
	cp -r extensions/* debian/$(dkmssource)/usr/src/$(dkmssname)/
	find debian/$(dkmssource)/usr/src -name ".gitignore" -delete
	cp mconfig Makefile.extra Makefile.iptrules \
         debian/$(dkmssource)/usr/src/$(dkmssname)/

	# Copy the dkms configuration file
	cp $(CURDIR)/debian/$(dkmssource).conf \
	 debian/$(dkmssource)/usr/src/$(dkmssname)/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
	dh_installdocs -i
	dh_installexamples -i
#	dh_install -i
#	dh_installmenu -i
#	dh_installdebconf -i
#	dh_installlogrotate -i
#	dh_installemacsen -i
#	dh_installpam -i
#	dh_installmime -i
#	dh_installinit -i
#	dh_installcron -i
#	dh_installinfo -i
	dh_installman -i
	dh_link -i
	dh_compress -i
	dh_fixperms -i
	dh_installdeb -i
#	dh_perl -i
#	dh_python -i
#	dh_makeshlibs -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 -a
	dh_testroot -a
	dh_installchangelogs -a
	dh_installdocs -a
	dh_installexamples -a
	dh_installman -a
	dh_link -a
	dh_compress -a
	dh_fixperms -a
	dh_makeshlibs -a
	dh_installdeb -a
	dh_shlibdeps -a
	dh_strip -a
	dh_gencontrol -a
	dh_md5sums -a
	dh_builddeb -a

# This is an internal target (not meant to be used by anyone but package maintainers)
# This command build the package to get the list of generated modules, and creates an
# updated version of the dkms config file with all modules names
# This requires the current kernel headers to be present
update-dkms-conf:
	./configure ; \
	make ; \
	sed '/^BUILT_MODULE/,$$d' debian/xtables-addons-dkms.conf > debian/xtables-addons-dkms.conf.NEW ; \
	echo "# Do NOT edit manually" >> debian/xtables-addons-dkms.conf.NEW ;\
	echo "# this was generated by the target update-dkms-conf of debian/rules" >> debian/xtables-addons-dkms.conf.NEW ;\
	value=0; \
	for module_name in `find . -name "*.ko" -print | sed 's|.*/\([^/]*\)\.ko|\1|'`; \
	do \
		echo "BUILT_MODULE_LOCATION[$$value]=\"\""; \
		echo "BUILT_MODULE_NAME[$$value]=\"$$module_name\""; \
		echo "DEST_MODULE_LOCATION[$$value]=\"/updates\""; \
		echo ""; \
		value=`expr $$value + 1`; \
	done >> debian/xtables-addons-dkms.conf.NEW ;\
	echo "AUTOINSTALL=yes" >> debian/xtables-addons-dkms.conf.NEW ;\
	echo "now look at file debian/xtables-addons-dkms.conf.NEW"

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install  binary-modules kdist kdist_configure kdist_image kdist_clean update-dkms-conf
