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

# Uncomment this to turn on verbose mode.
export DH_VERBOSE=1

GIMPVER := $(shell dpkg -s libgimp2.0-dev | grep Version | awk '{print $$2}')
export GIMPVER
ALL_PLUGINS := $(shell find $(CURDIR) -mindepth 1 -maxdepth 1 -name .git -prune -o -name .pc -prune -o -name debian -prune -o -name old_plugins -prune -o -type d -printf '%f\n' | sort)
export ALL_PLUGINS
DISABLE_PLUGINS := btn4ws cmyk-tiff-2-pdf contact-sheet ez-perspective layer-effects openraster resynthesizer null
PLUGINS := $(shell for i in $(ALL_PLUGINS); do A=1; for j in $(DISABLE_PLUGINS); do if [ x$$i = x$$j ]; then A=0; fi; done; if [ $$A -ne 0 ]; then echo $$i; fi; done)
export PLUGINS
PLUGINBINDIR := "/usr/lib/gimp/2.0/plug-ins"
export PLUGINBINDIR
PLUGINSCRIPTDIR := "/usr/share/gimp/2.0/scripts"
export PLUGINSCRIPTDIR

%:
	dh $@ --with quilt,python3

# Here follows a small shell snipped to call dh_auto_* for all plugins
# Currently
# - if a Makefile exists in the plugin directory
#   we run dh_auto_$(1) with -O--sourcedirectory="$$plugin"
# - if $${plugin}/src exists, we run dh_auto_$(1) on that directory
# - else: fail :)
DH_AUTO_CALL = 	if [ "$$auto_command" = "dh_auto_configure" ]; then \
		    export options="$$options -- --enable-stack-protector" ;\
		fi ;\
		if [ -f $(CURDIR)/$$plugin/Makefile ]; then \
			$$auto_command -O--sourcedirectory="$${plugin}" $$options; \
		elif [ -d $(CURDIR)/$$plugin/src ]; then \
			$$auto_command -O--sourcedirectory="$${plugin}/src" $$options; \
		else \
			echo failed to build $$plugin; exit 255 ; \
		fi

PACKAGING_HELPER = /usr/bin/python3 $(CURDIR)/debian/packaging-helper.py


clean: $(ALL_PLUGINS:%=clean-%) debian/copyright debian/control
	dh $@ --with quilt,python3
	rm -f debian/gimp-plugin-registry.install

clean-%:
	rm -rf debian/$*

dh_auto_install-%:
	# run dh_auto_install to debian/$plugin
	set -e ;\
	    export auto_command="dh_auto_install" ;\
	    export plugin="$*" ;\
	    export options="--destdir=debian/$*" ;\
	    $(DH_AUTO_CALL)
	if [ -d debian/$*/usr/lib/gimp-plugin-registry ]; then \
	    mkdir -p debian/$*/usr/lib/gimp/2.0 ;\
	    mv debian/$*/usr/lib/gimp-plugin-registry debian/$*/usr/lib/gimp/2.0/plug-ins ;\
	fi
	# add files to debian/gimp-plugin-registry.install
	set -e; find debian/$* -type f -printf '%h\n' | sort -u  |\
	    while read dir; do \
	        echo "$$dir/* `echo $$dir | sed 's,debian/$*/,,'`" ;\
	    done >> debian/gimp-plugin-registry.install
	# create shlibdeps for the plugin into a temp file
	set -e; \
	   opts=`find debian/$* -type f -exec file {} \; |\
	   grep -E ' ELF ' |\
	   sed 's,:.*,,;s,^,-e,' |\
	   tr '\n' ' '` ;\
	   if [ -n "$$opts" ]; then \
	   	dpkg-shlibdeps -O $$opts | grep shlibs:Depends > debian/$*/substvars ;\
	   fi

dh_auto_%:
	set -e; \
	    export auto_command=`echo $* | sed 's,-.*,,;s,^,dh_auto_,'` ;\
	    export plugin=`echo $* | sed 's,[^-]*-,,'` ;\
	    $(DH_AUTO_CALL)


override_dh_auto_build: $(PLUGINS:%=dh_auto_build-%)

override_dh_auto_clean: $(PLUGINS:%=dh_auto_clean-%)

override_dh_auto_configure: $(PLUGINS:%=dh_auto_configure-%)

override_dh_auto_install: $(PLUGINS:%=dh_auto_install-%)
	#$(PACKAGING_HELPER) --generate-readme

#override_dh_auto_test: $(PLUGINS:%=dh_auto_test-%)
override_dh_auto_test:
	# nothign to do right now.

override_dh_python3:
	dh_python3
	dh_python3 usr/lib/gimp/2.0/python

CONTROL_FILES := $(shell for p in $(PLUGINS); do echo $$p/control; done)
COPYRIGHT_FILES := $(shell for p in $(PLUGINS); do echo $$p/copyright; done)
debian/copyright: debian/copyright.in debian/packaging-helper.py $(CONTROL_FILES) $(COPYRIGHT_FILES)
	$(PACKAGING_HELPER) --copyright
	-if [ -d .git ]; then git add $@; git commit -m 'Auto update of $@' $@; fi

debian/control: debian/control.in debian/packaging-helper.py $(CONTROL_FILES)
	$(PACKAGING_HELPER) --control
	-if [ -d .git ]; then git add $@; git commit -m 'Auto update of $@' $@; fi


watch:
	@$(PACKAGING_HELPER) --watch

.PHONY: watch override_dh_auto_build override_dh_auto_clean override_dh_auto_configure override_dh_auto_install override_dh_auto_test
