#!/usr/bin/make -f
# -*- makefile -*-

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

# Ubuntu defaults to use -Bsymbolic-functions, which breaks linking
# shared objects with static FFmpeg archives (see:
# https://bugs.launchpad.net/ubuntu/+source/ffmpeg/+bug/1942352).
ifeq ($(shell dpkg-vendor --derives-from Ubuntu && echo yes),yes)
ifneq (,$(LDFLAGS))
  LDFLAGS := $(filter-out %-Bsymbolic-functions,$(LDFLAGS))
endif
endif

# Number of CPUS
NO_CPUS=$(shell nproc)
ifeq ($(NO_CPUS),0)
NO_CPUS=1
endif

# Binary package names
JAMI_ALL_IN_ONE_PKG_NAME="jami-all"
JAMI_CLIENT_PKG_NAME="jami"
JAMI_DAEMON_PKG_NAME="jami-daemon"

# Bundled packages from contrib
BUNDLED_PKGS=""
ifeq (raspbian_10_armhf,$(findstring raspbian_10_armhf, $(DISTRIBUTION)))
# Raspbian's yaml-cpp lib does not work properly
BUNDLED_PKGS="--enable-ffmpeg --enable-yaml-cpp"
# Add host environment variables
CMAKE_OPTIONS=-DCHOST=${HOST_ARCH} \
  -DCMAKE_C_COMPILER=${HOST_ARCH}-gcc \
  -DCMAKE_CXX_COMPILER=${HOST_ARCH}-g++ \
  -DCMAKE_FIND_ROOT_PATH=/usr/${HOST_ARCH} \
  -DCMAKE_BUILD_TYPE=Release \
  -DPKG_CONFIG_EXECUTABLE=/usr/bin/${HOST_ARCH}-pkg-config
endif

# Qt-related variables
QT_JAMI_PREFIX := ${QT_JAMI_PREFIX}
export PATH := $(QT_JAMI_PREFIX)/bin:${PATH}
export LD_LIBRARY_PATH := $(QT_JAMI_PREFIX)/lib:${LD_LIBRARY_PATH}
export PKG_CONFIG_PATH := $(QT_JAMI_PREFIX)/lib/pkgconfig:${PKG_CONFIG_PATH}
export CMAKE_PREFIX_PATH := $(QT_JAMI_PREFIX):${CMAKE_PREFIX_PATH}
export CFLAGS := $(CFLAGS) -fno-lto
export CXXFLAGS := $(CXXFLAGS) -fno-lto

# Installation directories.
OCI_INSTALL_DIR = $(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME)

%:
	dh $@

override_dh_auto_configure:

override_dh_auto_build:
	# Daemon build
	mkdir -p daemon/build
	cd daemon/build && \
	cmake \
	  -DCMAKE_INSTALL_PREFIX=/usr \
	  -DJAMI_DBUS=On \
	  -DBUILD_SHARED_LIBS=On \
	  -DBUILD_TESTING=Off \
	  $(CMAKE_OPTIONS) ..
	make -C daemon/build -j$(NO_CPUS) VERBOSE=1
	pod2man daemon/man/jamid.pod > daemon/jamid.1

	# Qt client configure and build
	mkdir build && \
	cd build && \
	cmake \
	  -DBUILD_VERSION=$(BUILD_VERSION) \
	  -DCMAKE_INSTALL_PREFIX=/usr \
	  -DLIBJAMI_BUILD_DIR=$(CURDIR)/daemon/src \
	  -DENABLE_LIBWRAP=true \
	  -DBUILD_TESTING=Off \
	  $(CMAKE_OPTIONS) ..
	make -C build -j$(NO_CPUS) VERBOSE=1

override_dh_auto_clean:
	# Daemon clean
	rm -rfv daemon/contrib/native* daemon/contrib/build*
	rm -rfv daemon/jamid.1

	# Qt client clean
	# CMake build system has no distclean target, so use clean.
	[ -f build/Makefile ] && make -C build clean || true
	rm -rfv build

override_dh_auto_install:
	# Daemon install
	cd daemon/build && make DESTDIR=$(CURDIR)/debian/$(JAMI_DAEMON_PKG_NAME) install
	rm -rfv $(CURDIR)/debian/$(JAMI_DAEMON_PKG_NAME)/usr/include
	rm -rfv $(CURDIR)/debian/$(JAMI_DAEMON_PKG_NAME)/usr/lib/*.a
	rm -rfv $(CURDIR)/debian/$(JAMI_DAEMON_PKG_NAME)/usr/lib/*.la

	# Qt client install
	cd build && \
	make DESTDIR=$(CURDIR)/debian/$(JAMI_CLIENT_PKG_NAME) install

	## Custom Qt package for Jami (libqt-jami)
	## Copy our own Qt library package content into the OCI package.
	for file_name in $$(dpkg-query -L libqt-jami); do 	\
	  mkdir -p "$(OCI_INSTALL_DIR)$$(dirname $$file_name)"; \
	  test -d "$$file_name" && continue;  			\
	  cp "$$file_name" "$(OCI_INSTALL_DIR)$$file_name";	\
	done

	# Jami all-in-one install
	#   Daemon
	cd daemon/build && make DESTDIR=$(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME) install
	rm -rfv $(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME)/usr/include
	rm -rfv $(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME)/usr/lib/*.a
	rm -rfv $(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME)/usr/lib/*.la
	#   Qt client
	cd build && \
	make DESTDIR=$(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME) install

override_dh_shlibdeps:
	dh_shlibdeps -- -x$(JAMI_ALL_IN_ONE_PKG_NAME)

tmpdir:= $(shell mktemp -d)
workdir:= $(shell pwd)
PKD := $(abspath $(dir $(MAKEFILE_LIST)))
version_to_download := $(shell dpkg-parsechangelog -ldebian/changelog | perl -ne 'print $$1 if m{^Version:\s+(?:\d+:)?(\d.*)(?:\~dfsg.+)(?:\-\d+.*)};')
