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

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

# 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_CLIENT_GNOME_PKG_NAME="jami-gnome"
JAMI_LIB_CLIENT_PKG_NAME="jami-libclient"
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} \
		-DPKG_CONFIG_EXECUTABLE=/usr/bin/${HOST_ARCH}-pkg-config
else
ifneq (ubuntu_21.04,$(findstring ubuntu_21.04, $(DISTRIBUTION)))
BUNDLED_PKGS="--enable-ffmpeg" # For ubuntu 21.04 it seems there is massive issues with linking for swscale
endif
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)/lib/cmake:${CMAKE_PREFIX_PATH}

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

%:
	dh $@

override_dh_auto_configure:
	###########################
	## Daemon configure      ##
	###########################
	mkdir -p daemon/contrib/native
	cd daemon/contrib/native && \
        ../bootstrap \
				--host=${HOST_ARCH} \
				--disable-downloads \
				--no-checksums \
				--disable-ogg \
				--disable-flac \
				--disable-vorbis \
				--disable-vorbisenc \
				--disable-speex \
				--disable-sndfile \
				--disable-gsm \
				--disable-speexdsp \
				--disable-natpmp  \
				--enable-gnutls $(BUNDLED_PKGS) && \
		make list && \
		make -j$(NO_CPUS) V=1
	cd daemon && \
		./autogen.sh && \
		./configure \
				--prefix=/usr \
				--disable-shared \
				--host=${HOST_ARCH}

	#############################
	## libringclient configure ##
	#############################
	cd lrc && \
		mkdir build && \
		cd build && \
		cmake  \
			-DRING_BUILD_DIR=$(CURDIR)/daemon/src \
			-DCMAKE_INSTALL_PREFIX=/usr \
			-DCMAKE_INSTALL_LIBDIR=lib \
			$(CMAKE_OPTIONS) \
			..

	############################
	## gnome client configure ##
	############################
	cd client-gnome && \
		mkdir build && \
		cd build && \
		cmake \
			-DCMAKE_INSTALL_PREFIX=/usr \
			-DCMAKE_INSTALL_LIBDIR=lib \
			-DLibRingClient_PROJECT_DIR=/$(CURDIR)/lrc \
			-DGSETTINGS_LOCALCOMPILE=OFF \
			$(CMAKE_OPTIONS) \
			..

	#########################
	## qt client configure ##
	#########################
	# needs to be done after lrc is built; see below

	dh_auto_configure

override_dh_auto_build:
	#######################
	## Daemon build      ##
	#######################
	make -C daemon -j$(NO_CPUS) V=1
	pod2man daemon/man/dring.pod > daemon/dring.1

	#########################
	## libringclient build ##
	#########################
	make -C lrc/build -j$(NO_CPUS) V=1

	########################
	## gnome client build ##
	########################
	make -C client-gnome/build LDFLAGS="-lpthread" -j$(NO_CPUS) V=1

	###################################
	## qt client configure and build ##
	###################################
	cd client-qt && \
		mkdir build && \
		cd build && \
		cmake \
			-DCMAKE_INSTALL_PREFIX=/usr \
			-DLRC=$(CURDIR)/lrc \
			$(CMAKE_OPTIONS) \
			..
	make -C client-qt/build -j$(NO_CPUS) V=1

override_dh_auto_clean:
	#######################
	## Daemon clean      ##
	#######################
	if [ -f daemon/contrib/native/Makefile ]; then make -C daemon/contrib/native distclean; fi
	rm -rfv daemon/contrib/native
	rm -rfv daemon/dring.1

	#########################
	## libringclient clean ##
	#########################
	# CMake build system has no distclean target, so use clean.
	if [ -f lrc/build/Makefile ]; then make -C lrc/build clean; fi
	rm -rfv lrc/build

	########################
	## gnome client clean ##
	########################
	# CMake build system has no distclean target, so use clean.
	if [ -f client-gnome/build/Makefile ]; then make -C client-gnome/build clean; fi
	rm -rfv client-gnome/build

	#####################
	## qt client clean ##
	#####################
	# CMake build system has no distclean target, so use clean.
	if [ -f client-qt/build/Makefile ]; then make -C client-qt/build clean; fi
	rm -rfv client-qt/build

override_dh_auto_install:
	#########################
	## Daemon install      ##
	#########################

	cd daemon && 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

	#########################
	## Jami client install ##
	#########################

        ## LibRingClient
	cd lrc/build && make DESTDIR=$(CURDIR)/debian/$(JAMI_LIB_CLIENT_PKG_NAME) install
	rm -rfv $(CURDIR)/debian/$(JAMI_LIB_CLIENT_PKG_NAME)/usr/include

	# This is a symlink, should be in -dev package
	rm -v $(CURDIR)/debian/$(JAMI_LIB_CLIENT_PKG_NAME)/usr/lib/libringclient.so

	# cmake files
	rm -rfv $(CURDIR)/debian/$(JAMI_CLIENT_PKG_NAME)/usr/lib/cmake

        ## GNOME client
	cd client-gnome/build && \
		make DESTDIR=$(CURDIR)/debian/$(JAMI_CLIENT_GNOME_PKG_NAME) install
	rm -rfv $(CURDIR)/debian/$(JAMI_CLIENT_GNOME_PKG_NAME)/usr/bin/jami

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

	ln -sf /usr/bin/jami $(CURDIR)/debian/jami/usr/bin/ring.cx

	## 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 AiO install ##
	######################

        ## daemon
	cd daemon && 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

        ## LibRingClient
	cd lrc/build && make DESTDIR=$(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME) install
	rm -rfv $(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME)/usr/include

	# This is a symlink, should be in -dev package
	rm -v $(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME)/usr/lib/libringclient.so

	# cmake files
	rm -rfv $(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME)/usr/lib/cmake

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

	ln -sf /usr/bin/jami $(CURDIR)/debian/$(JAMI_ALL_IN_ONE_PKG_NAME)/usr/bin/ring.cx

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+.*)};')
