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

include /usr/share/dpkg/pkg-info.mk

export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

CHANGELOG_DATE ?= $(shell LC_ALL=C date -u -d @$(SOURCE_DATE_EPOCH) +"%d %B %Y")

export CHIPDB_SUBDIR=fpga-icestorm/chipdb

export deb_udevdir = $(shell pkg-config --variable=udevdir udev | sed s,^/,,)

%:
	dh $@ --with sphinxdoc

override_dh_auto_clean:
	dh_auto_clean
	$(RM) debian/man/*.1
	$(RM) -r icebox/__pycache__
	$(RM) -r docs/build
	for dir in examples/*/; do make -C $$dir clean; done

execute_after_dh_auto_build:
	make -C docs html

override_dh_auto_install:
	PREFIX=/usr dh_auto_install

override_dh_install:
	dh_install

        # Note: dh_install will install icebox source files twice, since we
        # can't tell it only to install non-overlapping files in the
        # install file, we fix this up here.
        #
        # Also, on arch:all builds only the chipdb package gets built so
        # the fpga-icestorm directory will be missing.
	if [ -d debian/fpga-icestorm ]; then \
		cd debian/fpga-icestorm/usr/share/fpga-icestorm/python/ && \
		find . -type f -print0 | xargs -0 -n1 -I{} rm ../../../bin/{}; \
	fi

override_dh_installman:
	cd debian/man ; CHANGELOG_DATE="$(CHANGELOG_DATE)" ./genmanpages.sh
	dh_installman