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


# 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 postgres cpl 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=	cpl-c avpops flatstore pdt lcr msilo bdb dbtext iptrtpproxy ldap oracle osp pa rls
EXTRA_EXCLUDED_MODULES=

### modules depending on mysql
##MYSQL_MODULES=mysql
#jabber related modules
JABBER_MODULES=jabber
###module depending on radiusclient
##RADIUS_MODULES=acc_radius auth_radius group_radius uri_radius avp_radius
#cpl related modules
CPL_MODULES=cpl-c
### pa related modules
##PA_MODULES=pa rls dialog presence_b2b xcap
# postgres modules
POSTGRES_MODULES=postgres
# xmlrpc module
XMLRPC_MODULES=xmlrpc

# the same but with path prepended (needed for modules="...")
##MYSQL_MOD_PATH=$(addprefix modules/, $(MYSQL_MODULES))
JABBER_MOD_PATH=$(addprefix modules/, $(JABBER_MODULES))
##RADIUS_MOD_PATH=$(addprefix modules/, $(RADIUS_MODULES))
CPL_MOD_PATH=$(addprefix modules/, $(CPL_MODULES))
##PA_MOD_PATH=$(addprefix modules/, $(PA_MODULES))
POSTGRES_MOD_PATH=$(addprefix modules/, $(POSTGRES_MODULES))
XMLRPC_MOD_PATH=$(addprefix modules/, $(XMLRPC_MODULES))

SERCTL_PATH=tools/serctl

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.

	touch configure-stamp


build: build-stamp

