#!/usr/bin/make -f

#export DH_VERBOSE=1

CXX_INSTALLED_VERSION := $(shell dpkg-query --showformat='$${Version}' --show g++)
CXX_PREFERED_VERSION = 4:8.0
CXX_PREFERED_COMPILER = g++-8
C_PREFERED_COMPILER = gcc-8

CMAKE_FLAGS = -DICINGA2_LTO_BUILD=On

DEB_HOST_ARCH = $(shell dpkg-architecture -qDEB_HOST_ARCH)

ifneq (,$(wildcard /usr/include/icinga-boost))
CMAKE_FLAGS += \
  -DBoost_NO_BOOST_CMAKE=TRUE \
  -DBoost_NO_SYSTEM_PATHS=TRUE \
  -DBOOST_LIBRARYDIR=/usr/lib/$(DEB_HOST_MULTIARCH)/icinga-boost \
  -DBOOST_INCLUDEDIR=/usr/include/icinga-boost \
	-DCMAKE_INSTALL_RPATH=/usr/lib/$(DEB_HOST_MULTIARCH)/icinga-boost
endif

# disable unity/lto builds for arches with low memory
ifneq ($(findstring $(DEB_HOST_ARCH), armhf),)
CMAKE_FLAGS += -DICINGA2_UNITY_BUILD=Off
CMAKE_FLAGS += -DICINGA2_LTO_BUILD=Off
endif

ifneq ($(findstring $(DEB_HOST_ARCH), mips),)
CMAKE_FLAGS += -DICINGA2_UNITY_BUILD=Off
CMAKE_FLAGS += -DICINGA2_LTO_BUILD=Off
endif

# check if the installed default g++ is already newer as preferred
ifneq ($(shell dpkg --compare-versions "$(CXX_INSTALLED_VERSION)" ge "$(CXX_PREFERED_VERSION)" && echo 1 || echo 0),1)
# if not choose the prefered compiler directly
ifneq (,$(shell command -v $(CXX_PREFERED_COMPILER) 2>/dev/null))
# if it is installed
$(warning Installed GCC $(CXX_INSTALLED_VERSION) is older than $(CXX_PREFERED_VERSION) - using alternative binary $(CXX_PREFERED_COMPILER))
CMAKE_FLAGS += -DCMAKE_CXX_COMPILER=$(CXX_PREFERED_COMPILER)
CMAKE_FLAGS += -DCMAKE_C_COMPILER=$(C_PREFERED_COMPILER)
endif
endif

%:
	dh $@ --with systemd --with bash_completion

override_dh_systemd_enable:
	dh_systemd_enable --name=icinga2
override_dh_auto_build:
	dh_auto_build --parallel

override_dh_auto_configure:
	dh_auto_configure \
		-- \
		-DCMAKE_INSTALL_LOCALSTATEDIR=/var \
		-DCMAKE_INSTALL_SYSCONFDIR=/etc \
		-DICINGA2_SYSCONFIGFILE=/etc/default/icinga2 \
		-DICINGA2_RUNDIR=/run \
		-DICINGA2_USER=nagios \
		-DICINGA2_GROUP=nagios \
		-DICINGA2_COMMAND_GROUP=www-data \
		-DICINGA2_GIT_VERSION_INFO=false \
		-DICINGA2_PLUGINDIR=/usr/lib/nagios/plugins \
		-DUSE_SYSTEMD=ON \
		$(CMAKE_FLAGS)

override_dh_auto_install:
	dh_auto_install
	# remove features-enabled - these will be activated by postinst later
	rm -r debian/tmp/etc/icinga2/features-enabled/*

override_dh_install-indep:
	dh_install -i
	chmod +x debian/icinga2-common/usr/lib/icinga2/prepare-dirs
	rm debian/icinga2-common/etc/icinga2/features-available/ido-*

override_dh_install-arch:
	dh_install -a
	for dbms in mysql pgsql; do \
		mkdir -p debian/icinga2-ido-$$dbms/usr/share/dbconfig-common/data/icinga2-ido-$$dbms/install ; \
		mkdir -p debian/icinga2-ido-$$dbms/usr/share/dbconfig-common/data/icinga2-ido-$$dbms/upgrade/$$dbms; \
		cp lib/db_ido_$$dbms/schema/$$dbms.sql debian/icinga2-ido-$$dbms/usr/share/dbconfig-common/data/icinga2-ido-$$dbms/install/$$dbms ; \
		for file in lib/db_ido_$$dbms/schema/upgrade/* ; do \
			cp $$file debian/icinga2-ido-$$dbms/usr/share/dbconfig-common/data/icinga2-ido-$$dbms/upgrade/$$dbms/`basename $$file .sql`; \
		done; \
	done

override_dh_installinit:
	dh_installinit --name=icinga2

.PHONY: override_dh_strip
override_dh_strip:
	dh_strip --dbg-package=icinga2-dbg

# we don't build shared libraries
override_dh_makeshlibs:
	dh_makeshlibs --no-scripts || dh_makeshlibs --noscripts

# vi: noexpandtab ts=4 sw=4 :
