#!/usr/bin/make -f 
# -*- makefile -*-
# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1
DPKG_EXPORT_BUILDFLAGS = 1
include /usr/share/dpkg/buildflags.mk

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

SOVERSION=1.4
BALL_INSTALL_PATH=build
PYTHON_VERSION=2.6
PYTHON_SITE_PATH=$(shell python -c "import distutils.sysconfig; print distutils.sysconfig.get_python_lib(0,0)[4:];")

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

%:
	dh $@ --buildsystem=cmake --builddirectory=build --with python2

override_dh_auto_configure:
	mkdir -p build
	cd build && LDFLAGS="$(LDFLAGS)" CXXFLAGS="$(CXXFLAGS)" CFLAGS="$(CFLAGS)" CPPFLAGS="$(CPPFLAGS)" \
	            cmake .. -DCMAKE_SOURCE_DIR=$(CURDIR) -DCMAKE_INSTALL_PREFIX=./usr -DBALL_PATH=/usr -DBALL_DATA_PATH=/usr/share/BALL-$(SOVERSION)/data

# Set BALL_DATA_PATH so that test scripts find it
export BALL_DATA_PATH=$(CURDIR)/data
# Make sure HOME exists (for Directory_test)
export HOME=/tmp

# Disable test for arch-indep builds
override_dh_auto_test-indep:


# The testsuite fails on almost any non-amd64 port.
# Run it, but ignore the return code for now.
#
# TODO: Improve the portability of code and testsuite.
ifndef DEB_HOST_ARCH
$(warning DEB_HOST_ARCH is not defined.  I don't know which \
architecture I'm supposed to build for.  \
Expect "interesting" results.)
else ifneq ($(DEB_HOST_ARCH),amd64)
$(warning Building for $(DEB_HOST_ARCH) which is not amd64.  \
We will run the test suite, but ignore any errors to prevent FTBFS.  \
See Debian bug #830894 for the discussion and how you can help.\
)
override_dh_auto_test-arch:
	dh_auto_test || true
endif


override_dh_auto_build-arch:
	## Fix upstream issue of missing UI files
	uic-qt4 source/VIEW/DIALOGS/assignBondOrderConfigurationDialog.ui > include/BALL/VIEW/UIC/ui_assignBondOrderConfigurationDialog.h
	uic-qt4 source/VIEW/DIALOGS/assignBondOrderResultsDialog.ui       > include/BALL/VIEW/UIC/ui_assignBondOrderResultsDialog.h
	uic-qt4 source/VIEW/DIALOGS/atomOverview.ui                       > include/BALL/VIEW/UIC/ui_atomOverview.h
	## ...
	## Uhmmm, seems we need to do this for all UI files mentioned in
	##   source/VIEW/DIALOGS/sources.cmake
	## but I really hope some cmake expert might be able to find a clue
	## how to trigger cmake to do it as intended by upstream.
	## Somehow the code
	##   ADD_BALL_UIFILES("VIEW/DIALOGS" "${UI_LIST}")
	## needs to be called before the build code below can be started
	make -C build BALL VIEW BALLView build_tests install

override_dh_auto_build-indep:
	make -C build doc doc_tutorial install

