#!/usr/bin/make -f

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

include /usr/share/dpkg/architecture.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

# Get FPC and CGE units installation path
CGE_UNITS_DIR=${FPCDIR}/units/castle-game-engine

# 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'))
BUILDOPTS=-k"${LDFLAGS}"\
	-dRELEASE \
	-Mobjfpc \
	-Sh \
	-Ci \
	-Sm \
	-Sc \
	-Sg \
	-Si \
	-O2 \
	-Xs \
	-Fu${CGE_UNITS_DIR} \

#export DH_VERBOSE=1

%:
	dh ${@}

override_dh_auto_clean:
	$(MAKE) clean-code
	dh_auto_clean

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

	fpc $(BUILDOPTS) -Ficode -Fucode view3dscene.lpr
	fpc $(BUILDOPTS) -Ficode -Fucode tovrmlx3d.lpr

	inkscape --export-width=48 --export-height=48 freedesktop/view3dscene.svg \
		--export-filename=freedesktop/view3dscene.png

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.
	mkdir -p $(TMP_DIR)/whitedune
	cp -R /usr/share/doc/whitedune-docs/ $(TMP_DIR)/whitedune
	PATH=$$PATH:$(CURDIR) jenkins_scripts/run_tests.sh $(TMP_DIR)/run_tests_errors.txt $(TMP_DIR)/run_tests_output.txt
	cat $(TMP_DIR)/run_tests_errors.txt

override_dh_auto_install:
