#!/usr/bin/make -f

# Enable verbose builds to be able to spot missing hardening flags
export VERBOSE=1

# Use all hardening features
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# CMake doesn't use CPPFLAGS, pass them to CFLAGS/CXXFLAGS to enable the
# missing (hardening) flags.
export DEB_CFLAGS_MAINT_APPEND   = $(shell dpkg-buildflags --get CPPFLAGS)
export DEB_CXXFLAGS_MAINT_APPEND = $(shell dpkg-buildflags --get CPPFLAGS)

%:
	dh $@

override_dh_auto_configure:
	cmake . -DCMAKE_INSTALL_PREFIX="/usr" -DNO_RUBY=1 -DNO_PYTHON=1

override_dh_installchangelogs:
	dh_installchangelogs NEWS

override_dh_install:
	dh_install
	# Copy the .desktop file, upstream misses that
	cp canorus.desktop $(CURDIR)/debian/canorus/usr/share/applications/
	# Remove FreeSans.ttf, the package depends on ttf-freefont
	rm -rf $(CURDIR)/debian/canorus-data/usr/share/canorus/fonts/FreeSans.ttf

override_dh_clean:
	dh_clean
	# Preserve handwritten Makefiles during clean
	mv doc/usersguide/Makefile doc/usersguide/Makefile.upstream
	mv doc/developersguide/Makefile doc/developersguide/Makefile.upstream
	# Remove left over files from upstream (there is no distclean target)
	find -type f -name "*\.qm" | xargs rm -f
	find -type f -name "*\.cmake" | xargs rm -f
	find -type f -name "Makefile" | xargs rm -f
	find -type f -name "CMakeCache\.txt" | xargs rm -f
	find -type f -name "install_manifest\.txt" | xargs rm -f
	find -type d -name "CMakeFiles" | xargs rm -rf
	# Restore handwritten Makefiles after clean
	mv doc/usersguide/Makefile.upstream doc/usersguide/Makefile
	mv doc/developersguide/Makefile.upstream doc/developersguide/Makefile

# This target is for the creation of a new tarball which should
# be completely free according to the DFSG.
#
# As we need to create a new tarball anyway, this target can be
# used to clean up some cruft and unneeded files from upstream's
# releases.
#
# NOTE:
# Make sure to *not* use the included zlib sources for linking,
# but the library provided by Debian.
#
# NOTE:
# The only free example score is examples/musicxml/Echigo-Jishi.xml,
# all other scores do not have a license.

UPSTREAM_VERSION=0.7.1rc1
DEBIAN_VERSION=0.7.1~rc1+dfsg
get-orig-source:
	rm -rf canorus-$(DEBIAN_VERSION)/
	tar xaf canorus-$(UPSTREAM_VERSION).tar.bz2
	mv canorus-$(UPSTREAM_VERSION) canorus-$(DEBIAN_VERSION)
	# This is needed to comply with DFSG
	mv canorus-$(DEBIAN_VERSION)/examples/musicxml/Echigo-Jishi.xml canorus-$(DEBIAN_VERSION)/examples/musicxml/backup
	find canorus-$(DEBIAN_VERSION)/examples/musicxml/ -type f -name "*\.xml" | xargs rm -rf
	mv canorus-$(DEBIAN_VERSION)/examples/musicxml/backup canorus-$(DEBIAN_VERSION)/examples/musicxml/Echigo-Jishi.xml
	# Remove upstream's debian directory
	find canorus-$(DEBIAN_VERSION)/ -type d -name "debian" | xargs rm -rf
	# Remove upstream's building information
	find canorus-$(DEBIAN_VERSION)/ -type d -name "\.settings" | xargs rm -rf
	find canorus-$(DEBIAN_VERSION)/ -type d -name "\.externalToolBuilders" | xargs rm -rf
	find canorus-$(DEBIAN_VERSION)/ -type f -name "\.*project" | xargs rm -rf
	# Remove files for different platforms
	find canorus-$(DEBIAN_VERSION)/ -type d -name "windows" | xargs rm -rf
	find canorus-$(DEBIAN_VERSION)/ -type d -name "macosx" | xargs rm -rf
	# Remove zlib directory
	find canorus-$(DEBIAN_VERSION)/ -type d -name "zlib" | xargs rm -rf
	tar cjf canorus_$(DEBIAN_VERSION).orig.tar.bz2 canorus-$(DEBIAN_VERSION)/
	rm -rf canorus-$(DEBIAN_VERSION)/

# TODO: Provide a target to re-create the xpm pixmap for the menu
# This requires gm to be installed
create-menu-pixmap:
	# This does not work, the pixmaps are generated by hand with gimp
	#gm convert -resize 32x32 src/ui/images/clogosm.png debian/canorus-32x32.xpm
	#gm convert -resize 16x16 src/ui/images/clogosm.png debian/canorus-16x16.xpm
