#!/usr/bin/make -f
# -*- makefile -*-

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1

# This has to be exported to make some magic below work.
export DH_OPTIONS

CXXFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CXXFLAGS += -O0
else
	CXXFLAGS += -O2
endif
ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMJOBS = $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    MAKEFLAGS += -j$(NUMJOBS)
endif

configure: configure-stamp
configure-stamp:
	dh_testdir

	/bin/gunzip -c /usr/share/doc/zlib1g-dev/examples/minigzip.c.gz > minigzip.c
	/usr/bin/patch -p1 < debian/patches-zlib/minigzip.diff
	rm -f build.conf
	cp build.conf.example build.conf
	qmake-qt4 -after \
		'QMAKE_CXXFLAGS_RELEASE = $(CXXFLAGS)' \
		'QMAKE_POST_LINK ~= s/strip/:' \
		qtiplot.pro

	mkdir -p icons/48x48
	mkdir -p icons/128x128
	cp qtiplot_logo.png icons/128x128/qtiplot.png
	convert qtiplot_logo.png -resize 48x48 icons/48x48/qtiplot.png

	touch configure-stamp

build: build-arch build-indep

build-arch: build-arch-stamp
build-arch-stamp: configure-stamp

	$(MAKE)

	touch $@

build-indep: build-indep-stamp
build-indep-stamp: configure-stamp

	doxygen Doxyfile
	$(MAKE) -C manual -f Makefile html-pdf
	touch $@

clean:
	dh_testdir
	dh_testroot

	rm -f build-arch-stamp build-indep-stamp configure-stamp

	[ ! -f Makefile ] || $(MAKE) clean
	find -type f -name Makefile ! -path ./manual/Makefile -exec rm -f {} \;
	find -type f -name '*.qm' -exec rm -f {} \;
	rm -rf icons
	rm -f minigzip.c
	rm -f qtiplot/qtiplot
	rm -f manual/html/*.html
	rm -f manual/qtiplot-manual-en.pdf
	rm -f manual/html/qtiplot.adp
	rm -rf API
	rm -rf tmp
	rm -f qtiplot/translations/*.qm
	rm -rf 3rdparty/qwtplot3d/lib
	rm -rf 3rdparty/qwtplot3d/tmp
	rm -f build.conf
	rm -f 3rdparty/qwt/lib/libqwt.a
	dh_clean

install: install-arch install-indep
install-indep:
	dh_testdir
	dh_testroot
	dh_prep -i

install-arch:
	dh_testdir
	dh_testroot
	dh_prep -s
	dh_installdirs -s

	# use dh_install for all files to move instead of cleaning behind
	# $(MAKE) INSTALL_ROOT=$(CURDIR)/debian/qtiplot install
	## move manual to doc package
	# mkdir -p $(CURDIR)/debian/qtiplot//usr/share/doc/qtiplot-doc/
	# mv $(CURDIR)/debian/qtiplot//usr/share/doc/qtiplot/manual $(CURDIR)/debian/qtiplot//usr/share/doc/qtiplot-doc/
	## For some reason all header files for QWT are installed, removing.
	# rm -rf debian/qtiplot/usr/local

binary-common:
	dh_testdir
	dh_testroot
	dh_install -X.js
	dh_installman
	dh_installchangelogs
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installmime
	dh_python2
	dh_link
	dh_strip
	dh_compress --exclude=.pdf --exclude=.adp
	dh_fixperms
	dh_makeshlibs
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-s binary-common

binary: binary-arch binary-indep

get-orig-source:
	VER=$(shell dpkg-parsechangelog | sed -rne 's,^Version: ([^-+~]+).*,\1,p'); \
	uscan --force-download --rename --download-version=$$VER --destdir=. && (\
	tar -xj --exclude 3rdparty/liborigin --exclude manual/html/qtiplot.adp --exclude 3rdparty/zlib --exclude .gitignore -f qtiplot_$$VER.orig.tar.bz2 ; \
	chmod -R o-w qtiplot-$$VER ; \
	fromdos `find qtiplot-$$VER -type f |grep -v .png`; \
	find qtiplot-$$VER -type f -exec chmod a-x {} \; ;\
	rm -f qtiplot-$$VER/.gitignore ;\
	tar -czf qtiplot_$$VER.orig.tar.gz qtiplot-$$VER; \
	rm -rf qtiplot-$$VER; \
	rm -f qtiplot_$$VER.orig.tar.bz2);

.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch configure get-orig-source
