#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright 2015 Vasudev Kamath <kamathvasudev@gmail.com>
# Copyright 2015 Ramakrishnan Muthukrishnan <ram@rkrishnan.org>
# Copyright 2016-2017 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for librem
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

major := $(shell grep -Po '\bVER_MAJOR := \K\d+' Makefile)
lib = librem
pkg-lib = $(lib)$(major)

include /usr/share/pkg-kde-tools/makefiles/1/cdbs/symbolshelper.mk
include /usr/share/cdbs/1/class/langcore.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg-dev = $(lib)-dev
pkg-dbg = $(pkg-lib)-dbg

LDFLAGS += -Wl,-soname,librem.so.0

# LIBDIR for installation
LIBDIR=/usr/lib/

# TODO: Drop when makefiles.mk can handle the package
common-build-arch common-build-indep:: build_rem_package
build_rem_package:
	make info all RELEASE=1 \
		HAVE_LIBPTHREAD=1 \
		HAVE_ARMV6=$(filter armhf ,$(DEB_HOST_ARCH)) \
		USE_OPENSSL=1 \
		USE_ZLIB=1 \
		EXTRA_CFLAGS="$(CFLAGS) $(CPPFLAGS)" \
		EXTRA_LFLAGS="$(LDFLAGS)" \
		LIB_SUFFIX=.so.0

common-install-arch common-install-indep:: install_rem_package
install_rem_package:
	make install DESTDIR=debian/tmp \
		LIBDIR=$(LIBDIR) \
		LIB_SUFFIX=.so.0
	cd debian/tmp/$(LIBDIR) && ln -s -f librem.so.0 librem.so
	sed -i "s,libdir=\$${prefix}/lib,libdir=\$${prefix}/lib/$(DEB_TARGET_MULTIARCH)," debian/tmp/$(LIBDIR)/pkgconfig/librem.pc

binary-post-install/$(pkg-lib) binary-post-install/$(pkg-dev):: \
 debian/stamp-local-shlibs-$(lib)
debian/stamp-local-shlibs-$(lib): \
 binary-install/$(pkg-lib) binary-install/$(pkg-dev)
	d-shlibmove --commit \
		--devunversioned \
		--exclude-la \
		--multiarch \
		--override s/$(lib)$(major)-dev/$(lib)-dev/ \
		--override s/libre0-dev/libre-dev/ \
		--movedev "debian/tmp/usr/include/*" usr/include/ \
		--movedev "debian/tmp/usr/lib//pkgconfig/librem.pc" usr/lib/$(DEB_TARGET_MULTIARCH)/pkgconfig \
		debian/tmp/usr/lib/$(lib).so
	touch $@
clean::
	rm -f debian/stamp-local-shlibs-$(lib)
	make clean
