#!/usr/bin/make -f

SHELL := sh -e

D := $(CURDIR)/debian/suckless-tools
TOOLS := $(filter-out debian, $(wildcard *))
ALTERNATIVES := $(wildcard debian/local/*)
RULE_FILE_DIR := $(dir $(firstword $(MAKEFILE_LIST)))

%:
	dh $@

override_dh_auto_configure:
	for ALTERNATIVE in $(ALTERNATIVES); \
	do \
		tool=`basename $${ALTERNATIVE} | perl -pe "s/.*\K\..*//g"`;\
		extn=`basename $${ALTERNATIVE} | perl -pe "s/.*\.//g"`; \
		newfork=$${tool}.$${extn}; \
		cp -r $${tool} $${newfork}; \
		patch -p1 < $${ALTERNATIVE} -d $${newfork}; \
		TOOLS="$(TOOLS) $${newfork}"; \
	done
	dh_auto_configure

override_dh_auto_clean:
	dh_auto_clean
	for TOOL in $(TOOLS); \
	do \
		make -C $${TOOL} clean; \
	done
	@diff -q tabbed/config.h tabbed/config.def.h && rm -f tabbed/config.h || true

override_dh_auto_build:
	for TOOL in $(TOOLS);\
	do \
		make -C $${TOOL}; \
	done

override_dh_auto_install:
	for TOOL in $(TOOLS); \
	do \
		$(MAKE) -C $${TOOL} DESTDIR=$(D) PREFIX=/usr install; \
	done

override_dh_installdocs:
	dh_installdocs
	for TOOL in $(TOOLS); \
	do \
		cp $${TOOL}/README $(D)/usr/share/doc/suckless-tools/README.$${TOOL}; \
	done

override_dh_fixperms:
	dh_fixperms -Xusr/bin/slock

get-orig-source:
	$(RULE_FILE_DIR)create_orig_source $(RULE_FILE_DIR)


