#!/usr/bin/make -f

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

LC_ALL:=C.UTF-8
export LC_ALL

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:=	-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
# enable more debugging
OUR_CPPFLAGS+=	-UNDEBUG
# disable phoning home
OUR_CPPFLAGS+=	-DMSCORE_NO_UPDATE_CHECKER

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

# things that cannot be passed via CPPFLAGS
OUR_CPPFLAGS+=	-I$(shell pwd)/debian/q-include

export DEB_BUILD_MAINT_OPTIONS := hardening=+all
export DEB_CPPFLAGS_MAINT_APPEND := ${OUR_CPPFLAGS}
export DEB_CFLAGS_MAINT_APPEND := ${OUR_CFLAGS}
export DEB_CXXFLAGS_MAINT_APPEND := ${OUR_CXXFLAGS}
export DEB_LDFLAGS_MAINT_APPEND := ${OUR_LDFLAGS}

%:
	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_WEBENGINE=OFF
CMAKE_DEFS+=	-DOMR=ON
CMAKE_DEFS+=	-DUSE_SYSTEM_POPPLER=ON
CMAKE_DEFS+=	-DBUILD_CRASH_REPORTER=OFF

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

CMAKE_DEFS+=	-DMUSESCORE_LABEL="Snapshot package"
CMAKE_DEFS+=	-DMSCORE_INSTALL_SUFFIX="-snapshot"

override_dh_auto_configure:
	rm -rf debian/q-include
	mkdir debian/q-include
	printf '#define DEB_PKG_VERSION "%s (%s %s/%s)"\n' \
	    "$$(dpkg\-parsechangelog | sed -n '/^Version: */{s///p;q;}')" \
	    "$$(lsb_release -si)" \
	    "$$(lsb_release -sc)" \
	    "${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
	doxygen Doxyfile.plugins

override_dh_auto_test:
	exec mksh debian/buildtest ${DEB_BUILD_OPTIONS}

override_dh_install-indep:
	# move stuff to packages
	dh_install
	# add doxygen-generated files
	cd doc && exec cp -r plugins \
	    ../debian/musescore-snapshot-common/usr/share/mscore-*/manual/
	dh_doxygen -pmusescore-snapshot-common \
	    $$(cd debian/musescore-snapshot-common && \
	    echo usr/share/mscore-*/manual/plugins/html)
	# doc-base insists on /usr/share/doc/ prefix
	mkdir -p debian/musescore-snapshot-common/usr/share/doc/musescore-snapshot-common
	ln -sfTr \
	    debian/musescore-snapshot-common/usr/share/mscore-*/manual/plugins \
	    debian/musescore-snapshot-common/usr/share/doc/musescore-snapshot-common/plugin-doxy
	# make system-wide soundfonts available
	cd debian/musescore-snapshot-common/usr/share/mscore-* && \
	    mkdir -p sound && \
	    ln -sf ../../sounds/sf2 ../../sounds/sf3 ../../sounds/sfz sound/

override_dh_auto_clean:
	rm -rf debian/q-include
	dh_auto_clean
	# remove possible doxygen remnants
	rm -rf doc/plugins
