#!/usr/bin/make -f

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

%:
	dh $@ --parallel --max-parallel=4 --with autotools_dev

ifeq ($(DEB_BUILD_ARCH_OS),hurd)
OPTARGS:= --disable-alsa
endif
ifeq ($(DEB_BUILD_ARCH_OS),kfreebsd)
OPTARGS:= --disable-alsa
endif

override_dh_auto_clean:
	dh_autotools-dev_updateconfig
	./configure \
            --datadir=/usr/share/residualvm \
            --enable-release \
            --enable-verbose-build \
            --prefix=/usr $(OPTARGS)
	dh_autotools-dev_restoreconfig
	dh_auto_clean

override_dh_auto_configure:
	./configure \
            --datadir=/usr/share/residualvm \
            --enable-release \
            --enable-verbose-build \
            --prefix=/usr $(OPTARGS)

FPATH=gui/themes/modern
override_dh_auto_build:
	## replacing bundled fonts...
	for F in $(FPATH)/Free*.ttf; do cp -v /usr/share/fonts/truetype/freefont/$$(basename $${F}) $(FPATH)/ ;done
	## re-compressing "modern" theme...
	cd $(FPATH) && zip -9 ../modern.zip -r .
	## build executable(s)...
	dh_auto_build

override_dh_auto_test:
	chmod +x test/cxxtest/cxxtestgen.py    # correcting permissions
	-dh_auto_test

override_dh_installchangelogs:
	dh_installchangelogs NEWS

# gracefully handle stripping if -dbg package (un-)commented in debian/control
override_dh_strip:
	[ -d "$(CURDIR)/debian/residualvm-dbg" ] \
        && dh_strip --dbg-package=residualvm-dbg \
        || dh_strip

override_dh_gencontrol:
	dh_gencontrol -- -Vmy:Built-Using="$(foreach pkg,fonts-freefont-ttf,$(word 2,$(shell dpkg --status $(pkg) | grep Source)) (=$(word 2,$(shell dpkg --status $(pkg) | grep Version))),)"

override_dh_builddeb:
	dh_builddeb -- -Zxz

# get-orig-source
PKD  = $(abspath $(dir $(MAKEFILE_LIST)))
PKG  = $(word 2,$(shell dpkg-parsechangelog -l$(PKD)/changelog | grep ^Source))
VER ?= $(shell dpkg-parsechangelog -l$(PKD)/changelog | perl -ne 'print $$1 if m{Version:\s*([\d\.]+)}')
DTYPE := +dfsg
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER)$(DTYPE).orig.tar.xz
	@

$(PKG)_$(VER)$(DTYPE).orig.tar.xz:
	@echo "# Downloading..."
	uscan --noconf --verbose --rename --destdir=$(CURDIR) --check-dirname-level=0 --force-download --download-version $(VER) $(PKD)
	@echo "# Extracting..."
	mkdir $(PKG)-$(VER) \
	&& tar xf $(PKG)_$(VER).orig.tar.* --directory $(PKG)-$(VER) --strip-components 1 \
	|| $(RM) -r $(PKG)-$(VER)
	@echo "# Clean-up..."
	cd $(PKG)-$(VER) \
	&& $(RM) -r -v \
             gui/themes/fonts/topaz    \
             gui/themes/fonts/Arial*
	@echo "# Packing..."
	tar -caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" "$(PKG)-$(VER)" \
	&& $(RM) -r "$(PKG)-$(VER)"
