#!/usr/bin/make -f

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

# Exit immediately if any untested command fails
SHELL := sh -e

# Path of 'debian' directory
DEB_DIR := $(abspath $(dir $(MAKEFILE_LIST)))

# Provide the correct pkg-config for cross-building
include /usr/share/dpkg/architecture.mk
export PKG_CONFIG := $(DEB_HOST_GNU_TYPE)-pkg-config

# Enable all hardening flags
export DEB_BUILD_MAINT_OPTIONS = hardening=+all

# The upstream Makefiles are written in a way that suppresses the recipe
# echoing. This means that tools like 'blhc' cannot inspect the build logs.
# Instead of maintaining a patch that will remove the preceding '@' symbol
# from all recipes, we pass the '--trace' flag to make, which prints the
# entire recipe to be executed, even for recipes that are normally silent
MAKEFLAGS += --trace

D := $(DEB_DIR)/suckless-tools
TOOLS := $(filter-out debian, $(wildcard *))


%:
	dh $@

.PHONY: override_dh_auto_clean
override_dh_auto_clean:
	for TOOL in $(TOOLS); do \
		dh_auto_clean --sourcedirectory=$${TOOL}; \
	done

.PHONY: override_dh_auto_build
override_dh_auto_build:
	for TOOL in $(TOOLS); do \
		dh_auto_build --sourcedirectory=$${TOOL}; \
	done

.PHONY: override_dh_auto_install
override_dh_auto_install:
	for TOOL in $(TOOLS); do \
		dh_auto_install --sourcedirectory=$${TOOL} -- PREFIX=/usr; \
	done

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


# --------------------------------------------------------------------
# http://wiki.debian.org/onlyjob/get-orig-source
#
# As per policy §4.9, this target may be invoked in any directory.
.PHONY: get-orig-source
get-orig-source:
	uscan --noconf --destdir=$(CURDIR) --check-dirname-level=0 \
		--download-current-version --force-download $(DEB_DIR)
