#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# $Id$
#
# History:
# --------
#  2003-05-30  updated for the 0.8.11 release, changed modules list and
#              some ser make options (andrei)
#  2003-06-27  changed to DH_COMPAT 4 (debian/compat) + other filenames
#              & small fixes (andrei)
#  2003-08-29  replaced modules-install with modules-install-all, to 
#               install also the modules doc (andrei)
#  2004-07-23  added ext and commented out the radius modules
#              (the current radius modules depend on radiusclient-ng which
#               is not yet part of debian)  (andrei)
#  2005-06-23  added cpl-c, pa & re-enabled radius (with libradiusclient-ng)
#                (andrei)
#  2007-09-11  updated for ser 2.0.0 and Debian Etch
#              presence modules commented out (can't be compiled due to some bug in Makefile)
#              added /etc/defaults/ser file installation
#              added ser-oob package
#                (pavel)
#  2007-10-31  updated for cvs head (pavel)
#  2009-07-08  updated for sip-router (andrei)


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

# This is the debhelper compatibility version to use.
# export DH_COMPAT=4
#  -- already set in compat
#  force no striping (always include debug symbols for now)
export DEB_BUILD_OPTIONS:="$(DEB_BUILD_OPTIONS) nostrip"

# modules not in the "main" ser package
EXCLUDED_MODULES=jabber xmlrpc cpl-c

# extra modules to skip, because they are not compilable now
# - regardless if they go to the main ser package or to some module package,
# they will be excluded from compile and install of all
EXTRA_EXCLUDED_MODULES=seas bdb dbtext oracle pa rls iptrtpproxy
#EXTRA_EXCLUDED_MODULES=

# possible module directories that can appear in MODULES_SP
# (only used for deducing a module name)
MDIRS=modules modules_s modules_k

# modules packaged in separate packages (complete with full modules_* path)
# with the package name: ser-$(module_name)-module
MODULES_SP=modules/db_mysql modules/db_postgres modules/tls modules/xmlrpc

# module groups that are packaged in seperate packages
# (with the name ser-$(group_name)-modules)
# Note: the order is important (should be in dependency order, the one
# on which other depend first)
PACKAGE_GROUPS=db radius

# directories with possible duplicate libraries (that should be deleted
# from current module* packages)
DUP_LIBS_DIRS=$(CURDIR)/debian/ser/usr/lib/ser \
			$(CURDIR)/debian/ser-db-modules/usr/lib/ser

# modules names out of modules sp
MODULES_SP_NAMES=$(filter-out $(MDIRS),$(subst /, ,$(MODULES_SP)))

# "function" to get package short name out of a dir/module_name
# it also transforms db_foo into foo
mod_name=$(subst db_,,$(lastword $(subst /, ,$(1))))

define PACKAGE_GRP_BUILD_template
	# package all the modules in PACKAGE_GROUPS in separate packages
	$(foreach grp,$(PACKAGE_GROUPS),\
		$(MAKE) every-module group_include="$(grp)"
	)
endef


define PACKAGE_MODULE_BUILD_template
	# package all the modules MODULES_SP in separate packages
	$(foreach mod,$(MODULES_SP),\
		$(MAKE) modules modules="$(mod)"
	)
endef


define PACKAGE_GRP_INSTALL_template
	$(foreach grp,$(PACKAGE_GROUPS),\
		$(MAKE) install-modules-all group_include="$(grp)" \
			basedir=$(CURDIR)/debian/ser-$(grp)-modules \
			cfg_prefix=$(CURDIR)/debian/ser-$(grp)-modules \
			doc-dir=share/doc/ser-$(grp)-modules
		# eliminate duplicate libs
		-for d in $(DUP_LIBS_DIRS); do \
			test "$$d" != "$(CURDIR)/debian/ser-$(grp)-modules/usr/lib/ser" &&\
			for r in $$d/lib*; do \
				echo "removing $(grp) lib `basename $$r` present also in $$d";\
				rm -f $(CURDIR)/debian/ser-$(grp)-modules/usr/lib/ser/`basename "$$r"` ; \
			done ; \
		done
	)
endef


