#!/usr/bin/make -f

# 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

DEB_HOST_MULTIARCH ?= $(shell dpkg-architecture -qDEB_HOST_MULTIARCH)

# CMake doesn't use hardening build flags set in CPPFLAGS
CFLAGS   += $(CPPFLAGS)
CXXFLAGS += $(CPPFLAGS)

PACKAGE := $(shell dpkg-parsechangelog | sed -n 's/^Source: //p')
VERSION := $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p' | sed -e 's/\+.*//; s/^[0-9]://')
BUILD_DATE := $(shell dpkg-parsechangelog | sed -n 's/^Date: //p' | LC_ALL=C date -u "+%d %B %Y" -f -)

MANPAGES:=$(wildcard debian/man/*.*.xml)

%:
	dh $@ --with pkgkde_symbolshelper \
	      --parallel

override_dh_clean:
	dh_clean debian/man/*.1
	-$(RM) -rf build/

override_dh_auto_configure:
	dh_auto_configure --builddirectory=build -- \
		-DCMAKE_VERBOSE_MAKEFILE=1 \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DLIB_POSTFIX="" \
		-DDEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH)

override_dh_auto_build:
	# Create man pages from DocBook XML
	for x in $(MANPAGES) ; do \
	  docbook2x-man --string-param header-3="$(BUILD_DATE)" $$x ; \
	  mv `basename $$x | sed 's/.xml$$//'` `dirname $$x` ; \
	done

	dh_auto_build --builddirectory=build

override_dh_auto_install:
	dh_auto_install --builddirectory=build

	mkdir -p debian/osgearth-data/usr/share/osgearth/maps && \
	  cp tests/*.earth debian/osgearth-data/usr/share/osgearth/maps/ && \
	  cp debian/*.earth debian/osgearth-data/usr/share/osgearth/maps/ && \
	  sed --in-place -e 's!\\!/!g' -e 's!\.\./data!/usr/share/osgearth/data!g' debian/osgearth-data/usr/share/osgearth/maps/*.earth

	mkdir -p debian/osgearth-data/usr/share/osgearth/data && \
	  cp data/world.tif debian/osgearth-data/usr/share/osgearth/data/ && \
	  cp -r data/resources/ debian/osgearth-data/usr/share/osgearth/data/

	# Don't install binaries which depend on removed data files
	$(RM) debian/*/usr/bin/osgearth_annotation
	$(RM) debian/*/usr/bin/osgearth_city
	$(RM) debian/*/usr/bin/osgearth_clamp
	$(RM) debian/*/usr/bin/osgearth_featureeditor
	$(RM) debian/*/usr/bin/osgearth_features
	$(RM) debian/*/usr/bin/osgearth_imageoverlay
	$(RM) debian/*/usr/bin/osgearth_los
	$(RM) debian/*/usr/bin/osgearth_minimap
	$(RM) debian/*/usr/bin/osgearth_occlusionculling
	$(RM) debian/*/usr/bin/osgearth_qt
	$(RM) debian/*/usr/bin/osgearth_tracks

	# Don't install example binaries
	$(RM) debian/*/usr/bin/osgearth_colorfilter
	$(RM) debian/*/usr/bin/osgearth_contour
	$(RM) debian/*/usr/bin/osgearth_controls
	$(RM) debian/*/usr/bin/osgearth_elevation
	$(RM) debian/*/usr/bin/osgearth_featurefilter
	$(RM) debian/*/usr/bin/osgearth_featureinfo
	$(RM) debian/*/usr/bin/osgearth_featuremanip
	$(RM) debian/*/usr/bin/osgearth_featurequery
	$(RM) debian/*/usr/bin/osgearth_graticule
	$(RM) debian/*/usr/bin/osgearth_manip
	$(RM) debian/*/usr/bin/osgearth_map
	$(RM) debian/*/usr/bin/osgearth_measure
	$(RM) debian/*/usr/bin/osgearth_package_qt
	$(RM) debian/*/usr/bin/osgearth_qt_simple
	$(RM) debian/*/usr/bin/osgearth_qt_windows
	$(RM) debian/*/usr/bin/osgearth_sequencecontrol
	$(RM) debian/*/usr/bin/osgearth_shadercomp
	$(RM) debian/*/usr/bin/osgearth_shadow
	$(RM) debian/*/usr/bin/osgearth_terrainprofile
	$(RM) debian/*/usr/bin/osgearth_tilesource
	$(RM) debian/*/usr/bin/osgearth_toc
	$(RM) debian/*/usr/bin/osgearth_verticalscale

override_dh_install:
	dh_install --autodest --list-missing

override_dh_shlibdeps:
	LD_LIBRARY_PATH=$(LD_LIBRARY_PATH):build/lib dh_shlibdeps

override_dh_makeshlibs:
	dh_makeshlibs -- -c0 -v$(VERSION)

get-orig-source:
	. debian/get-orig-source

