#!/usr/bin/make -f
# -*- makefile -*-
# Sample debian/rules that uses debhelper.
#
# This file was originally written by Joey Hess and Craig Small.
# As a special exception, when this file is copied by dh-make into a
# dh-make output file, you may use that output file without restriction.
# This special exception was added by Craig Small in version 0.37 of dh-make.
#
# Modified to make a template file for a multi-binary package with separated
# build-arch and build-indep targets  by Bill Allombert 2001

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

CPPFLAGS:=$(shell dpkg-buildflags --get CPPFLAGS)
CFLAGS:=$(shell dpkg-buildflags --get CFLAGS)
LDFLAGS:=$(shell dpkg-buildflags --get LDFLAGS)

CFLAGS+=$(CPPFLAGS)
CFLAGS+=$(LDFLAGS)

MAPCACHE_VERSION = $(shell dpkg-parsechangelog | sed -ne 's/^Version: \(.*\)-.*/\1/p' | sed -e 's/^\(.*\)~.*/\1/' | sed -e 's/dev.*/.0/')

MAKE_VERBOSE=1

COMMON_CONFIG = -DCMAKE_INSTALL_PREFIX=/usr \
		-DWITH_PIXMAN=1 \
		-DWITH_SQLITE=1 \
		-DWITH_BERKELEY_DB=0 \
		-DWITH_MEMCACHE=0 \
		-DWITH_TIFF=1 \
		-DWITH_TIFF_WRITE_SUPPORT=0 \
		-DWITH_GEOTIFF=0 \
		-DWITH_MAPSERVER=1 \
		-DWITH_PCRE=1 \
		-DWITH_APACHE=1 \
		-DWITH_VERSION_STRING=1 \
		-DWITH_CGI=1 \
		-DWITH_FCGI=1 \
		-DWITH_GEOS=1 \
		-DWITH_OGR=1

%:
	dh $@ --with apache2,pkgkde_symbolshelper

override_dh_clean:
	dh_clean

	-$(RM) -rf build/
	-$(RM) nginx/config

override_dh_auto_configure:
	mkdir -p build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Release $(COMMON_CONFIG)

override_dh_auto_build:
	# Create man page from DocBook XML
	-docbook2x-man $(CURDIR)/debian/mapcache_seed.1.xml
	-mv mapcache_seed.1 $(CURDIR)/debian/

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

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

	-mkdir -p $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/
	-mv -v $(CURDIR)/debian/tmp/usr/lib/libmapcache*.so* $(CURDIR)/debian/tmp/usr/lib/$(DEB_HOST_MULTIARCH)/

	mkdir -p $(CURDIR)/debian/tmp/usr/lib/cgi-bin/
	mv $(CURDIR)/debian/tmp/usr/bin/mapcache.fcgi $(CURDIR)/debian/tmp/usr/lib/cgi-bin/mapcache

	# Strip RPATH
	-find $(CURDIR)/debian/tmp/usr/bin -type f -exec chrpath --delete {} \;
	-find $(CURDIR)/debian/tmp/usr/lib -name "*.so*" -type f -exec chrpath --delete {} \;
	-find $(CURDIR)/build/apache -name "*.so*" -type f -exec chrpath --delete {} \;

	# libmapcache-dev headers
	-mkdir -p $(CURDIR)/debian/tmp/usr/include/mapcache/
	install -m 644 $(CURDIR)/include/*.h $(CURDIR)/debian/tmp/usr/include/mapcache/
	install -m 644 $(CURDIR)/build/*/*.h $(CURDIR)/debian/tmp/usr/include/mapcache/

	# Install apache module
	install -d $(CURDIR)/debian/tmp/etc/apache2/mods-available
	install -m 644 $(CURDIR)/debian/mapcache.load $(CURDIR)/debian/tmp/etc/apache2/mods-available

override_dh_install:
	dh_install --autodest --list-missing

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

