#!/usr/bin/make -f
# -*- makefile -*-

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

export DEB_BUILD_MAINT_OPTIONS=hardening=+all
$(info DEB_BUILD_MAINT_OPTIONS:$(origin DEB_BUILD_MAINT_OPTIONS)=$(DEB_BUILD_MAINT_OPTIONS))

%:
	dh $@ --parallel

override_dh_auto_build:
	# generate icon
	convert image/fheroes2_32x32.png debian/fheroes2.xpm
	# make translations
	#for lo in cs es fr hu nl pl pt_BR ru sv tr; do \
	#	$(MAKE) -C files/lang/$${lo}/LC_MESSAGES; \
	#done
	CFLAGS="$(CFLAGS) $(CPPFLAGS)" LDFLAGS="$(LDFLAGS)" \
	  dh_auto_build -- -C src \
	    WITH_AI=simple \
	    RELEASE=1 \
	    WITH_TOOLS=1 \
	    DEBUG=1

override_dh_compress:
	dh_compress --exclude=.key

override_dh_builddeb:
	dh_builddeb -- -Zxz

## http://wiki.debian.org/onlyjob/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+:)?(\d.*)(?:\-\d+.*)};')
SVND = $(shell echo $(VER) | perl -ne 'print $$1 if m/\+svn(\d+)/')
SVNR = $(shell echo $(VER) | perl -ne 'print $$1 if m/\+svn\d+r(\d+)/')
SVNU = svn://svn.code.sf.net/p/fheroes2/code/trunk/fheroes2
SVNO = --config-option config:miscellany:use-commit-times=yes
.PHONY: get-orig-source
get-orig-source: $(PKG)_$(VER).orig.tar.xz  $(info I: $(PKG)_$(VER))
	@

$(PKG)_$(VER).orig.tar.xz:
	$(if $(wildcard $(PKG)-$(VER)),$(error $(PKG)-$(VER) exist, aborting..))
	(svn checkout $(SVNO) -r $(SVNR) $(SVNU) $(PKG)-$(VER) || svn checkout $(SVNO) -r {$(SVND)} $(SVNU) $(PKG)-$(VER)) \
        || $(RM) -r $(PKG)-$(VER)
	@echo "Clean-up..."
	cd $(PKG)-$(VER) \
        && find . -depth -name ".svn" -exec $(RM) -r '{}' \; \
        && $(RM) -r src/engine/wince files/stats/*.7z src/xmlccwrap/[gt]* .bzrignore
	@echo "Packing..."
	find -L "$(PKG)-$(VER)" -xdev -type f -print | sort \
        | XZ_OPT="-6v" tar -caf "$(PKG)_$(VER).orig.tar.xz" -T- --owner=root --group=root --mode=a+rX \
        && $(RM) -r "$(PKG)-$(VER)"
