#!/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_configure:
	./configure \
            --datadir=/usr/share/scummvm \
            --enable-release \
            --enable-verbose-build \
            --prefix=/usr $(OPTARGS)

FPATH=gui/themes/scummmodern
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 ../scummmodern.zip -r .
	## build executable(s)...
	dh_auto_build

override_dh_installchangelogs:
	dh_installchangelogs NEWS

# gracefully handle stripping if -dbg package (un-)commented in debian/control
override_dh_strip:
	[ -d "$(CURDIR)/debian/scummvm-dbg" ] \
        && dh_strip --dbg-package=scummvm-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 \
             .*DS_Store                \
             gui/themes/fonts/topaz    \
             gui/themes/fonts/Arial*   \
             */*/*/*/*.o*              \
             */*/*/*.dll
	@echo "# Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print \
        | sort | XZ_OPT="-7v" tar -caf "$(PKG)_$(VER)$(DTYPE).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
        && $(RM) -r "$(PKG)-$(VER)"
