#!/usr/bin/make -f

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

export HOTPLUG_FIRMWARE=1

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif

# For DKMS. Use the full release (e.g.: 2.2.1+dfsg-1ubuntu1)
CVERSION := $(shell dpkg-parsechangelog | grep '^Version:' | cut -d' ' -f2 | cut -d\: -f2)

## MODULE-ASSISTANT STUFF
# prefix of the target package name
PREFIX:=dahdi
SKPG:=$(PREFIX)-source
PACKAGE:=$(PREFIX)-modules
PACKAGE_SRC:=$(PREFIX)-linux
# 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
-include $(MA_DIR)/include/common-rules.make

DEBVERSION:=$(shell head -n 1 debian/changelog \
		    | sed -e 's/^[^(]*(\([^)]*\)).*/\1/')
DEB_BASE_VERSION:=$(shell echo $(DEBVERSION) | sed -e 's/^.*://' -e 's/-[0-9~.a-z]*$$//')
UPVERSION:=$(shell echo $(DEB_BASE_VERSION) | sed -e 's/[~+]dfsg[0-9]*\(~\|$$\)/\1/' -e 's/~\(rc\|beta\)/-\1/')

UPFILENAME := $(PACKAGE_SRC)_$(UPVERSION).orig.tar.gz
FILENAME := $(PACKAGE_SRC)_$(DEB_BASE_VERSION).orig.tar.gz
URL := http://downloads.asterisk.org/pub/telephony/$(PACKAGE_SRC)/releases/$(PACKAGE_SRC)-$(UPVERSION).tar.gz

# If the makefile was properly-writen, there was a good separation 
# between kernel and userspace. As things stand now I'd like to work
# around problems with bilding stuff with HOSTCC by simply providing 
# the generated headers as part of the source:
GENERATED_SOURCES := include/dahdi/version.h
BUILD_DATE=$(shell dpkg-parsechangelog --show-field Date)

%:
	dh $@

kdist_clean: clean

kdist_config: prep-deb-files

binary-modules: prep-deb-files
	dh_testdir
	dh_testroot
	dh_prep
	#cp -a $(CURDIR)/debian/generated/* .
	make modules KERNEL_SOURCES=$(KSRC) MODVERSIONS=detect KERNEL=linux-$(KVERS) 
	make install-modules KERNELRELEASE=$(KVERS) DESTDIR=$(CURDIR)/debian/$(PKGNAME)
	# The modules are way too big. This is only in kernel 2.6
	# (Removed for now: breaks cross-building)
	#find debian/$(PKGNAME)/lib/modules -name '*.ko' |xargs strip -g
	$(RM) -f debian/$(PKGNAME)/lib/modules/$(KVERS)/modules.*
	dh_installmodules
	dh_installdebconf
	dh_installdocs
	dh_installchangelogs
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol -- -v$(VERSION)
	dh_md5sums
	dh_builddeb --destdir=$(DEB_DESTDIR)

## END OF M-A SECTION

# FIXME: this should be done by Files-Exclude in debian/copyright:
override_dh_auto_build:
ifneq ($(wildcard drivers/dahdi/xpp/firmwares/USB_FW.hex),)
	@echo "Tarball is not DFSG-compliant. Use './debian/rules get-orig-source'"
	exit 1
endif
	# For DKMS
	sed -e "s|#CVERSION#|$(CVERSION)|"				 \
		debian/dkms.conf.in > debian/dkms.conf
	sed -e "s|#CVERSION#|$(CVERSION)|"				 \
		debian/dahdi-dkms.install.in > debian/dahdi-dkms.install

	$(MAKE) docs
	$(MAKE) $(GENERATED_SOURCES)

override_dh_auto_clean:
	# Delete the generated dkms files
	rm -f debian/dahdi-dkms.install
	rm -f debian/dkms.conf
	rm -f $(GENERATED_SOURCES)
	rm -f dahdi/include/version.h
	[ ! -f Makefile ] || $(MAKE) dist-clean || true

TARPARDIR=$(CURDIR)/debian/tmp
TARDIR=$(TARPARDIR)/modules/$(PREFIX)

override_dh_auto_install:
	$(MAKE) install-include DESTDIR=$(CURDIR)/debian/$(SKPG)

override_dh_install:
	dh_install
	# driver source code
	mkdir -p $(TARDIR)/debian/generated
	cp Makefile $(TARDIR)/
	cp -p .version $(TARDIR)/
	for dir in build_tools firmware include drivers; do \
	  if [ -d $$dir ]; then cp -r $$dir $(TARDIR); fi; \
	done
	
	# Packaging infrastructure
	cp -r debian/rules debian/changelog debian/copyright\
	  debian/control debian/compat \
	  debian/control.modules.in \
	  $(TARDIR)/debian/
	
	tar cjf debian/$(SKPG)/usr/src/$(PREFIX).tar.bz2 \
	  -C $(TARPARDIR) modules --mtime="$(BUILD_DATE)"

print-version:
	@@echo "Debian version:          $(DEBVERSION)"
	@@echo "Upstream version:        $(UPVERSION)"

TARBALL_DIR=../tarballs/$(PACKAGE_SRC)-$(UPVERSION).tmp
get-orig-source:
	@@dh_testdir
	@@[ -d ../tarballs/. ]||mkdir -p ../tarballs
	@@echo Downloading $(UPFILENAME) from $(URL) ...
	@@wget  -nv -T10 -t3 --verbose -O ../tarballs/$(UPFILENAME) $(URL)
	@@echo Repacking as DFSG-free...
	@@mkdir -p $(TARBALL_DIR)/
	@@cd $(TARBALL_DIR) ; \
	tar xfz ../$(UPFILENAME)
	@@rm -rf $(TARBALL_DIR)/$(PACKAGE_SRC)-$(UPVERSION)/drivers/dahdi/xpp/firmwares/*.hex
	@@rm -f $(TARBALL_DIR)/$(PACKAGE_SRC)-$(UPVERSION)/drivers/dahdi/*.rbt
	@@cd $(TARBALL_DIR) ; \
	tar cfz ../$(FILENAME) *
	@@echo Cleaning up...
	@@$(RM) -rf $(TARBALL_DIR)/
	@@$(RM) -f ../tarballs/$(UPFILENAME)

