#!/usr/bin/make -f
# -*- mode: makefile; coding: utf-8 -*-
# Copyright © 2009 Adrian Knoth <adi@drcomp.erfurt.thur.de>
# Copyright © 2010, 2012-2014, 2016 Jonas Smedegaard <dr@jones.dk>
# Description: Main Debian packaging script for calf
#
# 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/>.

DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTO_UPDATE_AUTOCONF = ,
DEB_AUTO_UPDATE_AUTOHEADER = ,
DEB_AUTO_UPDATE_ACLOCAL = ,
DEB_AUTO_UPDATE_AUTOMAKE = ,

include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/rules/utils.mk
include /usr/share/cdbs/1/class/autotools.mk
include /usr/share/cdbs/1/rules/debhelper.mk

pkg = calf-plugins
docdir = /usr/share/calf/doc

DEB_UPSTREAM_URL = https://github.com/calf-studio-gear/calf/archive
DEB_UPSTREAM_TARBALL_BASENAME = $(DEB_UPSTREAM_TARBALL_VERSION)

# Build-depend unversioned on debhelper
#  TODO: Drop when adopted in cdbs
CDBS_BUILD_DEPENDS_rules_debhelper_v9 = debhelper

# Needed by upstream build process
libdev-deps = glib2.0 gtk2.0 readline expat1 jack fftw3 fluidsynth cairo2
deps = $(patsubst %,$(comma) lib%-dev,$(libdev-deps))
deps +=, libasound2-dev [linux-any], lv2-dev
deps +=, libtool

CDBS_BUILD_DEPENDS +=, $(deps)

# extract metadata from images before copyright check
CDBS_BUILD_DEPENDS +=, libimage-exiftool-perl, libregexp-assemble-perl
CDBS_BUILD_DEPENDS +=, libipc-system-simple-perl, lcdf-typetools
local_inspection_regex = png|jpg
DEB_COPYRIGHT_CHECK_IGNORE_REGEX = ^((.*/)?[^/]+\.($(local_inspection_regex))|debian/(changelog|copyright(|_hints|_newhints)))$$
debian/stamp-copyright-check: debian/stamp-extract-copyright
debian/stamp-extract-copyright:
	find * -type f -regextype posix-extended \
		-regex '.*\.($(local_inspection_regex))' \
		-print0 | perl -0 debian/print-metadata.pl
	touch $@
pre-build:: clean-extracted-copyright-during-build
clean-extracted-copyright-during-build: debian/stamp-copyright-check
	find -type f -name '*.metadata_dump' -delete
clean::
	find -type f -name '*.metadata_dump' -delete
	rm -f debian/stamp-extract-copyright

# put aside upstream-shipped temp files during build but after copyright-check
DEB_UPSTREAM_CRUFT_MOVE = calf.desktop configure INSTALL \
 Makefile.in $(patsubst %,%/Makefile.in,gui gui/icons/LV2 icons src src/calf) \
 aclocal.m4 \
 config.guess config.sub \
 compile \
 config.h.in \
 depcomp \
 install-sh \
 ltmain.sh \
 missing
DEB_ACLOCAL_ARGS = -Im4 --install --force
DEB_AUTOMAKE_ARGS = --add-missing --copy --foreign --force
clean::
	rm -rf m4

# adjust location of documentation
DEB_CONFIGURE_EXTRA_FLAGS += --docdir=$(docdir)

ifneq "$(filter amd64, $(DEB_HOST_ARCH_CPU))" ""
	DEB_CONFIGURE_EXTRA_FLAGS += --enable-sse
endif

# enable experimental plugins
DEB_CONFIGURE_EXTRA_FLAGS += --enable-experimental

# Avoid useless linking
LDFLAGS += -Wl,--as-needed

install/$(pkg)::
	sed -i "/dependency_libs/ s/'.*'/''/" `find . -name '*.la'`

# use separately packaged Javascript
CDBS_BUILD_DEPENDS +=, libjs-jquery, libjs-jquery-colorbox
CDBS_DEPENDS_$(pkg) = libjs-jquery, libjs-jquery-colorbox
binary-post-install/$(pkg)::
#	rm -f $(cdbs_curdestdir)$(docdir)/scripts/jquery.js
	ln -fsT ../../../javascript/jquery/jquery.min.js \
		$(cdbs_curdestdir)$(docdir)/scripts/jquery.js
#	rm -f $(cdbs_curdestdir)$(docdir)/scripts/jquery.prettyPhoto.js
	ln -fsT ../../../javascript/jquery-colorbox/jquery.colorbox-min.js \
		$(cdbs_curdestdir)$(docdir)/scripts/jquery.prettyPhoto.js
	ln -fsT ../../calf/doc \
		$(cdbs_curdestdir)usr/share/doc/$(pkg)/html
