#!/usr/bin/make -f

CWD:=$(abspath .)

ifeq (,$(filter terse,${DEB_BUILD_OPTIONS}))
export DH_VERBOSE=1
export V=1
export VERBOSE=1
endif

LC_ALL:=C.UTF-8
export LC_ALL
TZ:=UTC
export TZ

shellescape='$(subst ','\'',$(1))'
shellexport=$(1)=$(call shellescape,${$(1)})

MSPPKG=musescore
MSCPKG=${MSPPKG}-common
MSPDIR=debian/${MSPPKG}
MSCDIR=debian/${MSCPKG}
MSHARE=usr/share/mscore-*

DEB_HOST_ARCH?=$(shell dpkg-architecture -qDEB_HOST_ARCH)
DEB_HOST_ARCH_OS?=$(shell dpkg-architecture -qDEB_HOST_ARCH_OS)

OUR_CPPFLAGS:=
OUR_CFLAGS:=
OUR_CXXFLAGS:=
OUR_LDFLAGS:=

# unclutter build log; these are upstream’s to fix
OUR_CFLAGS+=	-Wno-deprecated-declarations
OUR_CXXFLAGS+=	-Wno-deprecated-declarations
OUR_CXXFLAGS+=	-Wno-deprecated-copy

# only needed when backporting to buster and older
OUR_LDFLAGS+=	-Wl,--as-needed

# defined for most of the code anyway; doing this here helps the PCH
OUR_CPPFLAGS+=	-DQT_NO_DEBUG
# avoid stray debugging output
OUR_CPPFLAGS+=	-DNDEBUG
# disable phoning home
OUR_CPPFLAGS+=	-DMSCORE_NO_UPDATE_CHECKER

# better debugging if requested
ifneq (,$(findstring optdbg,${DEB_BUILD_OPTIONS}))
OUR_CFLAGS+=	-Og -g3
OUR_CXXFLAGS+=	-Og -g3
endif

# things that cannot be passed via CPPFLAGS
OUR_CPPFLAGS+=	-I${CWD}/debian/q-include

dpkgbuildflagsmkescape=$(subst \,\\\,$(1))
export DEB_BUILD_MAINT_OPTIONS:=hardening=+all
export DEB_CPPFLAGS_MAINT_APPEND:=$(call dpkgbuildflagsmkescape,${OUR_CPPFLAGS})
export DEB_CFLAGS_MAINT_APPEND:=$(call dpkgbuildflagsmkescape,${OUR_CFLAGS})
export DEB_CXXFLAGS_MAINT_APPEND:=$(call dpkgbuildflagsmkescape,${OUR_CXXFLAGS})
export DEB_LDFLAGS_MAINT_APPEND:=$(call dpkgbuildflagsmkescape,${OUR_LDFLAGS})
include /usr/share/dpkg/buildflags.mk
ifneq (,$(wildcard /usr/share/dpkg/buildtools.mk))
include /usr/share/dpkg/buildtools.mk
endif
include /usr/share/dpkg/pkg-info.mk

%:
	dh $@ --buildsystem=cmake

CMAKE_DEFS:=	-DCMAKE_SKIP_RPATH=ON
CMAKE_DEFS+=	-DDOWNLOAD_SOUNDFONT=OFF
CMAKE_DEFS+=	-DUSE_SYSTEM_FREETYPE=ON \
		-DFREETYPE_INCLUDE_DIRS=/usr/include/freetype2/
CMAKE_DEFS+=	-DBUILD_WEBKIT=OFF

ifneq (linux,${DEB_HOST_ARCH_OS})
CMAKE_DEFS+=	-DBUILD_PORTMIDI=OFF
endif

override_dh_auto_configure:
	test -s fonts/mscore/mscore.old || \
	    mv fonts/mscore/mscore.ttf fonts/mscore/mscore.old
	cp -f debian/upstream/mscore.ttf fonts/mscore/mscore.ttf
	rm -rf debian/q-include
	mkdir debian/q-include
	printf '#define DEB_PKG_VERSION "%s (%s %s/%s)"\n' \
	    $(call shellescape,${DEB_VERSION}) \
	    "$$(lsb_release -si)" \
	    "$$(lsb_release -sc)" \
	    $(call shellescape,${DEB_HOST_ARCH}) \
	    >debian/q-include/DEB_PKG_VERSION.h
	exec dh_auto_configure -- ${CMAKE_DEFS}

override_dh_auto_build:
	env GNUMAKEFLAGS="$$GNUMAKEFLAGS --output-sync" \
	    dh_auto_build -- lrelease all

override_dh_auto_test:
	exec mksh debian/buildtest ${DEB_BUILD_OPTIONS}

execute_after_dh_install-indep:
	# make system-wide soundfonts available
	cd ${MSCDIR}/${MSHARE} && mkdir -p sound && \
	    ln -sf ../../sounds/sf2 ../../sounds/sf3 ../../sounds/sfz sound/

execute_after_dh_auto_clean:
	test '!' -s fonts/mscore/mscore.old || \
	    mv -f fonts/mscore/mscore.old fonts/mscore/mscore.ttf
	rm -rf debian/q-include
	-chmod -R g-w . # work around #796257 for Debian reproducible-builds
