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

# build identifier
BUILD_ID = $(shell cut -d ' ' -f 1,1 < /etc/issue.net)_$(shell dpkg-architecture -qDEB_BUILD_ARCH_CPU)

# Uncomment this to turn on verbose mode.
#export DH_VERBOSE=1
# caret build depends on some env vars
export VTK_INC_DIR=$(shell dirname $$(find /usr/include/ -type f -name vtkConfigure.h | head -n1))
export VTK_LIB_DIR=/usr/lib
export QWT_INC_DIR=/usr/include/qwt-qt4
export QWT_LIB_DIR=/usr/lib
export NETCDF_INC_DIR=/usr/include
export NETCDF_LIB_DIR=/usr/lib
export QTDIR=/usr

CARET_APPS="caret caret_command"
CXXFLAGS = -Wall -g

ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
	CXXFLAGS += -O0
else
	CXXFLAGS += -O2
endif


cfg-%:
	@echo "Configuring: '$*'"
	cd $* && qmake-qt4 "CONFIG+=release ubuntu dll" \
	                   "QMAKE_CXXFLAGS_RELEASE=$(CXXFLAGS) -DCARET_BUILDID=\"$(BUILD_ID)\"" \
	                   "QMAKE_LFLAGS_RELEASE += -Wl,--as-needed" \
	                   "CONFIG -= profile"


build-%:
	@echo "Building: '$*'"
	cd $* && make


clean-%:
	@echo "Cleaning: '$*'"
	# ignore when there is no Makefile
	-cd $* && make clean
	-cd $* && rm Makefile


configure: configure-stamp
configure-stamp: cfg-caret_statistics cfg-caret_common cfg-caret_brain_set \
                 cfg-caret_command_operations cfg-caret_files cfg-caret_cifti \
                 cfg-caret_uniformize cfg-caret_widgets cfg-caret \
                 cfg-caret_command
	dh_testdir
	touch $@


build: build-stamp
build-stamp: configure \
             build-caret_statistics build-caret_common \
             build-caret_uniformize build-caret_widgets build-caret_files \
             build-caret_brain_set build-caret_cifti build-caret_command_operations \
             build-caret build-caret_command
	dh_testdir
	touch $@


clean: clean-caret_statistics clean-caret_common clean-caret_brain_set \
       clean-caret_command_operations clean-caret_cifti clean-caret_files \
       clean-caret_uniformize clean-caret_widgets clean-caret \
       clean-caret_command
	dh_testdir
	dh_testroot
	-find . -name '*.so' -exec rm '{}' \;
	-rm caret/caret5 caret_command/caret_command
	rm -f build-stamp configure-stamp
	
	dh_clean


install: build
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs
	
	# install internal libs
	find $(CURDIR) -name '*.so' -exec cp \{\} $(CURDIR)/debian/caret/usr/lib/caret/lib \;
	
	# install binaries
	cp caret/caret5 caret_command/caret_command $(CURDIR)/debian/caret/usr/lib/caret/bin
	
	# ensure executable permissions for wrapper script
	chmod +x debian/caret_app_wrapper


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


# Build architecture-dependent files here.
binary-arch: build install
	dh_testdir
	dh_testroot
	dh_installchangelogs
	dh_installdocs
	dh_install
	dh_installmenu
	dh_installman
	dh_desktop
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_installdeb
	dh_shlibdeps -lcaret_brain_set:caret_command_operations:caret_uniformize:caret_widgets:caret_common:caret_files:caret_statistics:caret_cifti
	dh_gencontrol
	dh_md5sums
	dh_builddeb


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