#!/usr/bin/make -f
# -*- makefile -*-
#
# 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

BUILD_DIR=build-dir

ifeq ($(wildcard /usr/share/dpkg/buildflags.mk),)
CFLAGS := -Wall -g
LDFLAGS := -Wl,--as-needed -Wl,-z,defs
ifneq (,$(findstring noopt,$(DEB_BUILD_OPTIONS)))
CFLAGS += -O0
else
CFLAGS += -O2
endif
else
DPKG_EXPORT_BUILDFLAGS := 1
include /usr/share/dpkg/buildflags.mk
# append -Wl,--as-needed and -Wl,-z,defs to LDFLAGS if missing
comma := ,
ifeq (,$(findstring -Wl$(comma)--as-needed,$(LDFLAGS)))
LDFLAGS += -Wl,--as-needed
endif
ifeq (,$(findstring -Wl$(comma)-z$(comma)defs,$(LDFLAGS)))
LDFLAGS += -Wl,-z,defs
endif
# the same for -Wall and CFLAGS
ifeq (,$(findstring -Wall,$(CFLAGS)))
CFLAGS += -Wall
endif
# append CPPFLAGS to CFLAGS for hardening supprt. #653916
CFLAGS += $(CPPFLAGS)
CXXFLAGS += $(CPPFLAGS)
export CFLAGS
export CXXFLAGS
export LDLAGS
endif

# Default python version.
PYTHONDEFVERSION := $(shell pyversions -d | sed -e 's/python\([0-9.]\+\)/\1/')

CMAKE_LDFLAGS=-DCMAKE_EXE_LINKER_FLAGS="$(LDFLAGS)" -DCMAKE_MODULE_LINKER_FLAGS="$(LDFLAGS)" -DCMAKE_SHARED_LINKER_FLAGS="$(LDFLAGS)" -DBUILD_HSI:BOOL=ON -DPythonLibs_FIND_VERSION=$(PYTHONDEFVERSION)

# Upstream version can be overriden so as to fetch newer revisions
# without having to modify the top-most changelog entry
UVERSION  ?= $(shell dpkg-parsechangelog | perl -ne 'print "$$1\n" if (/^Version: (.*?)(?:\+dfsg)?\-.*?$$/)')

# Where to store which manpages are generated (upstream keeps some manpages
# both under .pod and .1 formats).
GENERATED_MANPAGES=debian/generated-manpages

zthread-dep := $(shell \
	dpkg-query -f='$${source:Package} (= $${source:Version})' \
	-W libzthread-dev )

configure-stamp:
	dh_testdir
	# Configure the package.
	mkdir $(BUILD_DIR)
	cd $(BUILD_DIR) && cmake -DCMAKE_INSTALL_PREFIX=/usr $(CMAKE_LDFLAGS) ..
	touch $@


build: build-arch build-indep

build-arch:  build-arch-stamp
build-indep: build-indep-stamp

build-arch-stamp: configure-stamp
	# Compile the package.
	$(MAKE) -C $(BUILD_DIR) VERBOSE=1
	touch $@

build-indep-stamp: configure-stamp
	# Build manpages.
	for i in doc/*.pod ; do \
		if [ ! -f $${i%%.pod}.1 ] ; then \
			pod2man --center "HUGIN TOOLS" \
				--release $(UVERSION) \
				$$i -o $${i%%.pod}.1 ; \
			echo $${i%%.pod}.1 >> $(GENERATED_MANPAGES) ; \
		fi ; \
	done
	touch $@

clean:
	dh_testdir
	dh_testroot
	rm -f build-indep-stamp build-arch-stamp install-arch-stamp \
		install-indep-stamp configure-stamp src/celeste/Config.h

	# Clean generated manpages.
	if [ -f $(GENERATED_MANPAGES) ] ; then \
		for i in `cat $(GENERATED_MANPAGES)` ; do \
			rm -f $$i ; \
		done ; \
	fi
	rm -f $(GENERATED_MANPAGES)

	dh_clean

install: install-indep install-arch

install-indep: install-indep-stamp
install-arch: install-arch-stamp

install-indep-stamp: build-indep-stamp install-arch-stamp
	dh_testdir
	dh_testroot
	dh_installdirs -i

	dh_install --sourcedir=debian/tmp -i
	touch $@

install-arch-stamp: build-arch-stamp
	dh_testdir
	dh_testroot
	dh_prep
	dh_installdirs -a

	# Install the package into debian/tmp.
	$(MAKE) install -C $(BUILD_DIR) DESTDIR=$(CURDIR)/debian/tmp

	# Get rid of extra license file
	rm debian/tmp/usr/share/hugin/xrc/data/help_en_EN/LICENCE.manual

	dh_install --sourcedir=debian/tmp -a
	touch $@

binary-common:
	dh_testdir
	dh_testroot
	dh_installchangelogs ChangeLog
	dh_installdocs
	dh_installexamples
	dh_installmenu
	dh_installman
	dh_python2 -i /usr/share/hugin/data/plugins
	dh_python2 -a --no-guessing-versions
	dh_link
	dh_strip
	dh_compress
	dh_fixperms
	dh_makeshlibs --noscripts
	dh_installdeb
	dh_shlibdeps
	dh_gencontrol -- -Vlibzthread-dev:Built-Using='$(zthread-dep)'
	dh_md5sums
	dh_builddeb

# Build architecture independant packages using the common target.
binary-indep: build-indep install-indep
	$(MAKE) -f debian/rules DH_OPTIONS=-i binary-common

# Build architecture dependant packages using the common target.
binary-arch: build-arch install-arch
	$(MAKE) -f debian/rules DH_OPTIONS=-a binary-common

get-orig-source:
	rm -rf  $(CURDIR)/debian/get-orig-source
	mkdir $(CURDIR)/debian/get-orig-source
	uscan --force-download --rename --download-version ${UVERSION} \
		--destdir $(CURDIR)/debian/get-orig-source
	cd $(CURDIR)/debian/get-orig-source && \
		if test -e hugin_${UVERSION}.orig.tar.gz ; then \
		tar xzf hugin_${UVERSION}.orig.tar.gz ; else \
		tar xjf hugin_${UVERSION}.orig.tar.bz2 ; fi && \
		cd hugin-* && \
		rm -rvf platforms/windows/ mac/*  \
		src/foreign/getopt/src/msvs/ platforms/mac/* \
		platforms/CMakeLists.txt &&\
		touch platforms/CMakeLists.txt && \
		find -name '*.vcproj' -delete && \
		cd .. && \
		tar -cJf $(CURDIR)/../hugin_$(UVERSION)+dfsg.orig.tar.xz hugin-*
	rm -rf  $(CURDIR)/debian/get-orig-source


binary: binary-arch binary-indep
.PHONY: build clean binary-indep binary-arch binary install install-indep install-arch get-orig-source
