#!/usr/bin/make -f

DEB_AUTO_UPDATE_LIBTOOL = pre
DEB_AUTO_UPDATE_AUTOHEADER = 2.68
DEB_AUTO_UPDATE_AUTOCONF = 2.65
DEB_AUTO_UPDATE_ACLOCAL = 1.11
DEB_AUTO_UPDATE_AUTOMAKE = 1.11

-include /usr/share/cdbs/1/rules/upstream-tarball.mk
include /usr/share/cdbs/1/rules/buildcore.mk
include /usr/share/cdbs/1/rules/debhelper.mk
include /usr/share/cdbs/1/class/autotools.mk

pkg = $(DEB_SOURCE_PACKAGE)
pkgdoc = frei0r-plugins-doc
abi = 1.2

experimental := $(shell dpkg-parsechangelog | grep -x 'Distribution: \(experimental\|UNRELEASED\)')

CDBS_BUILD_DEPENDS += , pkg-config, libgavl-dev
CDBS_BUILD_DEPENDS += , libopencv-dev $(if $(experimental),(>= 2.4))

CDBS_PROVIDES_frei0r-plugins = frei0r-plugins-$(abi)

DEB_UPSTREAM_URL = http://www.piksel.no/$(pkg)/releases
DEB_UPSTREAM_PACKAGE = frei0r-plugins
DEB_UPSTREAM_TARBALL_MD5 = a2eb63feeeb0c5cf439ccca276cbf70c

# TODO: have (defaults for) these provided by CDBS
libtoolpaths = ltmain.sh
libtoolm4files = libtool.m4 ltoptions.m4 ltversion.m4 lt~obsolete.m4
autoheaderpaths = include/config.h.in
autoconfpaths = aclocal.m4 configure
m4files = $(libtoolm4files)
m4paths = $(addprefix m4/,$(m4stems))
aclocalpaths = config.guess config.sub depcomp install-sh missing
automakedirs = . doc include src
automakepaths = $(addsuffix /Makefile.in,$(automakedirs))
autotoolspaths = $(automakepaths) $(aclocalpaths) $(autoconfpaths)
autotoolspaths += $(m4paths) $(autoheaderpaths) $(libtoolpaths)

# put aside autogenerated files during build
# TODO: use DEB_UPSTREAM_CRUFT_MOVE when cdbs 0.4.106 is in stable
upstreamtmpstuff = $(libtoolm4paths)
pre-build:: debian/upstream-stuff
#debian/upstream-stuff: debian/stamp-copyright-check
debian/upstream-stuff:
	mkdir -p debian/upstream-stuff
	@for orig in $(upstreamtmpstuff); do \
		backup="debian/upstream-stuff/$$orig"; \
		[ ! -e "$$orig" ] || [ -e "$$backup" ] || { \
			mkdir -p "$$(dirname "$$backup")"; \
			echo mv "$$orig" "$$backup"; \
			mv "$$orig" "$$backup"; \
		}; \
	done
clean::
	@for orig in $(upstreamtmpstuff); do \
		backup="debian/upstream-stuff/$$orig"; \
		if [ -e "$$backup" ]; then \
			if [ -e "$$orig" ]; then \
				echo "rm -rf" "$$orig"; \
				rm -rf "$$orig"; \
			fi; \
			echo mv "$$backup" "$$orig"; \
			mv "$$backup" "$$orig"; \
		fi; \
	done
	rm -rf debian/upstream-stuff

# bootstrap autotools files (CDBS by default only update, not create)
DEB_MAKE_CLEAN_TARGET = distclean
DEB_AUTOMAKE_ARGS = --add-missing --copy --foreign
makefile-clean::
	rm -f $(filter-out $(upstreamtmpstuff),$(autotoolspaths))

# TODO: have autotools build docs as part of main build, when doxygen is found
DEB_BUILDDIR_$(pkgdoc) = doc
build/$(pkgdoc)::
	$(DEB_MAKE_INVOKE)
# TODO: have autotools support DESTDIR for doc install target
#install/$(pkgdoc)::
#	$(DEB_MAKE_INVOKE) $(DEB_MAKE_INSTALL_TARGET)
DEB_DH_INSTALL_ARGS_$(pkgdoc) = doc/html usr/share/$(pkg)
# TODO: have autotools support DESTDIR for doc clean target
#DEB_PHONY_RULES += doc-clean/$(pkgdoc)
#doc-clean/$(pkgdoc):
#	$(DEB_MAKE_INVOKE) distclean
#clean:: doc-clean/$(pkgdoc)
clean::
	rm -f $(patsubst %,doc/html/*.%,html js css png map md5)
	rm -f doc/Makefile