build-stamp: configure-stamp 
	dh_testdir

	# Add here commands to compile the package.
	$(MAKE) clean_cfg
	$(MAKE) clean_modules_cfg
	$(MAKE) all group_include="standard" skip_modules="$(EXCLUDED_MODULES) $(EXTRA_EXCLUDED_MODULES)" cfg-target=/etc/ser/ prefix=/usr
	$(MAKE) clean_cfg
	$(MAKE) clean_modules_cfg
	$(MAKE) modules group_include="mysql" cfg-target=/etc/ser/ prefix=/usr skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#$(MAKE) modules modules="$(JABBER_MOD_PATH)" cfg-target=/etc/ser/ prefix=/usr skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	$(MAKE) clean_cfg
	$(MAKE) clean_modules_cfg
	$(MAKE) modules group_include="radius" cfg-target=/etc/ser/ prefix=/usr skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#$(MAKE) modules modules="$(CPL_MOD_PATH)" cfg-target=/etc/ser/ prefix=/usr skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#$(MAKE) modules group_include="presence" cfg-target=/etc/ser/ prefix=/usr skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#$(MAKE) modules modules="$(POSTGRES_MOD_PATH)" cfg-target=/etc/ser/ prefix=/usr skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	$(MAKE) clean_cfg
	$(MAKE) clean_modules_cfg
	$(MAKE) modules modules="$(XMLRPC_MOD_PATH)" cfg-target=/etc/ser/ prefix=/usr skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#/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) -C lib -f Makefile.ser proper
	#-$(MAKE) include_modules=" $(JABBER_MODULES) $(CPL_MODULES) \
	#	$(POSTGRES_MODULES) $(XMLRPC_MODULES)" proper
	-$(MAKE) proper
	-$(MAKE) clean_cfg
	-$(MAKE) clean_modules_cfg

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/ser
	$(MAKE) clean_cfg
	$(MAKE) clean_modules_cfg
	$(MAKE) install group_include="standard" skip_modules="$(EXCLUDED_MODULES) $(EXTRA_EXCLUDED_MODULES)" \
		basedir=$(CURDIR)/debian/ser \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/ser \
		cfg-target=/etc/ser/
	# fix etc/ser dir location
	mv -f $(CURDIR)/debian/ser/usr/etc $(CURDIR)/debian/ser
	# install only the mysql module
	$(MAKE) clean_cfg
	$(MAKE) clean_modules_cfg
	$(MAKE) install-modules-all group_include="mysql" \
		basedir=$(CURDIR)/debian/ser-mysql-modules \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/ser-mysql-modules \
		cfg-target=/etc/ser/ \
		doc-dir=share/doc/ser-mysql-modules \
		skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#install only the jabber module
	#$(MAKE) install-modules-all modules="$(JABBER_MOD_PATH)"  \
	#	basedir=$(CURDIR)/debian/ser-jabber-module \
	#	prefix=/usr \
	#	cfg-prefix=$(CURDIR)/debian/ser-jabber-module \
	#	cfg-target=/etc/ser/ \
	#	doc-dir=share/doc/ser-jabber-module \
	#	skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#install only the radius modules
	#$(MAKE) install-modules-all group_include="radius"  \
	#	basedir=$(CURDIR)/debian/ser-radius-modules \
	#	prefix=/usr \
	#	cfg-prefix=$(CURDIR)/debian/ser-radius-modules \
	#	cfg-target=/etc/ser/ \
	#	doc-dir=share/doc/ser-radius-modules \
	#	skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#install only the cpl module
	#$(MAKE) install-modules-all modules="$(CPL_MOD_PATH)"  \
	#	basedir=$(CURDIR)/debian/ser-cpl-module \
	#	prefix=/usr \
	#	cfg-prefix=$(CURDIR)/debian/ser-cpl-module \
	#	cfg-target=/etc/ser/ \
	#	doc-dir=share/doc/ser-cpl-module \
	#	skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#install only the presence modules
	#$(MAKE) install-modules-all group_include="presence"  \
	#	basedir=$(CURDIR)/debian/ser-presence-modules \
	#	prefix=/usr \
	#	cfg-prefix=$(CURDIR)/debian/ser-presence-modules \
	#	cfg-target=/etc/ser/ \
	#	doc-dir=share/doc/ser-presence-modules \
	#	skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#install only the postgres module
	#$(MAKE) install-modules-all modules="$(POSTGRES_MOD_PATH)"  \
	#	basedir=$(CURDIR)/debian/ser-postgres-module \
	#	prefix=/usr \
	#	cfg-prefix=$(CURDIR)/debian/ser-postgres-module \
	#	cfg-target=/etc/ser/ \
	#	doc-dir=share/doc/ser-postgres-module \
	#	skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	#install only the xmlrpc module
	$(MAKE) clean_cfg
	$(MAKE) clean_modules_cfg
	$(MAKE) install-modules-all modules="$(XMLRPC_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/ser-xmlrpc-module \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/ser-xmlrpc-module \
		cfg-target=/etc/ser/ \
		doc-dir=share/doc/ser-xmlrpc-module \
		skip_modules="$(EXTRA_EXCLUDED_MODULES)"
	# 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/usr/lib/ser/
	# and fix path to the *.sql files in ser_mysql.sh script
	sed -i 's#CREATE_SCRIPT=`dirname $$0`"/"$$DEFAULT_CREATE_SCRIPT#CREATE_SCRIPT="/usr/lib/ser/"\$$DEFAULT_CREATE_SCRIPT#' $(CURDIR)/debian/ser/usr/sbin/ser_mysql.sh
	sed -i 's#DATA_SCRIPT=`dirname $$0`"/"$$DEFAULT_DATA_SCRIPT#DATA_SCRIPT="/usr/lib/ser/"$$DEFAULT_DATA_SCRIPT#' $(CURDIR)/debian/ser/usr/sbin/ser_mysql.sh
	sed -i 's#DROP_SCRIPT=`dirname $$0`"/"$$DEFAULT_DROP_SCRIPT#DROP_SCRIPT="/usr/lib/ser/"$$DEFAULT_DROP_SCRIPT#' $(CURDIR)/debian/ser/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/ser-oob.cfg $(CURDIR)/debian/ser-oob/etc/ser/
	# and remove it from main ser package
	rm -f $(CURDIR)/debian/ser/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

