#!/usr/bin/make -f
# (c) 2012, Eric Maeker, MD (fr), The FreeMedForms project, BSD 3 clause license
# The @ in front of a command prevents make from displaying

# The new build system of the FreeMedForms project (0.8.0) creates the
#   buildpsecs/compiling.pri
# during the qmake step. This file differs from apps to apps, so we need
# cache it and reuse it before doing any make steps.

# vars
FREEMEDFORMS_EMR:=freemedforms
FREEDIAMS:=freediams
FREEACCOUNT:=freeaccount

QMAKE:=qmake-qt4
QMAKE_GLOBAL_CONFIG:="CONFIG+=LINUX_INTEGRATED"
QMAKE_FREEMEDFORMS_CONFIG := "CONFIG+=with-alerts" "CONFIG+=with-webcam"
QMAKE_SUBPROJECT_CONFIG := "CONFIG+=dontbuildlibs" "CONFIG+=dontinstallresources" "CONFIG+=dontinstalllibs"
QMAKE_IPATH := "INSTALL_ROOT_PATH=/usr/"
QMAKE_COMPILING:=buildspecs/compiling.pri

# func
AUTOCONF_MAIN = -r $(QMAKE_GLOBAL_CONFIG) $(QMAKE_IPATH) $(QMAKE_FREEMEDFORMS_CONFIG) $(1).pro
AUTOCONF_SUB = -r $(QMAKE_GLOBAL_CONFIG) $(QMAKE_IPATH) $(QMAKE_SUBPROJECT_CONFIG) $(1).pro

%:
	dh $@ --parallel --buildsystem=qmake_qt4 # --dbg-package=freemedforms-project-dbg

override_dh_auto_configure:
	lrelease global_resources/translations/*.ts

	dh_auto_configure -D$(FREEMEDFORMS_EMR) -- $(call AUTOCONF_MAIN, $(FREEMEDFORMS_EMR)) $(extra_configure_opts)
	cp $(QMAKE_COMPILING) build/$(FREEMEDFORMS_EMR).pri

	dh_auto_configure -D$(FREEDIAMS) -- $(call AUTOCONF_SUB, $(FREEDIAMS)) $(extra_configure_opts)
	cp $(QMAKE_COMPILING) build/$(FREEDIAMS).pri

	dh_auto_configure -D$(FREEACCOUNT) -- $(call AUTOCONF_SUB, $(FREEACCOUNT)) $(extra_configure_opts)
	cp $(QMAKE_COMPILING) build/$(FREEACCOUNT).pri

override_dh_auto_clean:
	rm -Rf bin
	rm -Rf build
	rm -f global_resources/translations/[a-ps-z]*.qm
	dh_auto_clean
	find . -name Makefile -delete

override_dh_auto_build:
	cp build/$(FREEMEDFORMS_EMR).pri $(QMAKE_COMPILING)
	dh_auto_build -D$(FREEMEDFORMS_EMR)

	cp build/$(FREEDIAMS).pri $(QMAKE_COMPILING)
	dh_auto_build -D$(FREEDIAMS)

	cp build/$(FREEACCOUNT).pri $(QMAKE_COMPILING)
	dh_auto_build -D$(FREEACCOUNT)

override_dh_auto_install:
	cp build/$(FREEMEDFORMS_EMR).pri $(QMAKE_COMPILING)
	dh_auto_install -D$(FREEMEDFORMS_EMR) -Smakefile -- INSTALL_ROOT=$(CURDIR)/debian/tmp

	cp build/$(FREEDIAMS).pri $(QMAKE_COMPILING)
	dh_auto_install -D$(FREEDIAMS) -Smakefile -- INSTALL_ROOT=$(CURDIR)/debian/tmp

	cp build/$(FREEACCOUNT).pri $(QMAKE_COMPILING)
	dh_auto_install -D$(FREEACCOUNT) -Smakefile -- INSTALL_ROOT=$(CURDIR)/debian/tmp	
	
	# Remove empty dirs
	find $(CURDIR)/debian/tmp/usr/share -type d -empty -delete

get-orig-source:
	mkdir -p ../tarballs
	uscan --verbose --force-download --destdir=../tarballs

