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

ifneq (,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
	NJOBS := -j $(subst parallel=,,$(filter parallel=%,$(subst $(COMMA), ,$(DEB_BUILD_OPTIONS))))
endif

# 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/\+.*//')

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

MAKE_VERBOSE=1

%:
	dh $@ --with pkgkde_symbolshelper

override_dh_clean:
	dh_clean

	-$(RM) -f $(CURDIR)/debian/man/*.1
	-$(RM) -rf build/

override_dh_auto_configure:
	mkdir -p build && cd build && cmake .. -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 $$x ; \
	  mv `basename $$x | sed 's/.xml$$//'` `dirname $$x` ; \
	done

	$(MAKE) $(NJOBS) -C build VERBOSE=$(MAKE_VERBOSE)

override_dh_auto_install:
	$(MAKE) -C build install DESTDIR=$(CURDIR)/debian/tmp VERBOSE=$(MAKE_VERBOSE)

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

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

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

	# Don't install example binaries
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_colorfilter
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_contour
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_controls
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_elevation
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_featurefilter
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_featureinfo
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_featuremanip
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_featurequery
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_graticule
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_manip
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_map
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_measure
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_package_qt
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_qt_simple
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_qt_windows
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_sequencecontrol
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_shadercomp
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_shadow
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_terrainprofile
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_tilesource
	$(RM) -f $(CURDIR)/debian/tmp/usr/bin/osgearth_toc
	$(RM) -f $(CURDIR)/debian/tmp/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

