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

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

export DEB_BUILD_MAINT_OPTIONS = hardening=+all

WAF := python3 ./waf
CONFIGUREFLAGS := --debug

ifneq (,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    NUMCPUS := $(shell getconf _NPROCESSORS_ONLN 2>/dev/null || echo 1)
    NUMJOBS := $(patsubst parallel=%,%,$(filter parallel=%,$(DEB_BUILD_OPTIONS)))
    TOOMANYJOBS := $(shell [ $(NUMJOBS) -gt $(NUMCPUS) ] && echo true)
  ifeq ($(TOOMANYJOBS),true)
      BUILDFLAGS := -j$(NUMCPUS)
  else
      BUILDFLAGS := -j$(NUMJOBS)
  endif
endif

PKG := $(CURDIR)/debian/zutty
HIC := $(PKG)/usr/share/icons/hicolor
P2M := pod2man -s 1 -u -c Zutty -r $(DEB_VERSION_UPSTREAM)

%:
	dh $@

override_dh_auto_build:
	$(WAF) build $(BUILDFLAGS)
	$(P2M) debian/zutty.pod > debian/zutty.1

override_dh_auto_configure:
	cp -pf version.txt version.orig
	$(WAF) configure $(CONFIGUREFLAGS) --prefix=/usr

override_dh_auto_install:
	$(WAF) install --destdir=$(PKG)
	mv $(PKG)/usr/bin/zutty.dbg $(PKG)/usr/bin/zutty
	test -f version.orig && mv -f version.orig version.txt || true

execute_after_dh_installdirs:
	cp -p icons/zutty.desktop $(PKG)/usr/share/applications/
	cp -p icons/zutty.svg $(HIC)/scalable/apps/
	cp -p icons/zutty_128x128.png $(HIC)/128x128/apps/zutty.png
	cp -p icons/zutty_96x96.png $(HIC)/96x96/apps/zutty.png
	cp -p icons/zutty_64x64.png $(HIC)/64x64/apps/zutty.png
	cp -p icons/zutty_48x48.png $(HIC)/48x48/apps/zutty.png
	cp -p icons/zutty_32x32.png $(HIC)/32x32/apps/zutty.png
	cp -p icons/zutty_16x16.png $(HIC)/16x16/apps/zutty.png

override_dh_auto_clean:
	$(WAF) clean || true
	find -name "*.pyc" -delete
	rm -rf build .waf*
	rm -f .lock-waf_linux_build debian/zutty.1
	dh_auto_clean
