#!/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

# These are used for cross-compiling and for saving the configure script
# from having to guess our platform (since we know it already)
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      ?= $(shell dpkg-architecture -qDEB_BUILD_ARCH)

DEB_TEST_TARGET ?= Experimental

QGIS_VERSION=$(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p')

ifeq (,$(DISPLAY))
TESTMAKE=xvfb-run -a -n 1 -s "-screen 0 1280x1024x24 -dpi 96" $(MAKE)
else
TESTMAKE=$(MAKE)
endif


QGIS_MAJOR=$(shell sed -ne 's/SET(CPACK_PACKAGE_VERSION_MAJOR "\([0-9]*\)")/\1/p' CMakeLists.txt)
QGIS_MINOR=$(shell sed -ne 's/SET(CPACK_PACKAGE_VERSION_MINOR "\([0-9]*\)")/\1/p' CMakeLists.txt)
QGIS_PATCH=$(shell sed -ne 's/SET(CPACK_PACKAGE_VERSION_PATCH "\([0-9]*\)")/\1/p' CMakeLists.txt)
QGIS_ABI=$(QGIS_MAJOR).$(QGIS_MINOR).$(QGIS_PATCH)
GRASS_ABI=$(subst .,,$(shell pkg-config --modversion grass|sed -e "s/\.//g" -e "s/RC/-/"))
GRASS=grass$(subst .,,$(shell pkg-config --modversion grass|cut -d. -f1,2))

CMAKE_OPTS := \
	-DCMAKE_VERBOSE_MAKEFILE=1 \
	-DGRASS_PREFIX=/usr/lib/grass64 \
	-DBINDINGS_GLOBAL_INSTALL=TRUE \
	-DPEDANTIC=TRUE \
	-DWITH_MAPSERVER=TRUE \
	-DMAPSERVER_SKIP_ECW=TRUE \
	-DQGIS_CGIBIN_SUBDIR=/usr/lib/cgi-bin \
	-DWITH_APIDOC=TRUE \
	-DWITH_INTERNAL_SPATIALITE=FALSE \
	-DWITH_PYSPATIALITE=FALSE \
	-DWITH_QSPATIALITE=TRUE \
	-DWITH_GLOBE=TRUE \
	-DPYTHON_LIBRARY=/usr/lib/$(DEB_BUILD_MULTIARCH)/libpython2.7.so

ifneq (,$(findstring debug,$(DEB_BUILD_OPTIONS)))
	CMAKE_OPTS += -DCMAKE_BUILD_TYPE=Debug
endif

# Don't build tests, running them requires network
#ifneq (,$(filter nocheck,$(DEB_BUILD_OPTIONS)))
	CMAKE_OPTS += -DENABLE_TESTS=FALSE
#endif

DEB_BUILD_MULTIARCH ?= $(shell dpkg-architecture -qDEB_BUILD_MULTIARCH)
QT_PLUGIN_DIR = usr/lib/$(DEB_BUILD_MULTIARCH)/qt4/plugins

CFLAGS   += $(CPPFLAGS)
CXXFLAGS += $(CPPFLAGS)

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

ifneq (,$(findstring profile,$(DEB_BUILD_OPTIONS)))
	CFLAGS += -pg
	CXXFLAGS += -pg
	LDFLAGS += -pg
endif

%:
	dh $@ --with python2,pkgkde_symbolshelper --parallel

override_dh_clean:
	dh_clean

	# automatically generated files
	-$(RM) -f $(CURDIR)/src/core/qgscontexthelp_texts.cpp
	-$(RM) -f $(CURDIR)/src/core/qgsexpression_texts.cpp

	-$(RM) -rf build/

override_dh_auto_configure:
	dh_auto_configure -- $(CMAKE_OPTS)

override_dh_auto_test:
	# Don't run tests, requires network
	#-LD_LIBRARY_PATH=$(PWD)/build/output/lib:$(LD_LIBRARY_PATH) $(TESTMAKE) -C build $(DEB_TEST_TARGET)

override_dh_auto_install:
	dh_auto_install

	# remove unwanted files
	$(RM) -f $(CURDIR)/debian/tmp/usr/share/qgis/doc/api/installdox
	$(RM) -f $(CURDIR)/debian/tmp/usr/share/qgis/doc/api/jquery.js
	$(RM) -f $(CURDIR)/debian/tmp/usr/share/qgis/doc/LICENCE

	# remove extra license files
	-find $(CURDIR)/debian/tmp/usr/share/qgis/resources/cpt-city-qgis-min/ -name COPYING.xml -delete

	# Man pages are installed by dh_installman
	$(RM) -f $(CURDIR)/debian/tmp/usr/man/man1/qgis.1
	$(RM) -f $(CURDIR)/debian/tmp/usr/man/man1/qbrowser.1

	# Don't ship srs.db, automatically updated in postinst with crssync
	mv $(CURDIR)/debian/tmp/usr/share/qgis/resources/srs.db $(CURDIR)/debian/tmp/usr/share/qgis/resources/srs-template.db

	# Install menu pixmap
	install -o root -g root -d $(CURDIR)/debian/tmp/usr/share/pixmaps
	install -o root -g root -m 644 $(CURDIR)/images/icons/qgis_icon.svg $(CURDIR)/debian/tmp/usr/share/pixmaps/qgis.svg
	install -o root -g root -m 644 $(CURDIR)/images/icons/qgis-icon.png $(CURDIR)/debian/tmp/usr/share/pixmaps/qgis.png
	install -o root -g root -m 644 $(CURDIR)/images/icons/qgis-mime-icon.png $(CURDIR)/debian/tmp/usr/share/pixmaps/qgis-mime.png
	install -o root -g root -m 644 $(CURDIR)/debian/qgis-icon.xpm $(CURDIR)/debian/tmp/usr/share/pixmaps/qgis.xpm

	# Install application icon
	for size in 8x8 16x16 22x22 24x24 32x32 36x36 42x42 48x48 64x64 72x72 80x80 96x96 128x128 192x192 256x256 512x512; do \
		install -o root -g root -d $(CURDIR)/debian/tmp/usr/share/icons/hicolor/$${size}/apps ; \
		install -o root -g root -m 644 $(CURDIR)/debian/qgis-icon$${size}.png $(CURDIR)/debian/tmp/usr/share/icons/hicolor/$${size}/apps/qgis.png ; \
	done
	
	install -o root -g root -d $(CURDIR)/debian/tmp/usr/share/icons/scalable/apps
	install -o root -g root -m 644 $(CURDIR)/images/icons/qgis_icon.svg $(CURDIR)/debian/tmp/usr/share/icons/scalable/apps/qgis.svg

	# Install desktop files
	install -o root -g root -d $(CURDIR)/debian/tmp/usr/share/applications
	install -o root -g root -m 644 $(CURDIR)/debian/qgis.desktop $(CURDIR)/debian/tmp/usr/share/applications
	install -o root -g root -m 644 $(CURDIR)/debian/qbrowser.desktop $(CURDIR)/debian/tmp/usr/share/applications

	# Mime info
	install -o root -g root -d $(CURDIR)/debian/tmp/usr/share/mime/packages
	install -o root -g root -m 644 $(CURDIR)/debian/qgis.xml $(CURDIR)/debian/tmp/usr/share/mime/packages

	install -o root -g root -d $(CURDIR)/debian/tmp/usr/share/mimelnk/application
	install -o root -g root -m 644 $(CURDIR)/debian/mime/application/* $(CURDIR)/debian/tmp/usr/share/mimelnk/application

	# qgis binaries
	install -o root -g root -m 755 -d $(CURDIR)/debian/qgis/usr/bin
	install -o root -g root -m 755 $(CURDIR)/debian/tmp/usr/bin/qgis $(CURDIR)/debian/qgis/usr/bin
	install -o root -g root -m 755 $(CURDIR)/debian/tmp/usr/bin/qbrowser $(CURDIR)/debian/qgis/usr/bin

	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/qgis
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/qbrowser

	# qgis binary wrappers
	install -o root -g root -m 755 -d $(CURDIR)/debian/qgis-plugin-grass/usr/bin
	install -o root -g root -m 755 $(CURDIR)/debian/qgis.sh $(CURDIR)/debian/qgis-plugin-grass/usr/bin/qgis
	install -o root -g root -m 755 $(CURDIR)/debian/qgis.sh $(CURDIR)/debian/qgis-plugin-grass/usr/bin/qbrowser

override_dh_install:
	dh_install --autodest --list-missing

override_dh_installchangelogs:
	dh_installchangelogs ChangeLog

override_dh_installman:
	dh_installman -pqgis qgis.1 qbrowser.1

override_dh_installmime:
	dh_installmime -pqgis

override_dh_python2:
	dh_python2 --no-guessing-versions
	dh_sip

override_dh_compress:
	dh_compress --exclude=pdf

override_dh_makeshlibs:
	dh_makeshlibs -Xqgis-plugin-grass -- -c0 -v$(QGIS_VERSION)

override_dh_shlibdeps:
	dh_shlibdeps -l/usr/lib/$(GRASS)/lib

