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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)
DEB_HOST_ARCH ?= $(shell dpkg-architecture -qDEB_HOST_ARCH)

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

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

export RADCLI=1

# modules not in the "main" kamailio package
EXCLUDED_MODULES=

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

# module groups that are packaged in seperate packages
# (with the name kamailio-$(group_name)-modules)
# Note: the order is important (should be in dependency order, the one
# on which other depend first)
PACKAGE_GROUPS=mysql postgres berkeley unixodbc radius presence \
			   ldap xml perl utils lua memcached \
			   snmpstats carrierroute xmpp cpl redis python geoip geoip2 \
			   sqlite ruby ims sctp \
			   tls outbound websocket autheph kazoo cnxcc \
			   erlang systemd phonenum mongodb rabbitmq python3

# module groups to be packaged onto kamailio-extra-modules
EXTRA_GROUPS=gzcompress uuid ev jansson

# mono not on all arches
ifneq ("$(wildcard /usr/share/mono/mono-archs.make)","")
override PACKAGE_GROUPS+= mono
else
override EXCLUDED_MODULES += mono
endif

# FTBFS on kfreebsd
ifeq ($(DEB_HOST_ARCH_OS),linux)
override PACKAGE_GROUPS+= json kazoo
else
override EXCLUDED_MODULES += json kazoo
endif

# FTBFS on powerpcspe because of AltiVec assumption #729635
ifeq ($(DEB_HOST_ARCH),powerpcspe)
	export NOALTIVEC=1
endif

# name of libdir in the path for libraries (e.g., lib for 32b, lib64 for 64b)
export LIBDIR ?= lib/$(DEB_HOST_MULTIARCH)

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

# "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="k$(grp)" quiet=verbose
	)
endef

define PACKAGE_GRP_INSTALL_template
	$(foreach grp,$(PACKAGE_GROUPS),\
		$(MAKE) install-modules-all group_include="k$(grp)" \
		BASEDIR=$(CURDIR)/debian/kamailio-$(grp)-modules \
		cfg_prefix=$(CURDIR)/debian/kamailio-$(grp)-modules \
		doc-dir=share/doc/kamailio-$(grp)-modules quiet=verbose
		# eliminate duplicate libs
		-for d in $(DUP_LIBS_DIRS); do \
			test "$$d" != "$(CURDIR)/debian/kamailio-$(grp)-modules/usr/$(LIBDIR)/kamailio" &&\
			for r in `find $$d -name 'lib*'|xargs`; do \
				echo "removing $(grp) lib `basename $$r` present also in $$d";\
				rm -f $(CURDIR)/debian/kamailio-$(grp)-modules/usr/$(LIBDIR)/kamailio/`basename "$$r"` ; \
			done ; \
		done
		find $(CURDIR)/debian/kamailio-$(grp)-modules -depth -empty -type d -exec rmdir {} \;
	)
endef

define PACKAGE_EXTRA_BUILD_template
	# package all the modules in EXTRA_GROUPS in separate packages
	$(foreach grp,$(EXTRA_GROUPS),\
		$(MAKE) every-module group_include="k$(grp)" quiet=verbose
	)
endef

define PACKAGE_EXTRA_INSTALL_template
	$(foreach grp,$(EXTRA_GROUPS),\
		$(MAKE) install-modules-all group_include="k$(grp)" \
		BASEDIR=$(CURDIR)/debian/kamailio-extra-modules \
		cfg_prefix=$(CURDIR)/debian/kamailio-extra-modules \
		doc-dir=share/doc/kamailio-extra-modules quiet=verbose
	)
	# eliminate duplicate libs
	-for d in $(DUP_LIBS_DIRS); do \
		test "$$d" != "$(CURDIR)/debian/kamailio-extra-modules/usr/$(LIBDIR)/kamailio" &&\
		for r in `find $$d -name 'lib*'|xargs`; do \
			echo "removing extra lib `basename $$r` present also in $$d";\
			rm -f $(CURDIR)/debian/kamailio-extra-modules/usr/$(LIBDIR)/kamailio/`basename "$$r"` ; \
		done ; \
	done
	find $(CURDIR)/debian/kamailio-extra-modules -depth -empty -type d -exec rmdir {} \;
endef

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

# CFLAGS + CPPFLAGS -> CC_EXTRA_OPTS
CC_EXTRA_OPTS := $(shell dpkg-buildflags --get CPPFLAGS)
CC_EXTRA_OPTS += $(shell DEB_CFLAGS_MAINT_STRIP=-O2 dpkg-buildflags --get CFLAGS)
CXXFLAGS := $(shell DEB_CXXFLAGS_MAINT_STRIP=-O2 dpkg-buildflags --get CXXFLAGS)
# LDFLAGS -> LD_EXTRA_OPTS
LD_EXTRA_OPTS := $(shell dpkg-buildflags --get LDFLAGS)

# https://wiki.debian.org/ReproducibleBuilds/
CC_EXTRA_OPTS += -DVERSION_NODATE

configure: configure-stamp
configure-stamp:
	dh_testdir
	# Add here commands to configure the package.
	$(MAKE) FLAVOUR=kamailio cfg prefix=/usr cfg_prefix=$(CURDIR)/debian/kamailio \
			cfg_target=/etc/kamailio/ \
			BASEDIR=$(CURDIR)/debian/kamailio \
			skip_modules="$(EXCLUDED_MODULES) $(EXTRA_EXCLUDED_MODULES)" \
			CC_EXTRA_OPTS="$(CC_EXTRA_OPTS)" \
			LD_EXTRA_OPTS="$(LD_EXTRA_OPTS)" \
			group_include="kstandard" quiet=verbose

	touch configure-stamp


build: build-stamp
build-arch: build-stamp
build-indep: build-stamp
build-stamp: configure-stamp
	dh_testdir
	# Add here commands to compile the package.
	$(MAKE) all quiet=verbose
	# make groups
	$(call PACKAGE_GRP_BUILD_template)
	$(call PACKAGE_EXTRA_BUILD_template)
	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_prep
	dh_installdirs
	# Add here commands to install the package into debian/kamailio
	$(MAKE) install group_include="kstandard"
	# make group packages
	$(call PACKAGE_GRP_INSTALL_template)
	$(call PACKAGE_EXTRA_INSTALL_template)
	# move binaries of kamailio-berkeley-modules to kamailio-berkeley-bin
	mkdir -p $(CURDIR)/debian/kamailio-berkeley-bin/usr/
	mv $(CURDIR)/debian/kamailio-berkeley-modules/usr/sbin \
		$(CURDIR)/debian/kamailio-berkeley-bin/usr/
	# install /etc/default/kamailio file
	mkdir -p $(CURDIR)/debian/kamailio/etc/default
	cp -f debian/kamailio.default $(CURDIR)/debian/kamailio/etc/default/kamailio
	# delete /var/run/kamailio dir
	rm -rf $(CURDIR)/debian/kamailio/usr/local


# 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 -Xobsoleted -Xsr
	dh_installmenu
	dh_systemd_enable -pkamailio
	dh_installinit -pkamailio -- defaults 23
	dh_systemd_start -pkamailio
	dh_installcron
	dh_installman
	dh_installinfo
	dh_lintian
	dh_installchangelogs ChangeLog
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs
	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