#!/usr/bin/make -f
# debian/rules for Castle Game Engine

include /usr/share/dpkg/pkg-info.mk
export DH_ALWAYS_EXCLUDE := COPYING:LICENSE
export LANG:=C

# Documentation type to use chm/html
ifeq (${DEBDOCTYPE},)
DOCTYPE=html
# HTML Converter chmdocs or htmldocs for (default)
CONVERTER=${DOCTYPE}docs
else
DOCTYPE=${DEBDOCTYPE}
CONVERTER=${DOCTYPE}docs
endif
# Define FPC
ifndef FPC
FPC=/usr/bin/fpc
endif
# Set FPCVER
FPCVER=$(shell ${FPC} -iV)

CPU_TARGET?=$(shell ${FPC} -iTP)
OS_TARGET?=$(shell ${FPC} -iTO)
FPCTARGET=${CPU_TARGET}-${OS_TARGET}
export CGE_VERSION=$(shell echo ${DEB_VERSION_UPSTREAM} | sed -e 's/^\([0-9\.]*\).*/\1/')
DEB_BUILD=$(lastword $(subst -, ,${DEB_VERSION}))
# Get directories
INSTALL_DIR=$(CURDIR)/debian/tmp
CFG_DIR=${INSTALL_DIR}/etc/fpc-${FPCVER}.cfg.d
LIB_PREFIX=${INSTALL_DIR}/usr/lib
LIB_DIR=${LIB_PREFIX}/${DEB_SOURCE}
SRC_DIR=$(CURDIR)/debian/src/${DEB_SOURCE}-${CGE_VERSION}
TMP_DIR=${INSTALL_DIR}/tmp
# Define units installation default directory as recommended by Debian FPC Team
UNITS_DIR=${LIB_PREFIX}/${DEB_HOST_MULTIARCH}/${DEB_SOURCE}/fp-units/${FPCVER}
# Get utils
ifndef MKDIR
MKDIR=mkdir -p
endif
ifndef CP
CP=cp -Rfpl
endif
RM:=rm -rf

#export DH_VERBOSE=1

%:
	dh ${@}

override_dh_auto_clean:
	${MAKE} clean
	$(MAKE) strip-precompiled-libraries
	dh_auto_clean

