#!/usr/bin/make -f
# -*- makefile -*-

export DEB_BUILD_MAINT_OPTIONS=hardening=+bindnow


# grab the version from the debian changelog
VERSION := $(shell sed -n 's/.*(\([0-9\.]*[0-9]\).*).*/\1/;; p; q;' debian/changelog)

# grab the API version from the library SONAME
API_VERSION = $(shell objdump -p bin/*/libvl.so | perl -ne 'if(/^\s+SONAME\s+libvl.so./p) {print $${^POSTMATCH}; exit;}')

IMAGEMAGICK_POLICY := /etc/$(shell convert -version|sed -n '/^Version: /s@Version: ImageMagick \([[:digit:]]\+\)\..*@ImageMagick-\1@p')/policy.xml

%:
	dh $@

override_dh_auto_build:
	mkdir -p debian/tmp/ImageMagick
	sed -e '/<policy domain="coder" rights="none" pattern="PDF" .>/s@"none"@"read|write"@' "$(IMAGEMAGICK_POLICY)" > debian/tmp/ImageMagick/policy.xml
	make XDG_CONFIG_HOME="$(shell pwd)/debian/tmp" PYTHON=python3 MKOCTFILE=`which mkoctfile` VERB=1 CFLAGS+=-g all doc
	rm -Rf debian/tmp/ImageMagick

override_dh_auto_install: $(addprefix install/,data $(wildcard toolbox/*))
	cp bin/*/libvl.so libvl.so.$(VERSION)
	ln -fs libvl.so.$(VERSION) libvl.so
	ln -fs libvl.so.$(VERSION) libvl.so.$(API_VERSION)

override_dh_gencontrol:
	dh_octave_substvar
	dh_gencontrol

override_dh_strip: strip_mex
	dh_strip --dbgsym-migration='libvlfeat$(API_VERSION)-dbg (<< 0.9.21+dfsg0-1~)'

override_dh_installdocs:
	dh_installdocs -Xbuild/

override_dh_auto_clean:
	dh_auto_clean
	rm -f VERSION libvl.so* *.o
	rm -rf toolbox/noprefix/ toolbox/mex/



# The rest is all for octave. There's some CDBS infrastructure to automate this,
# but it's designed for octave-only source packages. I have both in one source,
# so I can't use it

include /usr/share/octave/debian/defs.make


# I want to take all the *.m in toolbox/xxx/ for all xxx except mex and doc and
# noprefix. I want to take only some particular toolbox/*.m. There are many
# other files strewn around that I want to ignore. This is somewhat
# convoluted...

OCTAVE_MPATH   := debian/octave-vlfeat$(MDIR)/vlfeat/toolbox
OCTAVE_BINPATH := debian/octave-vlfeat$(OCTDIR)/vlfeat/toolbox
OCTAVE_PHONY_TARGETS := $(addprefix install/,data $(wildcard toolbox/*) toolbox/mex toolbox/noprefix)
.PHONY: $(OCTAVE_PHONY_TARGETS)

OCT_TARGET=$(@:install/%=%)

$(addprefix install/toolbox/, vl_harris.m vl_help.m vl_root.m):
	install -d $(OCTAVE_MPATH)
	install -m 0644 $(OCT_TARGET) $(OCTAVE_MPATH)

# installing the .mex files with executable bits set to let dh_shlibdeps index
# them. I remove these bits as soon as dh_shlibdeps does its thing
install/toolbox/mex:
	install -d $(OCTAVE_BINPATH)
	install -m 0755 $(OCT_TARGET)/*/*/*.mex $(OCTAVE_BINPATH)

override_dh_shlibdeps:
	dh_shlibdeps
	chmod 0644 $(OCTAVE_BINPATH)/*.mex

override_dh_compress:
	dh_compress -X.m

$(filter-out %.m install/data install/toolbox/mex install/toolbox/doc install/toolbox/noprefix,$(OCTAVE_PHONY_TARGETS)):
	if [ -d $(OCT_TARGET) ]; then							\
		install -d $(OCTAVE_MPATH);						\
		install -d $(OCTAVE_MPATH)/$(notdir $(OCT_TARGET));			\
		install -m 0644 $(OCT_TARGET)/*.m $(OCTAVE_MPATH)/$(notdir $(OCT_TARGET));	\
	fi

install/data:
	install -d $(OCTAVE_MPATH)/../data
	install -m 0644 data/* $(OCTAVE_MPATH)/../data

# dh_strip doesn't see the .mex files as needing stripping, so I do it myself
.PHONY: strip_mex
strip_mex:
	strip --remove-section=.comment --remove-section=.note --strip-unneeded $(OCTAVE_BINPATH)/*.mex