#!/usr/bin/make -f
export DH_VERBOSE = 1

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

include /usr/share/dpkg/default.mk

COREBOOT_UTILS = cbfstool cbmem ifdtool intelvbttool kbc1126 nvramtool
COREBOOT_UTILS_MAN = cbfs-compression-tool cbfstool ifdtool ifittool ifwitool intelvbttool rmodtool
ifneq (,$(findstring $(DEB_HOST_ARCH), amd64 i386 x32))
COREBOOT_UTILS += msrtool bucts ectool intelmetool inteltool pmh7tool superiotool
COREBOOT_UTILS_MAN +=     bucts ectool intelmetool inteltool pmh7tool superiotool
endif

%:
	dh $@ --with sphinxdoc

# this might become needed for other tools in the future
override_dh_auto_configure:
	@echo RUN CONFIGURE KEEPS FAILING ELEGANT WORKAROUND
#	$(SHELL export PREFIX=/usr;cd util/msrtool;./configure)
	cp debian/Makefile util/msrtool

override_dh_auto_build:
	for f in $(COREBOOT_UTILS); do \
		$(MAKE) -C util/$$f;   \
	done
ifeq (,$(findstring nodoc, $(DEB_BUILD_OPTIONS)))
	# Build docs
	# Hack the Debian package version into the "release = " variable in Documentation/conf.py
	sed -i 's/^release =.*/release = "$(DEB_VERSION)"/' Documentation/conf.py
	PYTHONPATH=. http_proxy='http://127.0.0.1:9/' sphinx-build -N -E -bhtml Documentation build/html
endif

execute_before_dh_install-arch:
ifneq (,$(findstring $(DEB_HOST_ARCH), amd64 i386 x32))
	dh_install util/bucts/bucts usr/sbin
endif

execute_before_dh_installman-arch:
	mkdir -p debian/tmp
	# some executables do not support --help: fmaptool msrtool nvramtool
	for f in $(COREBOOT_UTILS_MAN); do \
		help2man -s8 -N -n "" -o debian/tmp/$$f.1 debian/coreboot-utils/usr/sbin/$$f; \
	done

override_dh_sphinxdoc:
ifeq (,$(findstring nodoc,$(DEB_BUILD_OPTIONS)))
	dh_sphinxdoc -p coreboot-utils-doc
endif
