#!/usr/bin/make -f

UPSTREAM_VERSION=0.7.R1002
PACKAGE_VERSION=0.7
get-orig-source:
	rm -rf canorus-$(PACKAGE_VERSION)/
	tar xjf canorus_$(UPSTREAM_VERSION)_source.tar.bz2
	# Most importantly, remove upstream's debian directory
	find canorus-$(UPSTREAM_VERSION)/ -type d -name "debian" | xargs rm -rf
	# Remove upstream's building information
	find canorus-$(UPSTREAM_VERSION)/ -type d -name "\.settings" | xargs rm -rf
	find canorus-$(UPSTREAM_VERSION)/ -type d -name "\.externalToolBuilders" | xargs rm -rf
	find canorus-$(UPSTREAM_VERSION)/ -type f -name "\.*project" | xargs rm -rf
	# Remove files for different platforms
	find canorus-$(UPSTREAM_VERSION)/ -type d -name "windows" | xargs rm -rf
	find canorus-$(UPSTREAM_VERSION)/ -type d -name "macosx" | xargs rm -rf
	# Remove zlib directory
	find canorus-$(UPSTREAM_VERSION)/ -type d -name "zlib" | xargs rm -rf
	mv canorus-$(UPSTREAM_VERSION)/ canorus-$(PACKAGE_VERSION)/
	tar czf canorus_$(PACKAGE_VERSION).orig.tar.gz canorus-$(PACKAGE_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

build:
	dh_testdir
	cmake . -DCMAKE_INSTALL_PREFIX="/usr" -DNO_RUBY=1 -DNO_PYTHON=1
	make

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

install: build
	dh_testdir
	make DESTDIR=$(CURDIR)/debian/tmp install
	dh install --before dh_installchangelogs
	dh_installchangelogs NEWS
	dh install --before dh_link
	# Correct location of example files
	mv $(CURDIR)/debian/canorus/usr/share/doc/canorus/examples/examples/* \
	$(CURDIR)/debian/canorus/usr/share/doc/canorus/examples
	rm -rf $(CURDIR)/debian/canorus/usr/share/doc/canorus/examples/examples/
	# 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
	dh install --remaining

# Build architecture-independent files here.
binary-indep: install
	dh $@

# Build architecture-dependent files here.
binary-arch: install
	dh $@

binary: binary-indep binary-arch
.PHONY: build clean binary-indep binary-arch binary install