override_dh_auto_clean:
	[ -d source ] && cd source ; \
	if [ -f Makefile ] ; \
	then  \
		make clean ; \
		cd PYTHON/EXTENSIONS; \
		make clean; \
		rm -rf ../lib/Linux*; \
		rm -rf ../doc/doxygen/latex/*; \
		rm -f $(CURDIR)/source/config.status 2>/dev/null; \
	fi
	rm -f data/BALLView/translations/BALLView.de_DE.qm
	rm -f data/BALLView/translations/BALLView.zh_TW.qm
	rm -f doc/TUTORIAL/*.aux
	rm -rf $(BALL_INSTALL_PATH)
	find . -name "*.o" | xargs -r rm
	find . -type l | xargs -r rm
	find doc/TUTORIAL/ -name "tutorial.*" | egrep -v ".(tex|bib)$$" |xargs -r rm
	rm -f include/BALL/BALL.doc
	# find . -name "*.doc" -delete
	# Remove self-create UI header file
	rm -f include/BALL/VIEW/UIC/ui_assignBondOrderConfigurationDialog.h \
	      include/BALL/VIEW/UIC/ui_assignBondOrderResultsDialog.h \
	      include/BALL/VIEW/UIC/ui_atomOverview.h


override_dh_auto_install-arch:
	mkdir -p debian/libball$(SOVERSION)/usr/lib \
		debian/libball$(SOVERSION)-data/usr/share/BALL-$(SOVERSION) \
		debian/libballview$(SOVERSION)/usr/lib \
		debian/python-ball/usr/share/BALL-$(SOVERSION) \
		debian/python-ball/usr/$(PYTHON_SITE_PATH) \
		debian/python-ballview/usr/$(PYTHON_SITE_PATH)

	mkdir -p debian/ballview/usr/share/BALL-$(SOVERSION)/data/graphics

	cp -r $(BALL_INSTALL_PATH)/usr/share/BALL/doc/BALLView/* debian/ballview/usr/share/doc/ballview/html
	cp    $(BALL_INSTALL_PATH)/usr/bin/BALLView		 debian/ballview/usr/bin/

	find $(BALL_INSTALL_PATH)/usr/lib/ -name libBALL*.so.* | xargs -r cp --target-directory debian/libball$(SOVERSION)/usr/lib/
	find $(BALL_INSTALL_PATH)/usr/lib/ -name libVIEW*.so.* | xargs -r cp --target-directory debian/libballview$(SOVERSION)/usr/lib/

	# debian needs the logo in the ballview - package, not in the libball
	cp $(BALL_INSTALL_PATH)/usr/share/BALL/data/graphics/ballview*.xpm debian/ballview/usr/share/pixmaps/

	cp -r source/EXAMPLES/PYTHON    debian/python-ball/usr/share/BALL-$(SOVERSION)
	cp $(BALL_INSTALL_PATH)/usr/lib/BALL.py       debian/python-ball/usr/$(PYTHON_SITE_PATH)
	cp $(BALL_INSTALL_PATH)/usr/lib/BALLCore.so   debian/python-ball/usr/$(PYTHON_SITE_PATH)
	cp $(BALL_INSTALL_PATH)/usr/lib/VIEW.so       debian/python-ballview/usr/$(PYTHON_SITE_PATH)

	chmod ugo-x debian/python-ball/usr/share/BALL-$(SOVERSION)/PYTHON/UTILITIES/*.py
	chmod ugo-x debian/python-ball/usr/share/BALL-$(SOVERSION)/PYTHON/BALLView/*.py
	chmod ugo-x debian/python-ball/usr/share/BALL-$(SOVERSION)/PYTHON/howtos/*.py

	# -dev packages are arch dependent
	mkdir -p debian/libball$(SOVERSION)-dev/usr/lib \
		debian/libballview$(SOVERSION)-dev/usr/include/BALL \
		debian/libballview$(SOVERSION)-dev/usr/lib \
		debian/libball$(SOVERSION)-dev/usr/include
	# move the cmake configuration files to their final location
	cp -R $(BALL_INSTALL_PATH)/usr/lib/cmake/ debian/libball$(SOVERSION)-dev/usr/lib

	cp -r $(BALL_INSTALL_PATH)/usr/include/BALL       debian/libball$(SOVERSION)-dev/usr/include
	cp -r $(BALL_INSTALL_PATH)/usr/include/BALL/VIEW  debian/libballview$(SOVERSION)-dev/usr/include/BALL

	# get rid of the VIEW includes in libball-dev
	rm -rf debian/libball$(SOVERSION)-dev/usr/include/BALL/VIEW

	# eliminating chrpath settings
	chmod 755 debian/ballview/usr/bin/BALLView
	chrpath -d debian/ballview/usr/bin/BALLView
	find debian -name "*.so" | xargs -r chrpath -d 

override_dh_auto_install-indep:
	mkdir -p debian/libball$(SOVERSION)-doc/usr/share/doc/libball$(SOVERSION)/html \
		debian/libball$(SOVERSION)-data/usr/share/BALL-$(SOVERSION)/doc

	cp    $(BALL_INSTALL_PATH)/usr/share/BALL/doc/TUTORIAL/tutorial.pdf   debian/libball$(SOVERSION)-doc/usr/share/doc/libball$(SOVERSION)/
	cp -r $(BALL_INSTALL_PATH)/usr/share/BALL/doc/classes                 debian/libball$(SOVERSION)-data/usr/share/BALL-$(SOVERSION)/doc
	cp -r $(BALL_INSTALL_PATH)/usr/share/BALL/doc/internal                debian/libball$(SOVERSION)-data/usr/share/BALL-$(SOVERSION)/doc
	# we need the BALLView documentation in the data path as well... sorry for that
	cp -r $(BALL_INSTALL_PATH)/usr/share/BALL/doc/BALLView debian/libball$(SOVERSION)-data/usr/share/BALL-$(SOVERSION)/doc
	cp -r $(BALL_INSTALL_PATH)/usr/share/BALL/doc/BALL     debian/libball$(SOVERSION)-doc/usr/share/doc/libball$(SOVERSION)/html

	cp -r $(BALL_INSTALL_PATH)/usr/share/BALL/data    debian/libball$(SOVERSION)-data/usr/share/BALL-$(SOVERSION) 

override_dh_installdocs:
	dh_installdocs
	find debian/*/usr/share -type d -empty -delete
	find debian/*/usr/share -name jquery.js -delete
	find debian/*/usr/share -name license.txt -delete
