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

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

CFLAGS = -Wall -g
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -O0
else
	CFLAGS += -O2
endif


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

# some default definitions, important!
# 
# Name of the source package
psource:=$(sname)-source

PKGMODULE:=martian-modem-source
PKG:=martian-modem

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

# 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:
#	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp
	
	# Cleaning kernel module building dir
	$(MAKE) -C source clean

configure: configure-stamp
configure-stamp:
	dh_testdir
	touch configure-stamp

build-arch: configure-stamp  build-arch-stamp
build-arch-stamp:
	dh_testdir
	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
	dh_installdirs lib/modules/$(KVERS)/extra
	# Build the module
	$(MAKE) -C source KERNEL_DIR=$(KSRC) KVERS=$(KVERS)
	
	# Install the module
	dh_install source/martian_dev.$ko lib/modules/$(KVERS)/extra
	
	# Building package stuff
	dh_installdocs debian/copyright README Concept
	dh_installchangelogs ChangeLog
	dh_compress 
	dh_fixperms 
	dh_installdeb 
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums 
	dh_builddeb --destdir=$(CURDIR)

build-indep: configure-stamp build-indep-stamp 
build-indep-stamp:
	dh_testdir
	touch $@

build: build-arch build-indep
	
	# Userspace tool building
	#
	$(MAKE) -C modem


clean: 
	#dh_testdir
	dh_testroot
	rm -f build-arch-stamp build-indep-stamp configure-stamp
			
	# Cleaning userspace util
	$(MAKE) -C modem clean
	
	dh_clean

#install: DH_OPTIONS=
install: build
	dh_testdir
	dh_testroot
	
	# Creating kernel module source container
	#
	dh_installdirs -p$(PKGMODULE) -i usr/src/modules/$(sname)/debian  usr/src/modules/$(sname)/source
	dh_link -s -p$(PKGMODULE) ../packages/default.sh usr/share/modass/overrides/$(PKGMODULE)
	
	# Copy only the driver source to the proper location
	dh_install -p$(PKGMODULE) *.[ch] usr/src/modules/$(sname)/source
	
	# Copy the needed debian/ pieces to the proper location
	dh_install -p$(PKGMODULE) ChangeLog README Concept usr/src/modules/$(sname)
	dh_install -p$(PKGMODULE) debian/*modules.in* usr/src/modules/$(sname)/debian
	dh_install -p$(PKGMODULE) debian/*_KVERS_* debian/rules debian/changelog debian/copyright \
                debian/compat usr/src/modules/$(sname)/debian/
	
	# Changing original martian.h path from upstream
	dh_install -p$(PKGMODULE) kmodule/* usr/src/modules/$(sname)/source
	
	# Due the lack of privative software and terms under "Redistribution and Usage" specified in Copyright section 2.3.2
	#    no modifications can be made over any files for distribution (read Copyright)
	dh_install -p$(PKGMODULE) martian.h usr/src/modules/$(sname)	
	
	# Have to set permissions now before tar'ing
	chown -R root.src debian/$(psource)/usr/src/modules
	cd debian/$(PKGMODULE)/usr/src && tar c modules | bzip2 -9 > $(sname).tar.bz2 && rm -rf modules; cd -
	
	# Creating userspace utils stuff
	#
	dh_installdirs -p$(PKG) /usr/sbin
	
	$(MAKE) -C modem all
	 
	dh_install -p$(PKG) modem/martian_modem usr/sbin



# Build architecture-independent files here.
# Pass -i to all debhelper commands in this target to reduce clutter.
binary-indep: build install
	dh_testdir 
	dh_testroot 
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples scripts/wv.conf
	dh_installinit -p$(PKG)
	dh_installman -p$(PKG) debian/martian_modem.8
	dh_install 
	dh_lintian -p$(PKG)
	dh_link 
	dh_compress 
	dh_fixperms 
	dh_makeshlibs
	dh_installdeb 
	dh_shlibdeps 
	dh_gencontrol 
	dh_md5sums 
	dh_builddeb 

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