#!/usr/bin/make -f
# Sample debian/rules that uses debhelper.
# GNU copyright 1997 to 1999 by Joey Hess.
#
# $Id: rules,v 1.5.2.4 2006/02/02 16:01:58 dan_pascu Exp $
#
# History:
# --------


# 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" package or unstable 
EXCLUDED_MODULES=	mysql jabber postgres cpl-c pa \
					auth_radius group_radius uri_radius

# modules depending on mysql
MYSQL_MODULES=mysql
# modules depending on postgres
POSTGRES_MODULES=postgres
#jabber related modules
JABBER_MODULES=jabber
#cpl related modules
CPL_MODULES=cpl-c
#module depending on radiusclient
RADIUS_MODULES=auth_radius group_radius uri_radius

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





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) all  skip_modules="$(EXCLUDED_MODULES)" cfg-target=/etc/openser/
	$(MAKE) modules modules="$(MYSQL_MOD_PATH)" cfg-target=/etc/openser/
	$(MAKE) modules modules="$(POSTGRES_MOD_PATH)" cfg-target=/etc/openser/
	$(MAKE) modules modules="$(JABBER_MOD_PATH)" cfg-target=/etc/openser/
	$(MAKE) modules modules="$(CPL_MOD_PATH)" cfg-target=/etc/openser/
	$(MAKE) modules modules="$(RADIUS_MOD_PATH)" cfg-target=/etc/openser/
	#/usr/bin/docbook-to-man debian/openser.sgml > openser.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) \
		include_modules="$(MYSQL_MODULES) $(POSTGRES_MODULES) $(JABBER_MODULES) $(CPL_MODULES) $(RADIUS_MODULES)"\
		 proper

	dh_clean

install: build
	dh_testdir
	dh_testroot
	dh_clean -k
	dh_installdirs

	# Add here commands to install the package into debian/openser
	$(MAKE) install  skip_modules="$(EXCLUDED_MODULES)" \
		basedir=$(CURDIR)/debian/openser \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser \
		cfg-target=/etc/openser/
	# install only the mysql module
	$(MAKE) install-modules-all modules="$(MYSQL_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-mysql-module \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-mysql-module \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-mysql-module
	# install only the postgres module
	$(MAKE) install-modules-all modules="$(POSTGRES_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-postgres-module \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-postgres-module \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-postgres-module
	#install only the jabber module
	$(MAKE) install-modules-all modules="$(JABBER_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-jabber-module \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-jabber-module \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-jabber-module
	#install only the cpl module
	$(MAKE) install-modules-all modules="$(CPL_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-cpl-module \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-cpl-module \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-cpl-module
	#install only the radius modules
	$(MAKE) install-modules-all modules="$(RADIUS_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-radius-modules \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-radius-modules \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-radius-modules
	# compile and install all TLS related packages 
	$(MAKE) exclude_modules="" proper
	# openser base package
	TLS=1 $(MAKE) install skip_modules="$(EXCLUDED_MODULES)" \
		basedir=$(CURDIR)/debian/openser-tls \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-tls \
		cfg-target=/etc/openser/
	# install only the mysql-tls module
	TLS=1 $(MAKE) install-modules-all modules="$(MYSQL_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-tls-mysql-module \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-tls-mysql-module \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-tls-mysql-module
	# install only the postgres-tls module
	TLS=1 $(MAKE) install-modules-all modules="$(POSTGRES_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-tls-postgres-module \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-tls-postgres-module \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-tls-postgres-module
	#install only the jabber-tls module
	TLS=1 $(MAKE) install-modules-all modules="$(JABBER_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-tls-jabber-module \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-tls-jabber-module \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-tls-jabber-module
	#install only the cpl-tls module
	TLS=1 $(MAKE) install-modules-all modules="$(CPL_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-tls-cpl-module \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-tls-cpl-module \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-tls-cpl-module
	#install only the radius-tls modules
	TLS=1 $(MAKE) install-modules-all modules="$(RADIUS_MOD_PATH)"  \
		basedir=$(CURDIR)/debian/openser-tls-radius-modules \
		prefix=/usr \
		cfg-prefix=$(CURDIR)/debian/openser-tls-radius-modules \
		cfg-target=/etc/openser/ \
		doc-dir=share/doc/openser-tls-radius-modules
	#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_installlogrotate
	dh_installinit -popenser -popenser-tls --name=openser -- defaults 23
	dh_installcron
	dh_installman
	dh_installinfo
	dh_installchangelogs
	dh_link
	dh_strip
	dh_compress 
	dh_fixperms
	dh_installdeb
	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