define PACKAGE_MODULE_INSTALL_template
	$(foreach mod,$(MODULES_SP),
		$(MAKE) install-modules-all modules="$(mod)" \
				modules_s="" modules_k="" \
				basedir=$(CURDIR)/debian/ser-$(call mod_name,$(mod))-module \
				doc-dir=share/doc/ser-$(call mod_name,$(mod))-module
		# eliminate duplicate libs
		-for d in $(DUP_LIBS_DIRS); do \
			test "$$d" != "$(CURDIR)/debian/ser-$(call mod_name,$(mod))-module/usr/lib/ser" &&\
			for r in $$d/lib*; do \
				echo "removing $(call mod_name, $(mod)) lib `basename $$r` present also in $$d";\
				rm -f $(CURDIR)/debian/ser-$(call mod_name,$(mod))-module/usr/lib/ser/`basename "$$r"` ; \
			done ; \
		done
	)
endef

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -g
endif
ifeq (,$(findstring nostrip,$(DEB_BUILD_OPTIONS)))
	INSTALL_PROGRAM += -s
endif

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	$(MAKE) cfg prefix=/usr cfg_prefix=$(CURDIR)/debian/ser \
			cfg_target=/etc/ser/ \
			basedir=$(CURDIR)/debian/ser \
			skip_modules="$(EXCLUDED_MODULES) $(EXTRA_EXCLUDED_MODULES)" \
			group_include="standard"

	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp 
	dh_testdir
	# Add here commands to compile the package.
	$(MAKE) all
	# make groups
	$(call PACKAGE_GRP_BUILD_template)
	# make single-module packages
	$(call PACKAGE_MODULE_BUILD_template)
	#/usr/bin/docbook-to-man debian/ser.sgml > ser.1
	touch build-stamp

clean:
	dh_testdir
	dh_testroot
	rm -f build-stamp configure-stamp
	# Add here commands to clean up after the build process.
	-$(MAKE) maintainer-clean
	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs
	# Add here commands to install the package into debian/ser
	$(MAKE) install group_include="standard"
	# fix etc/ser dir location -- not needed -- andrei
	# mv -f $(CURDIR)/debian/ser/usr/etc $(CURDIR)/debian/ser
	# make group packages
	$(call PACKAGE_GRP_INSTALL_template)
	# make single module packages
	$(call PACKAGE_MODULE_INSTALL_template)
	# install /etc/default/ser file
	mkdir -p $(CURDIR)/debian/ser/etc/default
	cp -f debian/ser.default $(CURDIR)/debian/ser/etc/default/ser
	# install the *.sql files for ser_mysql.sh script
	cp -f scripts/mysql/*.sql $(CURDIR)/debian/ser-mysql-module/usr/lib/ser/
	# and fix path to the *.sql files in ser_mysql.sh script
	sed -i 's#DEFAULT_SCRIPT_DIR=.*#DEFAULT_SCRIPT_DIR="/usr/lib/ser/"#' \
		$(CURDIR)/debian/ser-mysql-module/usr/sbin/ser_mysql.sh
	# install advanced ser config file for ser-oob package
	mkdir -p $(CURDIR)/debian/ser-oob/etc/ser
	cp -f etc/sip-router-oob.cfg $(CURDIR)/debian/ser-oob/etc/ser/ser-oob.cfg
	#dh_movefiles



# This single target is used to build all the packages, all at once, or
# one at a time. So keep in mind: any options passed to commands here will
# affect _all_ packages. Anything you want to only affect one package
# should be put in another target, such as the install target.
binary-common: 
	dh_testdir
	dh_testroot
	dh_installdebconf	
	dh_installdocs
	dh_installexamples
	dh_installmenu
#	dh_installlogrotate
#	dh_installemacsen
#	dh_installpam
#	dh_installmime
	dh_installinit  -- defaults 23
	dh_installcron
	dh_installman
	dh_installinfo
#	dh_undocumented
	dh_installchangelogs 
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
#	dh_perl
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture-independent packages using the common target
binary-indep: build install
# (Uncomment this next line if you have such packages.)
#        $(MAKE) -f debian/rules DH_OPTIONS=-i binary-common
# We have nothing to do by default.


# Build architecture-dependent packages using the common target
binary-arch: build install
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

# Any other binary targets build just one binary package at a time.
binary-%: build install
	$(MAKE) -f debian/rules binary-common DH_OPTIONS=-p$*

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install configure

