#!/usr/bin/make -f

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all
DPKG_EXPORT_BUILDFLAGS = 1

include /usr/share/dpkg/architecture.mk
include /usr/share/dpkg/buildflags.mk
include /usr/share/dpkg/pkg-info.mk

# Enable parallel builds.
NUMJOBS = 1
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
  MAKEFLAGS += -j$(NUMJOBS)
endif

# 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
EXTRA_EXCLUDED_MODULES += dbtext
EXTRA_EXCLUDED_MODULES += dnssec
EXTRA_EXCLUDED_MODULES += iptrtpproxy
EXTRA_EXCLUDED_MODULES += java
EXTRA_EXCLUDED_MODULES += mi_xmlrpc
EXTRA_EXCLUDED_MODULES += oracle
EXTRA_EXCLUDED_MODULES += pa
EXTRA_EXCLUDED_MODULES += purple

# 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
PACKAGE_GROUPS += postgres
PACKAGE_GROUPS += berkeley
PACKAGE_GROUPS += unixodbc
PACKAGE_GROUPS += radius
PACKAGE_GROUPS += presence
PACKAGE_GROUPS += ldap
PACKAGE_GROUPS += xml
PACKAGE_GROUPS += perl
PACKAGE_GROUPS += utils
PACKAGE_GROUPS += lua
PACKAGE_GROUPS += memcached
PACKAGE_GROUPS += snmpstats
PACKAGE_GROUPS += xmpp
PACKAGE_GROUPS += cpl
PACKAGE_GROUPS += redis
PACKAGE_GROUPS += python3
PACKAGE_GROUPS += geoip2
PACKAGE_GROUPS += sqlite
PACKAGE_GROUPS += ims
PACKAGE_GROUPS += sctp
PACKAGE_GROUPS += tls
PACKAGE_GROUPS += outbound
PACKAGE_GROUPS += websocket
PACKAGE_GROUPS += autheph
PACKAGE_GROUPS += cnxcc
PACKAGE_GROUPS += erlang
PACKAGE_GROUPS += secsipid
PACKAGE_GROUPS += lwsc
PACKAGE_GROUPS += lcrrate
PACKAGE_GROUPS += tcap

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

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

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

# select radius lib
ifeq ($(shell [ -f /usr/include/radcli/radcli.h ] && echo 1 || echo 0),1)
export RADCLI = 1
else
export FREERADIUS = 1
endif

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

D = $(CURDIR)/debian/$(DEB_SOURCE)

# Name of libdir in the path for libraries (e.g., the multiarch triplet).
LIBDIR ?= lib/$(DEB_HOST_MULTIARCH)

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

# CFLAGS + CPPFLAGS -> CC_EXTRA_OPTS
CC_EXTRA_OPTS := $(shell dpkg-buildflags --get CPPFLAGS)
CC_EXTRA_OPTS += $(shell dpkg-buildflags --get CFLAGS)
CXXFLAGS := $(shell 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

# Disable unsafe signal handlers
CC_EXTRA_OPTS += -DNO_SIG_DEBUG

# Fix for UDP buffer issue when TLS is enabled
CC_EXTRA_OPTS += -pthread -DKSR_PTHREAD_MUTEX_SHARED
LD_EXTRA_OPTS += -pthread -rdynamic -ldl -Wl,-Bsymbolic-functions

%:
	dh $@

override_dh_auto_configure:
	$(MAKE) cfg \
	  prefix=/usr \
	  cfg_prefix=$(D) \
	  cfg_target=/etc/kamailio/ \
	  FLAVOUR=kamailio \
	  BASEDIR=$(D) \
	  CC_EXTRA_OPTS="$(CC_EXTRA_OPTS)" \
	  LD_EXTRA_OPTS="$(LD_EXTRA_OPTS)" \
	  skip_modules="$(EXCLUDED_MODULES) $(EXTRA_EXCLUDED_MODULES)" \
	  group_include="kstandard"

override_dh_auto_build:
	$(MAKE) all
	:
	# Package all the modules in PACKAGE_GROUPS in separate packages
	for grp in $(PACKAGE_GROUPS); do \
	  $(MAKE) every-module group_include="k$$grp" ; \
	done
	# Package all the modules in EXTRA_GROUPS in separate packages.
	for grp in $(EXTRA_GROUPS); do \
	  $(MAKE) every-module group_include="k$$grp" quiet=verbose ; \
	done

override_dh_auto_clean:
	if [ -r autover.h -a ! -r debian/autover.h ]; then cp -a autover.h debian/; fi
	dh_auto_clean -- maintainer-clean
	if [ -r debian/autover.h -a ! -r autover.h ]; then mv debian/autover.h .; fi

override_dh_auto_install:
	$(MAKE) install LIBDIR=$(LIBDIR) group_include="kstandard"
	:
	# Install group packages.
	for grp in $(PACKAGE_GROUPS); do \
	  $(MAKE) install-modules-all \
	    LIBDIR=$(LIBDIR) \
	    BASEDIR=$(D)-$${grp}-modules \
	    cfg_prefix=$(D)-$${grp}-modules \
	    doc-dir=share/doc/$(DEB_SOURCE)-$${grp}-modules \
	    group_include="k$${grp}" ; \
	  : Eliminate duplicate libs ; \
	  for d in $(DUP_LIBS_DIRS); do \
	    test "$$d" != "$(D)-$${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 $(D)-$${grp}-modules/usr/$(LIBDIR)/kamailio/`basename "$$r"`; \
	    done ; \
	  done ; \
	  find $(D)-$${grp}-modules -depth -empty -type d -exec rmdir {} \; ;\
	done
	:
	# Install extra packages.
	for grp in $(EXTRA_GROUPS); do \
		$(MAKE) install-modules-all LIBDIR=$(LIBDIR) group_include="k$$grp" \
		BASEDIR=$(D)-extra-modules \
		cfg_prefix=$(D)-extra-modules \
		doc-dir=share/doc/$(DEB_SOURCE)-extra-modules ; \
	done
	# Eliminate duplicate extra libs.
	-for d in $(DUP_LIBS_DIRS); do \
	  test "$$d" != "$(D)-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 $(D)-extra-modules/usr/$(LIBDIR)/kamailio/`basename "$$r"`; \
	  done ; \
	done
	find $(D)-extra-modules -depth -empty -type d -exec rmdir {} \;
	:
	# Move binaries of kamailio-berkeley-modules to kamailio-berkeley-bin.
	mkdir -p $(D)-berkeley-bin/usr/
	mv $(D)-berkeley-modules/usr/sbin \
	   $(D)-berkeley-bin/usr/

override_dh_installexamples:
	dh_installexamples -Xobsoleted -Xsr

override_dh_installsystemd:
	dh_installsystemd --name=kamailio-lb
	dh_installsystemd --name=kamailio-lb@
	dh_installsystemd --name=kamailio-proxy
	dh_installsystemd --name=kamailio-proxy@
