#!/usr/bin/make -f

#export DH_VERBOSE=1

DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE  )
DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE )
DEB_BUILD_ARCH_OS   ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH_OS  )
ARCH                := $(shell dpkg-architecture -qDEB_BUILD_ARCH     )

# of course, qmake wants to do it its own way
QMAKE_CXXFLAGS += -Wall -Wshadow -Wextra -ggdb3
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
  QMAKE_CXXFLAGS_RELEASE = -O0
else
  QMAKE_CXXFLAGS_RELEASE = -O2
endif

# some useful variables
TOPDIR      :=$(shell pwd)
PKGNAME     :=$(word 2,$(shell dpkg-parsechangelog | grep ^Source  ) )
FULLVERSION :=$(word 2,$(shell dpkg-parsechangelog | grep ^Version ) )
UPSTREAMVER :=$(shell echo $(FULLVERSION) | sed 's/-[0-9.]\+$$//' | sed 's/^0~//' )

build: build-stamp
build-stamp:
	dh_testdir
	qmake-qt4 -makefile -after \
		QMAKE_CXXFLAGS="$(QMAKE_CXXFLAGS)" \
		QMAKE_CXXFLAGS_RELEASE="$(QMAKE_CXXFLAGS_RELEASE)"

	# libpthread is only used indirectly; no need for linking against it
	sed -i -e 's/-lpthread//' Makefile

	$(MAKE)
	touch build-stamp

clean:
	dh_testdir
	test ! -f Makefile  ||  $(MAKE) distclean
	dh_clean

install: build 
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	install -m 0755 -d debian/edfbrowser/usr/bin/
	install -m 0755 -d debian/edfbrowser/usr/share/doc/edfbrowser/
	install -m 0755 edfbrowser debian/edfbrowser/usr/bin/edfbrowser
	install -m 0755 "doc/EDFbrowser manual.html" \
		debian/edfbrowser/usr/share/doc/edfbrowser/manual.html

# Build architecture-independent files here.
binary-indep: build install
# We have nothing to do

# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_installman debian/edfbrowser.1
	dh_strip
	dh_compress
	dh_fixperms
	dh_link
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol
	dh_md5sums
	dh_builddeb

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