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

# Enable hardening build flags
export DEB_BUILD_MAINT_OPTIONS=hardening=+all

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

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

include /usr/share/dpkg/pkg-info.mk

UPSTREAM_VERSION := $(shell echo $(DEB_VERSION_UPSTREAM) | sed -e 's/\+.*//')

BUILD_DATE := $(shell LC_ALL=C date -u "+%d %B %Y" -d "@$(SOURCE_DATE_EPOCH)")

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 -- \
		-DBUILD_OSGEARTH_EXAMPLES=0 \
		-DCMAKE_VERBOSE_MAKEFILE=1 \
		-DCMAKE_BUILD_TYPE=Release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DDEB_HOST_MULTIARCH=$(DEB_HOST_MULTIARCH) \
		-DLIB_POSTFIX="" \
		-DOSG_DIR=/usr \
		-DOSGEARTH_QT_BUILD=1

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_3pv
	$(RM) debian/*/usr/bin/osgEarth_tests

override_dh_install:
	dh_install --autodest --list-missing

override_dh_installchangelogs:
	dh_installchangelogs docs/source/releasenotes.rst

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

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

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

