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

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

PLUGINS := $(shell find $(CURDIR) -mindepth 1 -maxdepth 1 -name .git -prune -o -name .pc -prune -o -name debian -prune -o -type d -printf '%f\n' | sort)
PKGNAME = monitoring-plugins-cyconet

%:
	dh $@ --with quilt,python2,autotools_dev --parallel

# 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/python $(CURDIR)/debian/packaging-helper.py


clean: $(PLUGINS:%=clean-%) debian/copyright debian/control
	dh $@ --with quilt,python2,autotools_dev --parallel
	rm -f debian/$(PKGNAME).install
	rm -f debian/README.Debian.plugins

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/$(PKGNAME) ]; then \
	    mkdir -p debian/$*/usr/lib/nagios ;\
	    mv debian/$*/usr/lib/$(PKGNAME) debian/$*/usr/lib/nagios/plugins ;\
	fi
	# add files to debian/$(PKGNAME).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/$(PKGNAME).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
	# remove CR from plugins
	set -e; \
	for fix in `grep -lIUr '
$$' $(CURDIR)/debian/$*/usr/lib/monitoring-plugins | xargs`; do \
	   if [ -n "$$fix" ]; then \
		sed -i 's/
$$//' $$fix ;\
	   fi ;\
	done

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_python2:
	dh_python2
	dh_python2 usr/lib/nagios/plugins usr/lib/nagios/cronjobs

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
