#!/usr/bin/make -f

export DH_VERBOSE=1

export DEB_BUILD_MAINT_OPTIONS := hardening=+all
export DH_ALWAYS_EXCLUDE := COPYING:LICENSE
export LANG:=C

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

# Set FPCVER
FPCVER=$(shell fpc -iV)

# Define FPC units installation default directory
FPCDIR=/usr/lib/${DEB_TARGET_MULTIARCH}/fpc/${FPCVER}

# Get directories
CURDIR:=$(shell pwd)
TMP_DIR=$(CURDIR)/debian/tmp

# Set default compilation options
LDFLAGS=$(strip $(shell DEB_BUILD_MAINT_OPTIONS=$(DEB_BUILD_MAINT_OPTIONS) \
	dpkg-buildflags --get LDFLAGS | \
	sed -e 's/-Wl,//g' -e 's/,//g' \
	-e 's1-specs=/usr/share/dpkg/.*\.specs11' \
	-e 's/-ffat-lto-objects//'))
BUILDOPTS=$(addprefix --compiler-option=-k,${LDFLAGS})\
	--compiler-option=@castle-game-engine \

%:
	dh ${@}

execute_after_dh_auto_clean:
	$(MAKE) clean-code

override_dh_auto_build:override_dh_auto_build-arch override_dh_auto_build-indep
override_dh_auto_build-arch:
	mkdir -p $(TMP_DIR)

	# Once pasdoc ships file_to_pascal_string, the next lines can be simplified
	fpc -FE$(TMP_DIR) debian/file_to_pascal_string.dpr
	PATH=${PATH}:$(TMP_DIR) $(MAKE) generate-code

	${MAKE} compile CASTLE_ENGINE_TOOL_OPTIONS='$(BUILDOPTS)'
	inkscape --export-width=48 --export-height=48 freedesktop/castle-model-viewer.svg \
		--export-filename=freedesktop/castle-model-viewer

override_dh_auto_build-indep:

override_dh_auto_test:
	# Code to run the package test suite.
	# We need the directories where the files to test are to be
	# writeable by the current (non-root) user. Copying them in
	# the current tree is the easiest way to accomplish that.
	#
	# TBD. New tests planned using castle-game-engine

override_dh_auto_install:
override_dh_auto_install-arch:
override_dh_auto_install-indep:

lintian:
	lintian -I -i ../castle-model-viewer_${DEB_VERSION}_${DEB_HOST_ARCH}.changes