prepare-source: prepare-source-stamp
prepare-source-stamp:
	# Building Pascal style font file first while making sure that
	# no unit or object files polute the source tree
	${MKDIR} ${TMP_DIR}
	${FPC} -FE${TMP_DIR} @castle-fpc.cfg @hardening \
		-otexture-font-to-pascal \
		tools/texture-font-to-pascal/texture_font_to_pascal.lpr
	cd src/fonts ; PATH=${PATH}:${TMP_DIR} ./mk_fonts.sh /usr/share/fonts/truetype/dejavu

	# cge embeds some images from xcf via png into Pascal source files
	# First we need to create the pngs
	for pngfile in $$(grep \\\.png\$$ debian/clean) ; do convert -layers flatten -background transparent $${pngfile%.png}.xcf $${pngfile} ; done
	# Then we build the helper function
	${FPC} -FE${TMP_DIR} -dRELEASE @castle-fpc.cfg @hardening \
		-oimage-to-pascal \
		tools/image-to-pascal/image_to_pascal.lpr
	# To finally convert png files to pas file
	PATH=${TMP_DIR} ${MAKE} -C src/ui/gui-images all

	# Upstream source contains auto-generated include files. Let's generate
	# them again.
	${FPC} -FE${TMP_DIR} -dRELEASE @castle-fpc.cfg @hardening \
		-Futools/internal/x3d-nodes-to-pascal/code \
		-ox3d-nodes-to-pascal \
		tools/internal/x3d-nodes-to-pascal/x3d_nodes_to_pascal.lpr
	cd tools/internal/x3d-nodes-to-pascal/ && \
		PATH=${TMP_DIR} x3d-nodes-to-pascal \
		--input-path nodes-specification/ \
		--output-path ../../../src/scene/x3d/auto_generated_node_helpers/

	# For reproducible builds we need to make sure the timestamp of the just
	# created files is fixed. Let's use SOURCE_DATE_EPOCH for that. For the
	# future, this needs to be even smarter, because now every Debian revision
	# will require all the reverse dependencies generating ppu files to be
	# rebuild.
	touch --no-dereference --date="@$${SOURCE_DATE_EPOCH}" \
		src/scene/x3d/auto_generated_node_helpers/*.inc \
		src/fonts/castletexturefont_*.pas \
		src/ui/castlecontrols_*.inc \

	touch prepare-source-stamp

# Annoying thing is we need to do this very early to save lots of overhead. If
# it can be done on a clean tree, right after prepare-source we don't need
# cleaning, right.
install-source: install-source-stamp
install-source-stamp:
	@echo "--- Copying the source code"
	dh_testdir
	${MKDIR} ${SRC_DIR}
	${CP} -t ${SRC_DIR} \
		$(CURDIR)/src/*
	# Remove statically linked libraries and object files from source packages
	find ${SRC_DIR} -name '*.a' -o -name '*.o' -o -name '*.obj' -delete
	# Fix files permission
	find $(SRC_DIR) -name '*.bmp' -o -name '*.pas' -exec chmod 644 '{}' ';'
	# Remove empty directories
	find ${SRC_DIR} -empty -delete
	touch install-source-stamp

override_dh_auto_build-arch: prepare-source install-source
	# Build the real engine
	${MAKE} build-using-fpmake tools
	# Generate man pages
	${MAKE} -C doc/man/man1

override_dh_auto_install-arch:
	# Install units
	${MKDIR} ${UNITS_DIR}
	${CP} -t ${UNITS_DIR} units/${FPCTARGET}/*
	# Mark package as manually compilable and install them
	for package in $$(find * -name '*.lpk') ; \
	do \
		pkgDir=$$(dirname "${LIB_DIR}/$${package}") ; \
		${MKDIR} "$${pkgDir}" ; \
		sed -e 's@\(\W*\)<CompilerOptions>.*@\1<AutoUpdate Value="Manually"/>\n&@' \
		    -e "s@\.\./src/@$$(relpath ${SRC_DIR} $${pkgDir})/@g" \
		    -e "s@\(\W*\)<UnitOutputDirectory .*@<UnitOutputDirectory Value=\"$$(relpath ${UNITS_DIR} $${pkgDir})\"/>@" \
		    "$${package}" > "${LIB_DIR}/$${package}" ; \
	done
	# Call upstream install target for the tools
	${MAKE} PREFIX=${INSTALL_DIR}/usr install
	# Install configration file so we can use: fpc @castle-game-engine progname
	${MKDIR} ${CFG_DIR}
	${CP} debian/castle-game-engine.cfg ${CFG_DIR}/castle-game-engine
	# Perform cleanup
	find ${INSTALL_DIR} -empty -delete
	fpc-depends

override_dh_auto_build-indep: prepare-source install-source
	@echo "--- Building Documentation"
	# Create logo first, before generating documentation
	${MAKE} -C doc/pasdoc/logo
	${MAKE} -C doc/pasdoc ${DOCTYPE}
	# pasdoc create a jquery file, but we link from jquery instead
	${RM} doc/reference/tipuesearch/jquery.min.js
	# Remove .npmignore file as Lintian complains about it.
	${RM} doc/reference/castle-engine-website-base/node_modules/slick-carousel/.npmignore
	# Remove windows executable files as Lintian complains about them.
	${RM} tools/contrib/x86_64-win64

override_dh_auto_install-indep: install-source

execute_before_dh_debputy:
	mkdir -p ${INSTALL_DIR}/packages
	echo 'If you are looking for CGE documentation, please install the package named' > ${INSTALL_DIR}/packages/README.txt
	echo 'castle-game-engine-doc.' >> ${INSTALL_DIR}/packages/README.txt

.PHONY:prepare-source install-source

lintian:
	lintian -I -i ../${DEB_SOURCE}_${DEB_VERSION}.dsc
