#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.

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

# This is the debhelper compatibility version to use.
USE_BRISTUFF=0
export HOTPLUG_FIRMWARE=1

-include /usr/share/quilt/quilt.make

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

## 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\(~\|$$\)/\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

EXTRA_MODS=opvxa1200

ifneq (,$(filter-out powerpc m68k armeb mips,$(shell dpkg-architecture -qDEB_HOST_ARCH)))
EXTRA_MODS += wcopenpci
endif

EXTRA_SUBDIRS += zaphfc ../staging/echo
EXTRA_MODS += dahdi_echocan_oslec

MOD_ARGS=MODULES_EXTRA="$(EXTRA_MODS)" SUBDIRS_EXTRA="$(EXTRA_SUBDIRS)"

kdist_clean: clean-unpatched

kdist_config: prep-deb-files

binary-modules: prep-deb-files
	dh_testdir
	dh_testroot
	dh_prep
	#cp -a $(CURDIR)/debian/generated/* .
	make $(MOD_ARGS) modules KERNEL_SOURCES=$(KSRC) MODVERSIONS=detect KERNEL=linux-$(KVERS) 
	make $(MOD_ARGS) install-modules KERNELRELEASE=$(KVERS) DESTDIR=$(CURDIR)/debian/$(PKGNAME)
ifeq (2.6,$(shell echo $(KVERS) | cut -d. -f1-2))
	# The 2.6 modules are way too big. This is only in kernel 2.6
	find debian/$(PKGNAME)/lib/modules -name '*.ko' |xargs strip -g
	$(RM) -f debian/$(PKGNAME)/lib/modules/$(KVERS)/modules.*
endif
	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

build: patch build-stamp
build-stamp: 
	dh_testdir
	
	$(MAKE) docs
	for mod in $(BRISTUFF_MODULES); do for i in c h; do \
		ln -s $$mod/$$mod.$$i drivers/dahdi/$$mod.$$i; \
	done; done
	$(MAKE) $(GENERATED_SOURCES)
	touch $@

clean: clean-unpatched unpatch
clean-unpatched:
	dh_testdir
	dh_testroot
	rm -f *-stamp
	
	# Delete the generated bristuff symlinks:
	$(RM) -f $(BRISTUFF_MODULES:%=%.[ch])
	# Add here commands to clean up after the build process.
	rm -f $(GENERATED_SOURCES)
	# * Makefile does not exist when running svn-buildpackage
	#   as the source tree is not there.
	# FIXME: This will fail with an ugly warning on the clean of the
	# modules build. However only fter the actuual clean.
	rm -f dahdi/include/version.h
	[ ! -f Makefile ] || $(MAKE) dist-clean || true
	dh_clean

TARPARDIR=$(CURDIR)/debian/tmp
TARDIR=$(TARPARDIR)/modules/$(PREFIX)
install: build-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	
	$(MAKE) install-include DESTDIR=$(CURDIR)/debian/$(SKPG)
	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
	dh_installudev --name=dahdi
	
# Build architecture-independent files here.
binary: install
	dh_testdir
	dh_testroot
	
	dh_installdocs
	dh_installchangelogs -i ChangeLog
	dh_installexamples -i
	dh_perl
	dh_link
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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

# For the record: updating openvox drivers:
# wget 'http://www.openvox.cn/viewvc/dahdi/trunk/opvxa1200.c?view=co' \
#  | sed -e '\r$//' >drivers/dahdi/opvxa1200.c
#
# Updating the zaphfc drivers:
#   rm -rf old new
#   mkdir -p old new/drivers/dahdi
#   svn export http://zaphfc.googlecode.com/svn/branches/2.2/zaphfc/ new/drivers/dahdi/zaphfc
#   diff -Npur  --no-timestamps old new >debian/patches/zaphfc
#   rm -rf old new

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
	@@cd $(TARBALL_DIR) ; \
	tar cfz ../$(FILENAME) *
	@@echo Cleaning up...
	@@$(RM) -rf $(TARBALL_DIR)/
	@@$(RM) -f ../tarballs/$(UPFILENAME)

.PHONY: build clean binary install configure patch unpatch
